JTextField和JComboBox在Java中的相同GridBagLayout中出现问题

时间:2016-01-24 16:51:04

标签: java swing jframe jtextfield gridbaglayout

我试图使用GridBagLayout将这两个元素放到框架上,但是只要我将JComboBox添加到GridBagLayout然后将其添加到框架中,JTextFields的宽度变得非常短,我不知道为什么或者如何解决它。当我拿出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);
    }
}

1 个答案:

答案 0 :(得分:2)

建议:

  • 摆脱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,事实上你很少&#39 ;我会想要这样做。更常见的是,您的GUI类将面向创建JPanels,然后可以将其放置到JFrames或JDialogs或JTabbedPanes中,或者在需要时通过CardLayouts交换。这将大大提高GUI编码的灵活性。