2

我还在复习旧的 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

4 回答 4

6

像这样的东西?

欢迎小组

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();
            }
        });
    }
}

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

在此处输入图像描述

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();
            }
        });
    }
}
于 2011-06-28T10:43:14.990 回答
4

尝试添加一个Box.createHorizontalStrut(i_width)

welcomePanel.add(welcomeLabel);
welcomePanel.add( Box.createHorizontalStrut(10) );
welcomePanel.add(dateLabel);
welcomePanel.add( Box.createHorizontalStrut(10) );
welcomePanel.add(createExitButton());
于 2011-06-28T08:38:50.347 回答
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

于 2011-06-28T08:50:21.100 回答
1

GridBagLayoutNetbeans 7.0处于最佳状态。检查出来,你不会后悔的。

建议:

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

免责声明:

编写自定义代码可能非常麻烦。你需要熟悉它。它在大多数地方提供了添加自定义代码的钩子。但是我还是觉得很麻烦。你需要自己排序。

于 2011-06-28T08:50:51.523 回答