栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 面试经验 > 面试问答

我无法解决的小GUI问题。JTextFields

面试问答 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

我无法解决的小GUI问题。JTextFields

这是我创建的GUI。

成绩计算器

  1. 我不知道您的主要方法在哪里,但是您必须始终通过调用SwingUtilities invokeLater方法来启动Swing应用程序。invokeLater方法将Swing组件的创建和执行放在事件调度线程(EDT)上。

  2. 使用GridBagLayout时,我使用创建的addComponent方法为每个Swing组件创建唯一的GridBagConstraints。我不想记住默认值。

  3. Jframe方法的顺序非常重要。在此示例中,记住Jframe方法的顺序。

  4. 我把指令放在Jtextarea中。这样,指令文本将根据Jtextarea的大小进行拆分。无需对HTML的换行符进行硬编码。

Here’s the pre.

package com.ggl.testing;import java.awt.Component;import java.awt.Container;import java.awt.GridBagConstraints;import java.awt.GridBagLayout;import java.awt.Insets;import javax.swing.Jframe;import javax.swing.JLabel;import javax.swing.JPanel;import javax.swing.JScrollPane;import javax.swing.Jtextarea;import javax.swing.JTextField;import javax.swing.SwingUtilities;public class GradeAnalysis implements Runnable {    private static final Insets normalInsets = new Insets(10, 10, 0, 10);    private static final Insets finalInsets = new Insets(10, 10, 10, 10);    public static void main(String[] args) {        SwingUtilities.invokeLater(new GradeAnalysis());    }    @Override    public void run() {        Jframe frame = new Jframe("Grade Calculator");        frame.setDefaultCloseOperation(Jframe.EXIT_ON_CLOSE);        frame.add(createMainPanel());        frame.pack();        frame.setVisible(true);    }    private JPanel createMainPanel() {        GridBagConstraints gbc = new GridBagConstraints();        // Adding the JPanels. Panel for instructions        JPanel panel = new JPanel();        panel.setLayout(new GridBagLayout());        int gridy = 0;        // JLabel for the Instructions.        Jtextarea instructiontextarea = new Jtextarea(5, 30);        instructiontextarea.setEditable(false);        instructiontextarea.setLineWrap(true);        instructiontextarea.setWrapStyleWord(true);        instructiontextarea.setText(getInstructions());        JScrollPane instructionScrollPane = new JScrollPane(instructiontextarea);        addComponent(panel, instructionScrollPane, 0, gridy++, 3, 1,     finalInsets, GridBagConstraints.CENTER,     GridBagConstraints.HORIZONTAL);        // JLabels for Assignment/Grade/Weight(Percent)        JLabel label1 = new JLabel("Assignment");        label1.setHorizontalAlignment(JLabel.CENTER);        addComponent(panel, label1, 0, gridy, 1, 1, finalInsets,     GridBagConstraints.CENTER, GridBagConstraints.HORIZONTAL);        JLabel label2 = new JLabel("Mark");        label2.setHorizontalAlignment(JLabel.CENTER);        addComponent(panel, label2, 1, gridy, 1, 1, finalInsets,     GridBagConstraints.CENTER, GridBagConstraints.HORIZONTAL);        JLabel label3 = new JLabel("Weight");        label3.setHorizontalAlignment(JLabel.CENTER);        addComponent(panel, label3, 2, gridy++, 1, 1, finalInsets,     GridBagConstraints.CENTER, GridBagConstraints.HORIZONTAL);        // JLabel Numbers for the number list of assignments at the side.        JLabel number = new JLabel("1");        number.setHorizontalAlignment(JLabel.CENTER);        addComponent(panel, number, 0, gridy, 1, 1, normalInsets,     GridBagConstraints.CENTER, GridBagConstraints.HORIZONTAL);        // JTextfield for Mark        JTextField mark = new JTextField(20);        mark.setHorizontalAlignment(JLabel.CENTER);        addComponent(panel, mark, 1, gridy, 1, 1, normalInsets,     GridBagConstraints.CENTER, GridBagConstraints.HORIZONTAL);        // JTextfield for Weight        JTextField weight = new JTextField(20);        weight.setHorizontalAlignment(JLabel.CENTER);        addComponent(panel, weight, 2, gridy++, 1, 1, normalInsets,     GridBagConstraints.CENTER, GridBagConstraints.HORIZONTAL);        return panel;    }    private String getInstructions() {        return "Instructions: Type in the grades you’ve received, along with the weights "     + "they’ll have in the determination of your overall average. After you "     + "press ‘Calculate’, the results will show your average so far. Every "     + "grade you enter must be a non-negative number, and every "     + "percentage/weight you enter must be a positive number :)";    }    private void addComponent(Container container, Component component, int gridx, int gridy, int gridwidth, int gridheight, Insets insets, int anchor, int fill) {        GridBagConstraints gbc = new GridBagConstraints(gridx, gridy,     gridwidth, gridheight, 1.0D, 1.0D, anchor, fill, insets, 0, 0);        container.add(component, gbc);    }}


转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/428632.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号