边框布局不起作用
我无法让 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您可以使用 BorderLayout.SOUTH 常量将 cancelButton 添加到面板:
但是您在哪里将面板的布局设置为 BorderLayout?由于您从未设置此容器的布局,因此它将使用 JPanel 的默认布局,即 FlowLayout。
解决方案:将面板 JPanel 的布局设置为 BorderLayout 以获得 BorderLayout 行为。
一旦解决了这个问题,您就会遇到另一个问题:
您将 JRadioButton 添加到同一面板 JPanel 中,而不考虑布局。我怀疑您想将 JRadioButtons 添加到自己的 JPanel,可能使用 GridLayout(1, 0) 或 GridLayout(0, 1) ,具体取决于所需的方向,然后您想要将此 JPanel 添加到面板中,可能在 BorderLayout.CENTER 位置。
此外,您的 okButton 也有类似的问题,因为您将其添加到面板而不考虑布局。
You add cancelButton to panel using the BorderLayout.SOUTH constant:
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:
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)
orGridLayout(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.
您可以尝试更改
为
结果:
You can try to change
to
Result:
正如Hovercraft Full Of Eels所说,
JPanel
默认行为是FlowLayout
,这是最简单的一个,它的描述是此处。您可以通过在构造函数中指定它来轻松地将其更改为您需要的管理器:As Hovercraft Full Of Eels says,
JPanel
default behavior is theFlowLayout
, 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: