Java 如何设置&;管理JOptionPane的布局
我有一个带有一些组件的简单JOptionPane,我想对它们进行排列 例如,我想把标签放在文本/密码字段旁边,设置它们的宽度等 这就像我是怎么做的,当然结果不是我想要的:Java 如何设置&;管理JOptionPane的布局,java,swing,layout-manager,joptionpane,Java,Swing,Layout Manager,Joptionpane,我有一个带有一些组件的简单JOptionPane,我想对它们进行排列 例如,我想把标签放在文本/密码字段旁边,设置它们的宽度等 这就像我是怎么做的,当然结果不是我想要的: public class CreateApplicationUserScreen { CreateApplicationUserScreen(){ JLabel l_name=new JLabel("Username"); JLabel l_pass=new JLabel("Password");
public class CreateApplicationUserScreen {
CreateApplicationUserScreen(){
JLabel l_name=new JLabel("Username");
JLabel l_pass=new JLabel("Password");
JTextField tf_name=new JTextField(10);
JPasswordField pf_pass=new JPasswordField(10);
JButton b_privs = new JButton("Privs");
JButton b_databases = new JButton("Databases");
int res = JOptionPane.showOptionDialog(window,
new Object[] { l_name, tf_name,
l_pass, pf_pass,
b_privs, b_databases },
"Create application user",
JOptionPane.OK_CANCEL_OPTION,
JOptionPane.PLAIN_MESSAGE,null,
new String[]{"Create", "Cancel"},
"default");
}
}
使用API中实现的JOptionPane
BoxLayout
- 您可以设置任何
,布局管理器
,字体
,等等图标
- 如果更改了LayoutManager,则需要调用JOptionPane.pack()
import java.awt.BorderLayout;
import java.awt.GridLayout;
import javax.swing.BorderFactory;
import javax.swing.JComboBox;
import javax.swing.JComponent;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
public class OptionPaneOptions {
private JPanel options;
private Object[] items;
public OptionPaneOptions() {
options = new JPanel(new GridLayout(0, 3));
options.add(createOptionPane("Plain Message", JOptionPane.PLAIN_MESSAGE));
options.add(createOptionPane("Error Message", JOptionPane.ERROR_MESSAGE));
options.add(createOptionPane("Information Message", JOptionPane.INFORMATION_MESSAGE));
options.add(createOptionPane("Warning Message", JOptionPane.WARNING_MESSAGE));
options.add(createOptionPane("Want to do something?", JOptionPane.QUESTION_MESSAGE));
items = new Object[]{"First", "Second", "Third"};
JComboBox choiceCombo = new JComboBox(items);
options.add(titled(new JOptionPane(choiceCombo,
JOptionPane.QUESTION_MESSAGE, JOptionPane.OK_CANCEL_OPTION), "Question Message"));
JFrame frame = new JFrame("JOptionPane'Options");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.add(options, BorderLayout.CENTER);
frame.pack();
frame.setVisible(true);
}
static JOptionPane createOptionPane(String message, int type) {
JOptionPane pane = new JOptionPane(message, type);
System.out.println(pane.getLayout());
String title = message;
if (type == JOptionPane.QUESTION_MESSAGE) {
title = "Question Message";
pane.setOptionType(JOptionPane.YES_NO_CANCEL_OPTION);
}
return titled(pane, title);
}
static <T extends JComponent> T titled(T c, String title) {
c.setBorder(BorderFactory.createTitledBorder(title));
return c;
}
public static void main(String[] args) {
OptionPaneOptions test = new OptionPaneOptions();
}
}
导入java.awt.BorderLayout;
导入java.awt.GridLayout;
导入javax.swing.BorderFactory;
导入javax.swing.JComboBox;
导入javax.swing.JComponent;
导入javax.swing.JFrame;
导入javax.swing.JOptionPane;
导入javax.swing.JPanel;
公共类选项PaneOptions{
私人JPanel期权;
私有对象[]项;
公共选项paneoptions(){
选项=新的JPanel(新的GridLayout(0,3));
添加(createOptionPane(“普通消息”,JOptionPane.Plain_消息));
添加(createOptionPane(“错误消息”,JOptionPane.Error_消息));
添加(createOptionPane(“信息消息”,JOptionPane.Information_Message));
添加(createOptionPane(“警告消息”,JOptionPane.Warning_消息));
添加(createOptionPane(“想做点什么吗?”,JOptionPane.QUESTION_消息));
items=新对象[]{“第一”、“第二”、“第三”};
JComboBox choiceCombo=新JComboBox(项目);
选项。添加(标题为(新作业窗格)(choiceCombo,
JOptionPane.QUESTION_消息,JOptionPane.OK_取消选项),“问题消息”);
JFrame=新JFrame(“JOptionPane”选项);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.add(选项,BorderLayout.CENTER);
frame.pack();
frame.setVisible(true);
}
静态JOptionPane createOptionPane(字符串消息,int类型){
JOptionPane=新建JOptionPane(消息,类型);
System.out.println(pane.getLayout());
字符串标题=消息;
if(type==JOptionPane.QUESTION\u消息){
title=“问题消息”;
pane.setOptionType(JOptionPane.YES\u NO\u CANCEL\u选项);
}
返回标题(窗格,标题);
}
静态T标题(T c,字符串标题){
c、 setboorder(BorderFactory.createTitledBorder(title));
返回c;
}
公共静态void main(字符串[]args){
OptionPaneOptions测试=新建OptionPaneOptions();
}
}
您可以将任何Swing组件添加到选项窗格中。因此,构建您自己的面板:
import java.awt.*;
import javax.swing.*;
public class OptionPanePanel
{
private static void createAndShowUI()
{
JFrame frame = new JFrame();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setLocationRelativeTo( null );
frame.setVisible( true );
// Build a custom panel
JPanel panel = new JPanel( new GridLayout(2, 2) );
panel.add( new JLabel("First Name") );
JTextField firstName = new JTextField(10);
// firstName.addAncestorListener( new RequestFocusListener(false) );
panel.add( firstName );
panel.add( new JLabel("Last Name") );
JTextField lastName = new JTextField(10);
panel.add( lastName );
int result = JOptionPane.showConfirmDialog(
frame, // use your JFrame here
panel,
"Use a Panel",
JOptionPane.YES_NO_OPTION,
JOptionPane.PLAIN_MESSAGE);
if(result == JOptionPane.YES_OPTION)
{
System.out.println(firstName.getText() + " : " + lastName.getText());
}
else
{
System.out.println("Canceled");
}
// Let Option Pane build the panel for you
Object[] msg = {"First Name:", firstName, "Last Name:", lastName};
result = JOptionPane.showConfirmDialog(
frame,
msg,
"Use default layout",
JOptionPane.OK_CANCEL_OPTION,
JOptionPane.PLAIN_MESSAGE);
if (result == JOptionPane.YES_OPTION)
{
System.out.println(firstName.getText() + " : " + lastName.getText());
}
else
{
System.out.println("Canceled");
}
}
public static void main(String[] args)
{
EventQueue.invokeLater(new Runnable()
{
public void run()
{
createAndShowUI();
}
});
}
}
这种方法的一个缺点是,焦点将集中在按钮上,而不是文本字段。因此,您可能希望使用将焦点放在您的第一个文本字段上。我不理解您的示例,因此我标记了#camickr答案。