【问题标题】:Spacing Labels and Buttons in JavaJava中的间距标签和按钮
【发布时间】:2023-03-19 20:35:01
【问题描述】:

我仍然在重温旧的 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());
}

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

【问题讨论】:

  • 您可以使用绝对布局并将组件放置在您想要的任何位置。 download.oracle.com/javase/tutorial/uiswing/layout/none.html
  • @Bartzilla: ".. 将组件放置在您想要的任何位置。" -1 真正的诀窍是将它们放置在需要的地方成为。一旦你弄清楚这背后的逻辑,你不妨在自定义布局管理器中表达它。

标签: java swing user-interface layout gridbaglayout


【解决方案1】:

这样的?

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

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

【讨论】:

【解决方案2】:

尝试添加Box.createHorizontalStrut(i_width)

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

【讨论】:

    【解决方案3】:

    看起来您想使用垂直 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

    【讨论】:

      【解决方案4】:

      GridBagLayoutNetbeans 7.0 处于最佳状态。看看,你不会后悔的。

      建议:

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

      免责声明:

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

      【讨论】:

      • "..您需要自行排序。" 当然,最好在没有强大的自动 IDE 的“帮助”的情况下完成。在您了解布局之前,将 IDE 用于布局问题并不是解决方案。
      • @Andrew:通过工具对其进行排序,然后阅读生成的代码......另一种方式。我自己讨厌魔法,不想在不知道的情况下做事。不过我不介意你的否定。
      • 如果您将“仇恨魔法”这句话重新运用到您的答案中(以某种形式),我会提议撤销否决票。该理论的问题在于,不是我对您的回复投了反对票。我觉得一个评论就够了。和平。
      • @Andrew:尽力了。感谢您的建议。
      • +1 这是 NetBeans GUI 编辑器的优点之一:您可以尝试不熟悉的布局。 IMO,GridBagLayout 的自定义布局控件使其非常更易于理解。
      猜你喜欢
      • 2017-11-26
      • 2017-04-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-08-03
      • 2012-11-26
      • 1970-01-01
      • 2021-05-13
      相关资源
      最近更新 更多