当前位置: 首页 > 面试题库 >

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

何承
2023-03-14
问题内容

我无法管理的小错误。所以现在我的程序GUI如下所示:

现在,“标记”列下有一个TextField,用户可以输入其数据。我也想对重量部分使用相同的功能,因为我想在“重量”列下插入一个TextField。

但是,当我尝试放入TextField时,当窗口较小时,两个Textfield都将像这样旋转:

而当窗口放大时:

我该如何做,以便在“标记和权重”下有一个文本字段?

Code:

public class Gradeanalysis implements ActionListener {

public void actionPerformed (ActionEvent e){
     GridBagConstraints gbc = new GridBagConstraints();

    //Adding the JPanels. Panel for instructions
    JPanel panel = new JPanel();
    panel.setLayout(new GridBagLayout());

    //JLabel for the Instructions.
    JLabel label = new JLabel("<html> Instructions: Type in the grades you’ve received, along with the weights they’ll have in the determination of your overall average. <br> After you press ‘Calculate’, the results will show your average so far. <br> Every grade you enter must be a non-negative number, and every percentage/weight you enter must be a positive number :)</html>");
    gbc.fill = GridBagConstraints.HORIZONTAL;
    gbc.gridwidth = 2;
    gbc.gridy = 0;
    panel.add(label, gbc);


    //JLabel1 for Assingment/Grade/Weight(Percent)
    JLabel label1 = new JLabel("<html><pre>Assingment\t\t\t\t\t  Mark\t\t\t\t\tWeight</pre></html>");
    gbc.fill = GridBagConstraints.HORIZONTAL;
    gbc.gridx = 0;
    gbc.gridy = 1;
    gbc.anchor = GridBagConstraints.NORTH;
    panel.add(label1, gbc);

    //JLabel Numbers for the number list of assingments at the side.
    JLabel numbers = new JLabel("1");
    gbc.gridx = 0;
    gbc.gridy = 2;
    gbc.anchor = GridBagConstraints.NORTH;
    gbc.weighty = 1;
    panel.add(numbers, gbc);

    //JTextfield for Mark
    JTextField mark = new JTextField(2);
    gbc.fill = GridBagConstraints.NONE;
    gbc.gridy = 2;
    panel.add(mark, gbc);


    //JTextfield for Weight
    JTextField weight = new JTextField(2);
    gbc.gridx = 2;
    panel.add(weight, gbc);


    //New frame set
    JFrame frame = new JFrame("Grade Calculator-- ");
    frame.setVisible(true);
    frame.setSize(750,700);
    frame.setDefaultCloseOperation(EXIT_ON_CLOSE);
    frame.add(panel);




}

}

Thanks for reading.


问题答案:

这是我创建的GUI。

成绩计算器

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

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

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

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

Here’s the code.

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);
    }

}


 类似资料:
  • 我们下载了项目“https://bitbucket.org/m2m/cumulocity-clients-/src/develop/”并尝试运行该项目,但我们遇到了以下问题: [信息]正在扫描项目...[错误][错误]处理POM时遇到一些问题:[致命]com.nsn.cumulocity.clients的父POM不可解析-Java:clients-Java:9.19.1-快照:无法在https:/

  • 本文向大家介绍解决无法配置SQL2005问题,包括了解决无法配置SQL2005问题的使用技巧和注意事项,需要的朋友参考一下 问题:点击“SQL Server Configuration Manager”却显示“无法连接到WMI 提供程序 请注意,你只能使用SQL Server 配置管理器来管理SQL Server 2005服务器。找不到指定的模块。[0x8007007e]” 解决方法: 1、到sy

  • 本文向大家介绍解决springboot无法注入JpaRepository的问题,包括了解决springboot无法注入JpaRepository的问题的使用技巧和注意事项,需要的朋友参考一下 使用内置服务器启动springboot项目时,会从@SpringBootApplication修饰类所在的包开始,加载当前包和所有子包下的类,将由@Component @Repository @Service

  • 本文向大家介绍解决layer.prompt无效的问题,包括了解决layer.prompt无效的问题的使用技巧和注意事项,需要的朋友参考一下 使用H-UI框架中的layer弹出层时发现 layer.js中没有layer.prompt,如果想要使用layer.prompt可以使用layer中的use从扩展中加载此扩展方法 代码如下: 之后就可以正常使用了。 以上这篇解决layer.prompt无效的问

  • 所以我现在在谷歌表格脚本编辑器中有这个代码。我想做的是设置一个函数,当特定的编辑发生在指定的列中时,它会向发生编辑的行中相邻单元格中的电子邮件地址发送电子邮件。 但我一直遇到这样的错误:TypeError:sheet。getRange(…)。getvalue不是函数(第4行,文件“代码”) 以下是我目前掌握的代码: ,,,