有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java GridBagLayout中的swing定位组件

我对Java比较陌生,我正在制作一个应用程序,帮助我估算建筑成本。我为它构建了后端,但按钮和文本字段都在一行中,这使得应用程序太宽了,无法放入我的屏幕。我现在尝试使用3x6网格袋布局来组织如下组件:

      Title
question1  *answer*
question2  *answer*
question3  *answer*
question4  *answer*
  --Calculate--
Estimate:    $***

当我运行程序时,我会被赋予“非法组件位置” 错误(粘贴在底部),此后不再弹出任何内容 我开始添加网格约束。问题不在于JButton或其动作侦听器。这是密码,抱歉 缺乏评论:

import java.util.*;
import packagepackage.HintTextFieldUI; 
import java.awt.*;
import java.awt.event.*;
import java.security.PublicKey;

import javax.swing.*;
import javax.swing.text.JTextComponent;
public class CP_GUI extends JFrame {
    
    public JLabel tLabel;
    
    public JTextField linear; 
    public JLabel liLabel;
    
   public JComboBox<String> sump; 
   public JLabel suLabel;
   
   public JComboBox<String> elec; 
   public JLabel elLabel;
   
    public JTextField prep;
    public JLabel prLabel;
    
   public JTextField estimate;
   public JLabel esLabel;
   
     public CP_GUI() { 
        JFrame frame = new JFrame();
        JPanel panel = new JPanel();
        panel.setLayout(new GridBagLayout());
        panel.setBorder(BorderFactory.createEmptyBorder(200, 200, 100, 100));
        GridBagConstraints c = new GridBagConstraints();
        
        String title = "Drain Tile Calculator";
        tLabel = new JLabel(title);
        c.fill = GridBagConstraints.HORIZONTAL;
        c.weightx = 0.5;
        c.ipady = 40;
        c.gridwidth = 3;
        c.gridx = 0;
        c.gridy = 0;
        panel.add(tLabel, c);
        frame.setTitle(title);

        liLabel = new JLabel("Basement Perimeter Length");
        c.fill = GridBagConstraints.HORIZONTAL;
        c.weightx = 0.5;
        c.gridx = 1;
        c.gridy = 0;
        panel.add(liLabel, c);
        linear = new JTextField(10);
        c.fill = GridBagConstraints.HORIZONTAL;
        c.weightx = 0.5;
        c.gridx = 1;
        c.gridy = 0;
        panel.add(linear, c);
        
        prLabel = new JLabel("Time needed to prepare site:");
        c.fill = GridBagConstraints.HORIZONTAL;
        c.weightx = 0.5;
        c.gridx = 2;
        c.gridy = 0;
        panel.add(prLabel, c);
        prep = new JTextField(10);
        c.fill = GridBagConstraints.HORIZONTAL;
        c.weightx = 0.5;
        c.gridx = 2;
        c.gridy = 2;
        panel.add(prep, c);
        
        suLabel = new JLabel("Using:");
        c.fill = GridBagConstraints.HORIZONTAL;
        c.weightx = 0.5;
        c.gridx = 3;
        c.gridy = 0;
        panel.add(suLabel, c);
        String[] sumpo = {"New sump pump","Existing sump pump"};
        sump = new JComboBox<>(sumpo);
        c.fill = GridBagConstraints.HORIZONTAL;
        c.weightx = 0.5;
        c.gridx = 3;
        c.gridy = 2;
        panel.add(sump, c);
        
        elLabel = new JLabel("Location of electrical outlet:");
        c.fill = GridBagConstraints.HORIZONTAL;
        c.weightx = 0.5;
        c.gridx = 4;
        c.gridy = 0;
        panel.add(elLabel, c);
        String[] electo = {"There is no outlet within 6 feet of the sump pump","There is an outlet nearby, or I do not need a new pump"};
        elec = new JComboBox<>(electo);
        c.fill = GridBagConstraints.HORIZONTAL;
        c.weightx = 0.5;
        c.gridx = 4;
        c.gridy = 2;
        panel.add(elec, c);
      
        estimate = new JTextField(10);
        
        JButton calculate = new JButton("Calculate");
        calculate.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent event){
                // TODO Auto-generated method stub
                Integer linVar = Integer.parseInt(linear.getText());
                linVar *= 13;
                
                Object sumps = sump.getSelectedItem();
                Integer sumpVar = 0;
                if("New sump pump".equals(sumps)) {
                    sumpVar += 260;}
                
                Object elecs = elec.getSelectedItem();
                Integer elecsVar = 0;
                if("There is no outlet within 6 feet of the sump pump".equals(elecs)) {
                    elecsVar += 280;}
                
                Integer prepsVar = Integer.parseInt(prep.getText());
                prepsVar += 30;
                prepsVar *= 235;
                prepsVar /= 100;
                
                linVar += sumpVar += elecsVar += prepsVar;
                /* overhead*/
                linVar += 2428;
                /* tax */
                linVar *= 11;
                linVar /= 10;
                /* margin */
                linVar *= 12;
                linVar /= 10;
                String toWords = String.valueOf(linVar);
                 
                    estimate.setUI(new HintTextFieldUI(toWords, true));
                    }

        });
        c.fill = GridBagConstraints.HORIZONTAL;
        c.ipady = 40;
        c.weightx = 0.5;
        c.gridwidth = 3;
        c.gridx = 5;
        c.gridy = 0;
        panel.add(calculate, c);
        
        esLabel = new JLabel("Estimated Cost:");
        c.fill = GridBagConstraints.HORIZONTAL;
        c.weightx = 0.5;
        c.gridwidth = 2;
        c.gridx = 6;
        c.gridy = 0;
        panel.add(esLabel, c);   
        c.fill = GridBagConstraints.HORIZONTAL;
        c.weightx = 0.5;
        c.gridx = 6;
        c.gridy = 2;
        panel.add(estimate, c); 
        

        frame.add(panel, GridBagConstraints.CENTER);
        
        frame.pack();
        frame.setVisible(true);
        
        linear.setUI(new HintTextFieldUI("Perimeter length", true));
        prep.setUI(new HintTextFieldUI("Minutes of preptime", true));
     }

    public static void main(String[] args) {
       new CP_GUI();
    }        
}

我怀疑问题可能与以下事实有关: 网格本身的形状(3x6)从未定义,或与框架的布局一起定义 未正确设置为网格行李布局

错误:

Exception in thread "main" java.lang.IllegalArgumentException: illegal component position
    at java.desktop/java.awt.Container.addImpl(Container.java:1115)
    at java.desktop/java.awt.Container.add(Container.java:1033)
    at java.desktop/javax.swing.JFrame.addImpl(JFrame.java:554)
    at java.desktop/java.awt.Container.add(Container.java:493)
    at craftsmanPeak/packagepackage.CP_GUI.<init>(CP_GUI.java:163)
    at craftsmanPeak/packagepackage.CP_GUI.main(CP_GUI.java:173)

非常感谢!我真的很感激


共 (1) 个答案

  1. # 1 楼答案

    frame.add(panel, GridBagConstraints.CENTER);JFrame使用BorderLayout默认情况下,去掉GridBagConstraints.CENTER

    我也清理了你的布局(相信我)(一些陌生人在网上说)🤣), (一团乱)

    你把xy的位置搞砸了

    enter image description here

    import java.awt.EventQueue;
    import java.awt.GridBagConstraints;
    import java.awt.GridBagLayout;
    import java.awt.Insets;
    import javax.swing.BorderFactory;
    import javax.swing.JButton;
    import javax.swing.JComboBox;
    import javax.swing.JFrame;
    import javax.swing.JLabel;
    import javax.swing.JPanel;
    import javax.swing.JTextField;
    
    public class Test {
    
        public static void main(String[] args) {
            new Test();
        }
    
        public Test() {
            EventQueue.invokeLater(new Runnable() {
                @Override
                public void run() {
                    JFrame frame = new JFrame();
                    frame.add(new TestPane());
                    frame.pack();
                    frame.setLocationRelativeTo(null);
                    frame.setVisible(true);
                }
            });
        }
    
        public class TestPane extends JPanel {
    
            public JLabel tLabel;
    
            public JTextField linear;
            public JLabel liLabel;
    
            public JComboBox<String> sump;
            public JLabel suLabel;
    
            public JComboBox<String> elec;
            public JLabel elLabel;
    
            public JTextField prep;
            public JLabel prLabel;
    
            public JTextField estimate;
            public JLabel esLabel;
    
            public TestPane() {
                setLayout(new GridBagLayout());
                setBorder(BorderFactory.createEmptyBorder(16, 16, 16, 16));
                GridBagConstraints c = new GridBagConstraints();
    
                String title = "Drain Tile Calculator";
                tLabel = new JLabel(title, JLabel.CENTER);
                c.fill = GridBagConstraints.HORIZONTAL;
                c.weightx = 0.5;
                c.ipady = 40;
                c.gridwidth = GridBagConstraints.REMAINDER;
                c.anchor = GridBagConstraints.CENTER;
                c.gridx = 0;
                c.gridy = 0;
                add(tLabel, c);
    
                c = new GridBagConstraints();
                c.anchor = GridBagConstraints.LINE_END;
                c.gridy = 1;
                c.gridx = 0;
    
                liLabel = new JLabel("Basement Perimeter Length:");
                prLabel = new JLabel("Time needed to prepare site:");
                suLabel = new JLabel("Using:");
                elLabel = new JLabel("Location of electrical outlet:");
    
                add(liLabel, c);
                c.gridy++;
                add(prLabel, c);
                c.gridy++;
                add(suLabel, c);
                c.gridy++;
                add(elLabel, c);
    
                linear = new JTextField(10);
                prep = new JTextField(10);
                String[] sumpo = {"New sump pump", "Existing sump pump"};
                sump = new JComboBox<>(sumpo);
                String[] electo = {"There is no outlet within 6 feet of the sump pump", "There is an outlet nearby, or I do not need a new pump"};
                elec = new JComboBox<>(electo);
    
                c.anchor = GridBagConstraints.LINE_START;
    
                c.gridx++;
                c.gridy = 1;
                add(linear, c);
                c.gridy++;
                add(prep, c);
                c.gridy++;
                add(sump, c);
                c.gridy++;
                add(elec, c);
    
                JButton calculate = new JButton("Calculate");
    ////            calculate.addActionListener(new ActionListener() {
    ////                public void actionPerformed(ActionEvent event) {
    ////                    // TODO Auto-generated method stub
    ////                    Integer linVar = Integer.parseInt(linear.getText());
    ////                    linVar *= 13;
    ////
    ////                    Object sumps = sump.getSelectedItem();
    ////                    Integer sumpVar = 0;
    ////                    if ("New sump pump".equals(sumps)) {
    ////                        sumpVar += 260;
    ////                    }
    ////
    ////                    Object elecs = elec.getSelectedItem();
    ////                    Integer elecsVar = 0;
    ////                    if ("There is no outlet within 6 feet of the sump pump".equals(elecs)) {
    ////                        elecsVar += 280;
    ////                    }
    ////
    ////                    Integer prepsVar = Integer.parseInt(prep.getText());
    ////                    prepsVar += 30;
    ////                    prepsVar *= 235;
    ////                    prepsVar /= 100;
    ////
    ////                    linVar += sumpVar += elecsVar += prepsVar;
    ////                    /* overhead*/
    ////                    linVar += 2428;
    ////                    /* tax */
    ////                    linVar *= 11;
    ////                    linVar /= 10;
    ////                    /* margin */
    ////                    linVar *= 12;
    ////                    linVar /= 10;
    ////                    String toWords = String.valueOf(linVar);
    ////
    ////                    estimate.setUI(new HintTextFieldUI(toWords, true));
    ////                }
    ////
    ////            });
    
                c = new GridBagConstraints();
                c.fill = GridBagConstraints.HORIZONTAL;
                c.insets = new Insets(10, 0, 10, 0);
                c.gridwidth = GridBagConstraints.REMAINDER;
                c.gridx = 0;
                c.gridy = 5;
                add(calculate, c);
    
                c = new GridBagConstraints();
                c.gridx = 0;
                c.gridy = 6;
                c.anchor = GridBagConstraints.LINE_END;
    
                esLabel = new JLabel("Estimated Cost:");
                estimate = new JTextField(10);
    
                add(esLabel, c);
    
                c.fill = GridBagConstraints.HORIZONTAL;
                c.gridx = 1;
                add(estimate, c);
    //            linear.setUI(new HintTextFieldUI("Perimeter length", true));
    //            prep.setUI(new HintTextFieldUI("Minutes of preptime", true));
            }
        }
    }