【问题标题】:Issue with JTextField and JComboBox in same GridBagLayout in JavaJava中相同GridBagLayout中的JTextField和JComboBox问题
【发布时间】:2016-01-24 16:51:03
【问题描述】:

我正在尝试使用 GridBagLayout 将这两个元素添加到框架中,但是一旦我将 JComboBox 添加到 GridBagLayout 然后将其添加到框架中,JTextField 的宽度就会变得非常短,我不知道为什么或如何解决它。当我把 JComboBox 拿出来时,它工作得很好,但我确实需要把 JComboBox 放在那里,否则我无法完成这个项目。任何有关如何解决此问题的帮助将不胜感激。代码如下:

package userInterface;
import java.awt.Dimension;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Calendar;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JTextField;

public class AddSpend extends JFrame {

    public AddSpend(){
        setTitle("Add New Spend");
        setSize(300,200);
        setVisible(true);
        setResizable(false);
        setDefaultCloseOperation(DISPOSE_ON_CLOSE);
        setLayout(new GridBagLayout());
        create();
    }

    private void create(){
        GridBagConstraints c = new GridBagConstraints();
        c.gridx = 0;
        c.gridy = 0;
        c.anchor = GridBagConstraints.LINE_END;
        add(new JLabel("Category: "), c);
        c.gridy++;
        add(new JLabel("Amount: "), c);
        c.gridy++;
        add(new JLabel("Date: "), c);

        c.gridx = 1;
        c.gridy = 0;
        c.anchor = GridBagConstraints.LINE_START;
        JComboBox<String> category = new JComboBox<String>();
        category.addItem("Test 1");
        category.addItem("Test 2");
        category.addItem("Test 3");
        add(category, c);
        c.gridy++;
        JTextField amount = new JTextField();
        add(amount, c);
        c.gridy++;
        JTextField date = new JTextField();
        add(date, c);
        c.gridy++;
        JButton today = new JButton("Today");
        today.addActionListener(new ActionListener(){
            public void actionPerformed(ActionEvent e){
                Calendar now = Calendar.getInstance();
                date.setText((now.get(Calendar.MONTH) + 1) + "/" + now.get(Calendar.YEAR));
            }
        });
        today.setFont(today.getFont().deriveFont(7.0f));
        today.setPreferredSize(new Dimension(30,15));
        add(today, c);
        c.gridy++;
        JButton add = new JButton("Add Spend");
        add(add, c);
    }
}

【问题讨论】:

    标签: java swing jframe jtextfield gridbaglayout


    【解决方案1】:

    建议:

    • 摆脱setSize(300,200); 并将其替换为pack(); 通过设置大小,您可以人为地将您的GUI 限制为可能不是最佳自然大小的特定大小。通过在将所有组件添加到 GUI 后调用 pack(),允许每个组件将自身调整为自己计算出的最佳大小。
    • 另外,将new JTextField(); 更改为new JTextField(col_width);,其中col_width 是一个int,它是您希望JTextField 显示的文本列数。这将建议 JTextField 增加其首选大小以适应 col_width 字符数。
    • 还将插图添加到您的 GridBagConstraints
    • 仅在将所有组件添加到 GUI 之后而不是之前调用 pack()setVisible(true)

    例如,

    import java.awt.Dimension;
    import java.awt.GridBagConstraints;
    import java.awt.GridBagLayout;
    import java.awt.Insets;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    import java.util.Calendar;
    
    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;
    import javax.swing.SwingUtilities;
    
    public class AddSpend extends JPanel {
    
        public AddSpend() {
            create();
        }
    
        private void create() {
            setLayout(new GridBagLayout());
            GridBagConstraints c = new GridBagConstraints();
    
            int ins = 5;
            c.insets = new Insets(ins, ins, ins, ins);
            setBorder(BorderFactory.createEmptyBorder(ins, ins, ins, ins));
    
            c.gridx = 0;
            c.gridy = 0;
            c.anchor = GridBagConstraints.LINE_END;
            add(new JLabel("Category: "), c);
            c.gridy++;
            add(new JLabel("Amount: "), c);
            c.gridy++;
            add(new JLabel("Date: "), c);
    
            c.gridx = 1;
            c.gridy = 0;
            c.anchor = GridBagConstraints.LINE_START;
            JComboBox<String> category = new JComboBox<String>();
            category.addItem("Test 1");
            category.addItem("Test 2");
            category.addItem("Test 3");
            add(category, c);
            c.gridy++;
            JTextField amount = new JTextField(10);
            add(amount, c);
            c.gridy++;
            JTextField date = new JTextField(10);
            add(date, c);
            c.gridy++;
            JButton today = new JButton("Today");
            today.addActionListener(new ActionListener() {
                public void actionPerformed(ActionEvent e) {
                    Calendar now = Calendar.getInstance();
                    date.setText((now.get(Calendar.MONTH) + 1) + "/" + now.get(Calendar.YEAR));
                }
            });
            today.setFont(today.getFont().deriveFont(7.0f));
            today.setPreferredSize(new Dimension(30, 15));
            add(today, c);
            c.gridy++;
            JButton add = new JButton("Add Spend");
            add(add, c);
        }
    
        public static void main(String[] args) {
            SwingUtilities.invokeLater(() -> {
                JFrame frame = new JFrame("Add New Spend");
                frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                frame.add(new AddSpend());
                frame.pack();
                frame.setResizable(false);
                frame.setLocationRelativeTo(null);
                frame.setVisible(true);
            });
        }
    }
    

    请注意,我更喜欢在 JFrame 上扩展 JPanel。通过让您的类扩展 JFrame,您可能会将自己画在一个角落,迫使您创建和显示 JFrame,而通常需要更大的灵活性。事实上,我敢冒险说,我创建的大部分 Swing GUI 代码扩展了 JFrame,事实上你很少愿意这样做这。更常见的是,您的 GUI 类将用于创建 JPanel,然后可以将其放置到 JFrame 或 JDialogs 或 JTabbedPanes 中,或者在需要时通过 CardLayouts 交换。这将大大增加您的 GUI 编码的灵活性。

    【讨论】:

      猜你喜欢
      • 2012-06-26
      • 2023-03-11
      • 1970-01-01
      • 1970-01-01
      • 2011-07-23
      • 1970-01-01
      • 2012-06-06
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多