java - Java 中的标签和按钮间距

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

谢谢。我读了很多书,似乎所有示例都是用于创建带有所有按钮的 Pane ,这让我发疯。

最佳答案

类似这样的吗?

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

关于java - Java 中的标签和按钮间距,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6503722/

相关文章:

java - Hibernate - 无法解析属性

Java:在 AbsoluteLayout 中,JButton 在鼠标进入之前不会显示

java - 创建 java C :\Program Files\Java\jdk1. 7.0_05\jre\bin\client\jvm.dll 失败

java - MigLayout LC::fill() 无需调整组件大小

java - JMenuBar 未从 RSyntaxTextArea 接收按键组合

java - 如何使用 GUI 实现建模应用程序

java - 2 行 ListView 未出现

java - 如何将 JTree 动态添加到创建的 JScrollPane 中?

Java GUI,根据actionListener改变面板

Java 数据库应用程序加载大量记录时卡住