GridBagLayout未按预期显示

时间:2013-05-08 14:56:23

标签: java swing layout gridbaglayout

这是我第一次涉足GridBagLayout,已经在线查看了Java文档(以及很多SO Q& As),并且我创建了一个我期望显示的面板:

+--------------------------+
|      choose a timer      |
+--------+--------+--------+
|  5min  |  25min | 30min  |
+--------+--------+--------+
|         00:00:00         |
+--------+--------+--------+
|  Start |  Pause |  Quit  |
+--------+--------+--------+

这是我正在使用的GridBagLayout。

public class ButtonSel extends JFrame implements ActionListener {
    JLabel buttonSelLabel = new JLabel("Choose Which Timer To Run");
    JButton pomoButton    = new JButton("00:25:00");
    JButton shrtButton    = new JButton("00:05:00");
    JButton longButton    = new JButton("00:30:00");
    JButton startButton   = new JButton("Go");
    JButton pauseButton   = new JButton("Pause");
    JButton quitButton    = new JButton("Quit");
    JLabel textDisplay    = new JLabel("00:00:00");
     //
    JPanel timerPanel     = new JPanel();
     //

public ButtonSel() {
    super("ButtonTest");
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    setLayout(new GridBagLayout());
    GridBagConstraints c = new GridBagConstraints();

            add(buttonSelLabel, c);    // line 1
             c.fill = GridBagConstraints.HORIZONTAL;
             c.gridx = 1;
             c.gridy = 1;
             c.gridwidth = 3;

            add(pomoButton, c);        // line 2
             c.gridx = 1;
             c.gridy = 2;
             c.gridwidth = 1;

            add(shrtButton, c);
             c.gridx = 2;
             c.gridy = 2;
             c.gridwidth = 1;

            add(longButton, c);
             c.gridx = 3;
             c.gridy = 2;
             c.gridwidth = 1;

            add(textDisplay, c);       // line 3
             c.fill = GridBagConstraints.HORIZONTAL;
             c.gridx=1;
             c.gridy=3;
             c.gridwidth=3;

            add(startButton, c);       // line 4
             c.gridx = 1;
             c.gridy = 4;
             c.gridwidth = 1;

            add(pauseButton, c);
             c.gridx = 2;
             c.gridy = 4;
             c.gridwidth = 1;

            add(quitButton, c);
             c.gridx = 2;
             c.gridy = 4;
             c.gridwidth = 1;

    pomoButton.addActionListener(this);
    shrtButton.addActionListener(this);
    longButton.addActionListener(this);
    startButton.addActionListener(this);
    pauseButton.addActionListener(this);
    quitButton.addActionListener(this);
}
public void actionPerformed(ActionEvent radioSelect) {
    Object source = radioSelect.getSource();
    if (source == pomoButton)
        textDisplay.setText("00:25:00");
    else
    if (source == shrtButton)
        textDisplay.setText("00:05:00");
    else
    if (source == longButton)
        textDisplay.setText("00:30:00");
    else
    if (source == startButton)
        textDisplay.setText("Started");
    else
    if (source == pauseButton)
        textDisplay.setText("Paused");
    else
    if (source == quitButton)
        textDisplay.setText("Quit");
    else
        textDisplay.setText("00:00:00");
}
}

我得到了这个输出,

enter image description here

并从this question获取关于水平对齐的提示我将HORIZONTAL约束添加到字段中。现在我得到了:

enter image description here

我使用(0,0)(1,1)作为我的起始(x,y)坐标,有趣的是,结果相同。

有人能看到我失踪的东西吗?

2 个答案:

答案 0 :(得分:4)

我必须添加几行才能使您的代码真正可以运行。

在将GridBagConstraints添加到组件后,您正在设置它。您必须在添加之前设置它们。

诀窍是设置你想要居中的JLabel的setHorizo​​ntalAlignment方法到中心对齐。

我格式化了您的代码并添加了一些Insets,以使您的GUI更具视觉吸引力。

这是GUI的图片。

Button Test GUI

这是代码。

import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.SwingConstants;
import javax.swing.SwingUtilities;

public class ButtonSel extends JFrame implements ActionListener {
    JLabel  buttonSelLabel  = new JLabel("Choose Which Timer To Run");
    JButton pomoButton      = new JButton("00:25:00");
    JButton shrtButton      = new JButton("00:05:00");
    JButton longButton      = new JButton("00:30:00");
    JButton startButton     = new JButton("Go");
    JButton pauseButton     = new JButton("Pause");
    JButton quitButton      = new JButton("Quit");
    JLabel  textDisplay     = new JLabel("00:00:00");
    JPanel  timerPanel      = new JPanel();


    public ButtonSel() {
        super("ButtonTest");
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        timerPanel = new JPanel();
        timerPanel.setLayout(new GridBagLayout());
        GridBagConstraints c = new GridBagConstraints();

        c.fill = GridBagConstraints.HORIZONTAL;
        c.anchor = GridBagConstraints.CENTER;
        c.weightx = 1.0D;
        c.weighty = 1.0D;
        c.gridx = 1;
        c.gridy = 1;
        c.gridwidth = 3;
        c.gridheight = 1;
        c.insets = new Insets(10, 10, 0, 0);
        c.ipadx = 0;
        c.ipady = 0;
        buttonSelLabel.setHorizontalAlignment(SwingConstants.CENTER);
        timerPanel.add(buttonSelLabel, c); // line 1

        c.gridx = 1;
        c.gridy = 2;
        c.gridwidth = 1;
        timerPanel.add(pomoButton, c); // line 2

        c.gridx = 2;
        c.gridy = 2;
        c.gridwidth = 1;
        timerPanel.add(shrtButton, c);

        c.gridx = 3;
        c.gridy = 2;
        c.gridwidth = 1;
        c.insets = new Insets(10, 10, 0, 10);
        timerPanel.add(longButton, c);

        c.fill = GridBagConstraints.HORIZONTAL;
        c.gridx = 1;
        c.gridy = 3;
        c.gridwidth = 3;
        c.insets = new Insets(10, 10, 0, 0);
        textDisplay.setHorizontalAlignment(SwingConstants.CENTER);
        timerPanel.add(textDisplay, c); // line 3

        c.gridx = 1;
        c.gridy = 4;
        c.gridwidth = 1;
        c.insets = new Insets(10, 10, 10, 0);
        timerPanel.add(startButton, c); // line 4

        c.gridx = 2;
        c.gridy = 4;
        c.gridwidth = 1;
        timerPanel.add(pauseButton, c);

        c.gridx = 3;
        c.gridy = 4;
        c.gridwidth = 1;
        c.insets = new Insets(10, 10, 10, 10);
        timerPanel.add(quitButton, c);

        pomoButton.addActionListener(this);
        shrtButton.addActionListener(this);
        longButton.addActionListener(this);
        startButton.addActionListener(this);
        pauseButton.addActionListener(this);
        quitButton.addActionListener(this);

        this.add(timerPanel);
        this.pack();
        this.setVisible(true);
    }

    public void actionPerformed(ActionEvent radioSelect) {
        Object source = radioSelect.getSource();
        if (source == pomoButton)
            textDisplay.setText("00:25:00");
        else if (source == shrtButton)
            textDisplay.setText("00:05:00");
        else if (source == longButton)
            textDisplay.setText("00:30:00");
        else if (source == startButton)
            textDisplay.setText("Started");
        else if (source == pauseButton)
            textDisplay.setText("Paused");
        else if (source == quitButton)
            textDisplay.setText("Quit");
        else
            textDisplay.setText("00:00:00");
    }

    public static void main(String[] args) {
        SwingUtilities.invokeLater(new Runnable() {
            @Override
            public void run() {
                new ButtonSel();            
            }
        });
    }

}

答案 1 :(得分:0)

我认为一个解决方案是BoxLayout()。你可以将你的面板划分为4个独立的面板。

1 - 选择时间 - 作为一个小组 2 - 5,25,30分钟 3点 - 00:00:00 4 - 开始暂停退出

然后准备主面板,添加4个面板并使用BoxLayout()/此面板的布局很重要: e.g。

JPanel p = new JPanel();
p.add(panel1);
p.add(panel2);
p.add(panel3);
p.add(panel4);
p.setLayout(new BoxLayout(p, BoxLayout.Y_AXIS)); 

BoxLayout.Y_AXIS - 小组将从上到下进行布局