我无法解决的小GUI问题。 JTextField的 [英] Small GUI issue I cannot fix. JTextFields

查看:58
本文介绍了我无法解决的小GUI问题。 JTextField的的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

小错误我无法做到。所以现在我的程序GUI看起来像这样:



现在Mark栏下面有一个TextField是用户可以输入他们的数据。我还想在重量部分使用相同的内容,我想在重量列下插入一个TextField。



然而,当我尝试放入TextField时,当窗口很小时,Textfields都会像这样转动:



当窗口放大时:


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


  • JFrame方法的顺序是非常重要。记住此示例中JFrame方法的顺序。


  • 我将指令放在JTextArea中。这样,指令文本根据JTextArea的大小进行拆分。没有必要用HTML硬编码换行符。


  • 这是代码。

      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;

    公共类GradeAnalysis实现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();

    //添加JPanel。说明小组
    JPanel panel = new JPanel();
    panel.setLayout(new GridBagLayout());

    int gridy = 0;

    //指令的JLabel。
    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);

    //分配的JLabel /等级/重量(百分比)
    JLabel label1 = new JLabel(Assignment);
    label1.setHorizo​​ntalAlignment(JLabel.CENTER);
    addComponent(panel,label1,0,gridy,1,1,finalInsets,
    GridBagConstraints.CENTER,GridBagConstraints.HORIZONTAL);

    JLabel label2 =新JLabel(Mark);
    label2.setHorizo​​ntalAlignment(JLabel.CENTER);
    addComponent(panel,label2,1,gridy,1,1,finalInsets,
    GridBagConstraints.CENTER,GridBagConstraints.HORIZONTAL);

    JLabel label3 =新的JLabel(重量);
    label3.setHorizo​​ntalAlignment(JLabel.CENTER);
    addComponent(panel,label3,2,gridy ++,1,1,finalInsets,
    GridBagConstraints.CENTER,GridBagConstraints.HORIZONTAL);

    // JLabel侧面分配号码列表的编号。
    JLabel number = new JLabel(1);
    number.setHorizo​​ntalAlignment(JLabel.CENTER);
    addComponent(panel,number,0,gridy,1,1,normalInsets,
    GridBagConstraints.CENTER,GridBagConstraints.HORIZONTAL);

    // JTextfield for Mark
    JTextField mark = new JTextField(20);
    mark.setHorizo​​ntalAlignment(JLabel.CENTER);
    addComponent(panel,mark,1,gridy,1,1,normalInsets,
    GridBagConstraints.CENTER,GridBagConstraints.HORIZONTAL);

    // JTextfield for Weight
    JTextField weight = new JTextField(20);
    weight.setHorizo​​ntalAlignment(JLabel.CENTER);
    addComponent(panel,weight,2,gridy ++,1,1,normalInsets,
    GridBagConstraints.CENTER,GridBagConstraints.HORIZONTAL);

    返回面板;
    }

    private String getInstructions(){
    return说明:输入您收到的成绩,以及权重
    +他们将会确定您的总体平均值。在
    +之后按计算,结果将显示您到目前为止的平均值。您输入的每个
    +等级必须是非负数,并且您输入的每个
    +百分比/重量必须是正数:);
    }

    private void addComponent(容器容器,组件组件,
    int gridx,int gridy,int gridwidth,int gridheight,Insets insets,
    int anchor,int填充){
    GridBagConstraints gbc = new GridBagConstraints(gridx,gridy,
    gridwidth,gridheight,1.0D,1.0D,anchor,fill,insets,0,0);
    container.add(component,gbc);
    }

    }


    Small error I can't manage to do. So right now my program GUI looks like this:

    Now there is a TextField under the 'Mark' column were the user can input their data. I also want the same for the weight section were I want to insert a TextField right under 'Weight' column.

    However when I try and put in a TextField, both the the Textfields turn like this when the window is small:

    and this when the window is enlarged:

    How can I make it so that there is a textfield under Mark AND Weight?

    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.

    解决方案

    Here's the GUI I created.

    1. I don't know where your main method is, but you must always start a Swing application with a call to the SwingUtilities invokeLater method. The invokeLater method puts the creation and execution of the Swing components on the Event Dispatch thread (EDT).

    2. When I use the GridBagLayout, I use the addComponent method I created to create a unique GridBagConstraints for each Swing component. I don't like to remember defaults.

    3. The order of the JFrame methods is extremely important. Memorize the order of the JFrame methods in this example.

    4. I put the instructions in a JTextArea. This way, the instruction text splits based on the size of the JTextArea. There's no need to hard code the line breaks with 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);
        }
    
    }
    

    这篇关于我无法解决的小GUI问题。 JTextField的的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

    查看全文
    登录 关闭
    扫码关注1秒登录
    发送“验证码”获取 | 15天全站免登陆