繁体   English   中英

Java中同一GridBagLayout中的JTextField和JComboBox问题

[英]Issue with JTextField and JComboBox in same GridBagLayout in Java

我试图使用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);
    }
}

意见建议:

  • 摆脱setSize(300,200); 并将其替换为pack(); 通过设置大小,您可以人为地将GUI限制为可能不是最佳自然大小的某个大小。 在将所有组件添加到GUI之后,通过调用pack() ,允许每个组件将自身的大小调整为自己计算出的最佳大小。
  • 另外,更改new JTextField(); new JTextField(col_width); 其中col_width是一个整数,它是您希望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类将面向创建JPanels,然后将其放置到JFrames或JDialogs或JTabbedPanes中,或者在需要时通过CardLayouts进行交换。 这将大大增加GUI编码的灵活性。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM