边框布局不起作用

发布于 2024-11-27 06:15:51 字数 1889 浏览 1 评论 0原文

我无法让 BorderLayout 工作。 我希望取消按钮位于底部,但它不起作用。 代码:

import java.awt.BorderLayout;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.ButtonModel;
import javax.swing.ButtonGroup;
import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JRadioButton;

class Test {
    public static JFrame owner;
    public static void main(String[] args) {
        final JDialog frame = new JDialog(owner, "Test");
        frame.setLayout(new BorderLayout());
        frame.setSize(500, 300);
        final JPanel panel = new JPanel();
        final ButtonGroup group = new ButtonGroup();
        String[] options = {"1", "2", "3"};
        for (String text : options) {
            JRadioButton option = new JRadioButton(text);
            option.setActionCommand(text);
            group.add(option);
            panel.add(option);
        }
        JButton okButton = new JButton("OK");
        okButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                ButtonModel selectedModel = group.getSelection();
                if (selectedModel != null) {
                    System.err.println(selectedModel.getActionCommand());
                }
            }
        });
        panel.add(okButton);
        JButton cancelButton = new JButton("Cancel");
        cancelButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                frame.setVisible(false);
                frame.dispose();
            }
        });
        panel.add(cancelButton, BorderLayout.SOUTH);
        frame.add(panel);
        frame.setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);
        frame.setVisible(true);
    }
}

I cannot get BorderLayout to work.
I want the cancelbutton to be positioned at the bottom, but it doesn't work.
Code:

import java.awt.BorderLayout;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.ButtonModel;
import javax.swing.ButtonGroup;
import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JRadioButton;

class Test {
    public static JFrame owner;
    public static void main(String[] args) {
        final JDialog frame = new JDialog(owner, "Test");
        frame.setLayout(new BorderLayout());
        frame.setSize(500, 300);
        final JPanel panel = new JPanel();
        final ButtonGroup group = new ButtonGroup();
        String[] options = {"1", "2", "3"};
        for (String text : options) {
            JRadioButton option = new JRadioButton(text);
            option.setActionCommand(text);
            group.add(option);
            panel.add(option);
        }
        JButton okButton = new JButton("OK");
        okButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                ButtonModel selectedModel = group.getSelection();
                if (selectedModel != null) {
                    System.err.println(selectedModel.getActionCommand());
                }
            }
        });
        panel.add(okButton);
        JButton cancelButton = new JButton("Cancel");
        cancelButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                frame.setVisible(false);
                frame.dispose();
            }
        });
        panel.add(cancelButton, BorderLayout.SOUTH);
        frame.add(panel);
        frame.setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);
        frame.setVisible(true);
    }
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

一口甜 2024-12-04 06:15:52

您可以使用 BorderLayout.SOUTH 常量将 cancelButton 添加到面板:

  panel.add(cancelButton, BorderLayout.SOUTH);

但是您在哪里将面板的布局设置为 BorderLayout?由于您从未设置此容器的布局,因此它将使用 JPanel 的默认布局,即 FlowLayout。

解决方案:将面板 JPanel 的布局设置为 BorderLayout 以获得 BorderLayout 行为。

一旦解决了这个问题,您就会遇到另一个问题:

  for (String text : options) {
     JRadioButton option = new JRadioButton(text);
     option.setActionCommand(text);
     group.add(option);
     panel.add(option);
  }

您将 JRadioButton 添加到同一面板 JPanel 中,而不考虑布局。我怀疑您想将 JRadioButtons 添加到自己的 JPanel,可能使用 GridLayout(1, 0) 或 GridLayout(0, 1) ,具体取决于所需的方向,然后您想要将此 JPanel 添加到面板中,可能在 BorderLayout.CENTER 位置。

此外,您的 okButton 也有类似的问题,因为您将其添加到面板而不考虑布局。

You add cancelButton to panel using the BorderLayout.SOUTH constant:

  panel.add(cancelButton, BorderLayout.SOUTH);

But where do you set panel's layout to be BorderLayout? Since you never set this container's layout it will use the default layout for JPanel which is FlowLayout.

Solution: set the panel JPanel's layout to BorderLayout to get BorderLayout behavior.

Once you solve this, you'll have another problem though:

  for (String text : options) {
     JRadioButton option = new JRadioButton(text);
     option.setActionCommand(text);
     group.add(option);
     panel.add(option);
  }

You're adding JRadioButton's to the same panel JPanel without regard to layout. I suspect that you want to add the JRadioButtons to their own JPanel, probably one that uses GridLayout(1, 0) or GridLayout(0, 1), depending on desired orientation, and then that you want to add this JPanel to panel, perhaps in the BorderLayout.CENTER position.

Also you have a similar problem with your okButton in that you add it to panel without regard to layout.

鹿港巷口少年归 2024-12-04 06:15:52

您可以尝试更改

panel.add(cancelButton, BorderLayout.SOUTH);

frame.add(cancelButton, BorderLayout.SOUTH);

结果:

在此处输入图像描述

You can try to change

panel.add(cancelButton, BorderLayout.SOUTH);

to

frame.add(cancelButton, BorderLayout.SOUTH);

Result:

enter image description here

〗斷ホ乔殘χμё〖 2024-12-04 06:15:52

正如Hovercraft Full Of Eels所说,JPanel默认行为是FlowLayout,这是最简单的一个,它的描述是此处。您可以通过在构造函数中指定它来轻松地将其更改为您需要的管理器:

panel = new JPanel(new BorderLayout())

As Hovercraft Full Of Eels says, JPanel default behavior is the FlowLayout, which is the simplest one, and it's described here. You can easily change it to the manager you need by specifying it inside the constructor:

panel = new JPanel(new BorderLayout())
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文