Java中的间距标签和按钮

时间:2011-06-28 08:33:14

标签: java swing user-interface layout gridbaglayout

我仍在刷新旧的Java GUI并遇到了一些问题。这只是整个GUI的东西仍然很新鲜,我只使用了FlowLayout(),我猜我正在寻找的东西是不能用它完成的。这不是作业或任何东西,只是我正在做的事情。无论如何,我的问题:

基本上,我希望它看起来像这样

Welcome!
Today's Date is: 
(space)
(space)
Exit button

我的问题是我不了解任何布局来完成这项工作。我一直在阅读和弄乱GridBagLayout,我无法做任何事情而且我尝试了另一种方式,按钮和dang程序一样大。无论如何,这里是我的代码,即使它不应该真的重要。

private void welcomeTab(){
    welcomePanel = new JPanel(new FlowLayout());
    String currentTime = SimpleDateFormat.getInstance().format(
    Calendar.getInstance().getTime());
    final JLabel welcomeLabel = new JLabel("Welcome!", JLabel.CENTER);
    final JLabel dateLabel = new JLabel ("Today's date is: " + currentTime, JLabel.CENTER);
    welcomePanel.add(welcomeLabel);
    welcomePanel.add(dateLabel);
    welcomePanel.add(createExitButton());
}

谢谢。我一直在读这么多,似乎所有的例子都是用所有按钮创建窗格,这让我疯了。

4 个答案:

答案 0 :(得分:6)

这样的东西?

Welcome Panel

import java.awt.*;
import javax.swing.*;
import javax.swing.border.*;
import java.util.Calendar;
import java.text.SimpleDateFormat;

class WelcomeLayout {

    private JPanel welcomePanel;

    WelcomeLayout() {
        welcomeTab();
        welcomePanel.setBorder(new TitledBorder("The Welcome Panel"));
        JOptionPane.showMessageDialog(null, welcomePanel);
    }

    private void welcomeTab() {
        welcomePanel = new JPanel(new GridLayout(0,1,1,1));
        String currentTime = SimpleDateFormat.getInstance().format(
        Calendar.getInstance().getTime());
        final JLabel welcomeLabel = new JLabel("Welcome!", JLabel.CENTER);
        final JLabel dateLabel = new JLabel ("Today's date is: " + currentTime, JLabel.CENTER);
        welcomePanel.add(welcomeLabel);
        welcomePanel.add(dateLabel);

        // one (kludgy) way to addd space.
        welcomePanel.add(new JLabel(""));
        welcomePanel.add(new JLabel(""));

        welcomePanel.add( createExitButton() );
    }

    private JComponent createExitButton() {
        JButton exit = new JButton("Exit");
        // the FlowLayout is to center the JButton;
        JPanel exitPanel = new JPanel(new FlowLayout(FlowLayout.CENTER));
        exitPanel.add(exit);
        return exitPanel;
    }

    public static void main(String[] args) {
        SwingUtilities.invokeLater(new Runnable() {
            public void run() {
                WelcomeLayout wl = new WelcomeLayout();
            }
        });
    }
}

使用Talha Ahmed Khan /Zéychin建议的BoxLayout

enter image description here

import java.awt.*;
import javax.swing.*;
import javax.swing.border.*;
import java.util.Calendar;
import java.text.SimpleDateFormat;

class WelcomeBoxLayout {

    private JPanel welcomePanel;

    WelcomeBoxLayout() {
        welcomeTab();
        welcomePanel.setBorder(new TitledBorder("The Welcome Panel"));
        JOptionPane.showMessageDialog(null, welcomePanel);
    }

    private void welcomeTab() {
        welcomePanel = new JPanel();
        BoxLayout layout = new BoxLayout(welcomePanel, BoxLayout.Y_AXIS);
        welcomePanel.setLayout(layout);
        String currentTime = SimpleDateFormat.getInstance().format(
        Calendar.getInstance().getTime());
        final JLabel welcomeLabel = new JLabel("Welcome!", JLabel.CENTER);
        final JLabel dateLabel = new JLabel ("Today's date is: " + currentTime, JLabel.CENTER);
        welcomePanel.add(welcomeLabel);
        welcomePanel.add(dateLabel);

        welcomePanel.add( Box.createVerticalStrut(20) );

        welcomePanel.add( new JButton("Exit") );
    }

    public static void main(String[] args) {
        SwingUtilities.invokeLater(new Runnable() {
            public void run() {
                WelcomeBoxLayout wl = new WelcomeBoxLayout();
            }
        });
    }
}

答案 1 :(得分:4)

尝试添加Box.createHorizontalStrut(i_width)

welcomePanel.add(welcomeLabel);
welcomePanel.add( Box.createHorizontalStrut(10) );
welcomePanel.add(dateLabel);
welcomePanel.add( Box.createHorizontalStrut(10) );
welcomePanel.add(createExitButton());

答案 2 :(得分:2)

看起来您想要使用垂直BoxLayout。我不确定Talha Ahmed Khan的想法, 因为水平支柱强制执行两个元素之间的水平空间量。

此链接应该有所帮助: http://download.oracle.com/javase/tutorial/uiswing/layout/box.html

这里是该页面上第一个示例源的直接链接: http://download.oracle.com/javase/tutorial/uiswing/examples/layout/BoxLayoutDemoProject/src/layout/BoxLayoutDemo.java

答案 3 :(得分:1)

GridBagLayoutNetbeans 7.0保持最佳状态。检查一下,你不会后悔。

<强>建议:

使用 Netbeans GridBagLayout Designer 排除问题,然后阅读生成的代码以了解修复。

<强>声明:

编写自定义代码非常繁琐。你需要熟悉它。它提供了在大多数地方添加自定义代码的钩子。但我仍觉得它非常麻烦。你需要自己对它进行排序。