我正在尝试将 TextBox 和 Button 水平放置在 JOptionPane.showOptionDialog 中。 我用过这个代码。
JTextField txt = new JTextField();
JButton btn = new JButton("Button");
int value = JOptionPane.showOptionDialog(this,
new Object[]{txt, btn},
"Hello World",
JOptionPane.OK_CANCEL_OPTION,
JOptionPane.INFORMATION_MESSAGE,
null, null, null);
但是文本框和按钮垂直显示。 我怎样才能水平显示它们? 请帮忙... 谢谢。
最佳答案
来自here您实际上可以这样做,设置一个具有文本字段和按钮的 JPanel。
int value = JOptionPane.showOptionDialog(this,
getPanel(),
"Hello World",
JOptionPane.OK_CANCEL_OPTION,
JOptionPane.INFORMATION_MESSAGE,
null, null, null);
private JPanel getPanel() {
JPanel panel = new JPanel();
JTextField txt = new JTextField(20);
JButton btn = new JButton("Button");
panel.add(txt);
panel.add(btn);
return panel;
}
编辑 每个 JPanel 对象都被初始化为使用 FlowLayout,除非您在创建 JPanel 时指定不同。根据文档here .
关于java - 如何水平显示 JOptionPane.showOptionDialog 中的项目?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18461830/