Java swing:格式化对话框中的内容
Java swing: format content inside dialog
我在尝试格式化对话框内的 JComponent 位置时遇到了一些问题。我觉得这个问题已经有了答案,但我在查找信息时遇到了一些问题。
我试过在元素上使用 setLocation,使用自定义 JOptionPane/JDialog(但不确定我是否正确执行了这些操作),但它们总是忽略位置的格式。所有元素都排成一条水平线
理想情况下,我希望 class 名称位于顶部,3 个成员列表位于中间,按钮位于底部。
我正在这样创建对话框:
JPanel createClass = new NewClass();
int result = JOptionPane.showConfirmDialog(GUI.this,
createClass, "Create a class",JOptionPane.OK_CANCEL_OPTION);
和 NewClass()(扩展 JPanel)看起来像这样:
//initializes the values
className = new JTextField(10);
superName = new JTextField(10);
publicMem = new DefaultListModel<String>();
protectedMem = new DefaultListModel<String>();
privateMem = new DefaultListModel<String>();
publicMem.addElement("Test");
publicMem.addElement("Test");
pubMemList = new JList(publicMem);
protMemList = new JList(protectedMem);
privMemList = new JList(privateMem);
pubMemList.setLocation(0,0);
newMember = new ButtonController(memberCommand);
//add Components to the JPanel
add(new JLabel("Class Name:"));
add(className);
add(Box.createHorizontalStrut(15));
add(new JLabel("Super Class Name:"));
add(superName);
add(new JLabel("\n"));
add(new JLabel("Public Members :"));
add(pubMemList);
add(new JLabel("Protected Members :"));
add(protMemList);
add(new JLabel("Private Members :"));
add(privMemList);
add(newMember);
因此,如果有人能指出我正确的方向或另一个类似的 post,我们将不胜感激。
"Ideally I would like the class names at the top, the 3 member lists in the middle and the button on the bottom"
您可以使用 BorderLayout
来实现它。以下代码段可能会帮助您入门:
setLayout(new BorderLayout());
//now add 3 panel as containers for top, center and bottom content
JPanel top = new JPanel();
add(top, BorderLayout.NORTH);
JPanel center = new JPanel();
add(center, BorderLayout.CENTER);
JPanel bottom = new JPanel();
add(bottom, BorderLayout.SOUTH);
//manage layout and add content to top container
top.setLayout(new FlowLayout());//actually it the default
top.add(new JLabel("Class Name:"));
JTextField className = new JTextField(10);
top.add(className);
top.add(new JLabel("Super Class Name:"));
JTextField superName = new JTextField(10);
top.add(superName);
//todo manage layout and add content to center container
//todo manage layout and add content to bottom container
我在尝试格式化对话框内的 JComponent 位置时遇到了一些问题。我觉得这个问题已经有了答案,但我在查找信息时遇到了一些问题。
我试过在元素上使用 setLocation,使用自定义 JOptionPane/JDialog(但不确定我是否正确执行了这些操作),但它们总是忽略位置的格式。所有元素都排成一条水平线
理想情况下,我希望 class 名称位于顶部,3 个成员列表位于中间,按钮位于底部。 我正在这样创建对话框:
JPanel createClass = new NewClass();
int result = JOptionPane.showConfirmDialog(GUI.this,
createClass, "Create a class",JOptionPane.OK_CANCEL_OPTION);
和 NewClass()(扩展 JPanel)看起来像这样:
//initializes the values
className = new JTextField(10);
superName = new JTextField(10);
publicMem = new DefaultListModel<String>();
protectedMem = new DefaultListModel<String>();
privateMem = new DefaultListModel<String>();
publicMem.addElement("Test");
publicMem.addElement("Test");
pubMemList = new JList(publicMem);
protMemList = new JList(protectedMem);
privMemList = new JList(privateMem);
pubMemList.setLocation(0,0);
newMember = new ButtonController(memberCommand);
//add Components to the JPanel
add(new JLabel("Class Name:"));
add(className);
add(Box.createHorizontalStrut(15));
add(new JLabel("Super Class Name:"));
add(superName);
add(new JLabel("\n"));
add(new JLabel("Public Members :"));
add(pubMemList);
add(new JLabel("Protected Members :"));
add(protMemList);
add(new JLabel("Private Members :"));
add(privMemList);
add(newMember);
因此,如果有人能指出我正确的方向或另一个类似的 post,我们将不胜感激。
"Ideally I would like the class names at the top, the 3 member lists in the middle and the button on the bottom"
您可以使用 BorderLayout
来实现它。以下代码段可能会帮助您入门:
setLayout(new BorderLayout());
//now add 3 panel as containers for top, center and bottom content
JPanel top = new JPanel();
add(top, BorderLayout.NORTH);
JPanel center = new JPanel();
add(center, BorderLayout.CENTER);
JPanel bottom = new JPanel();
add(bottom, BorderLayout.SOUTH);
//manage layout and add content to top container
top.setLayout(new FlowLayout());//actually it the default
top.add(new JLabel("Class Name:"));
JTextField className = new JTextField(10);
top.add(className);
top.add(new JLabel("Super Class Name:"));
JTextField superName = new JTextField(10);
top.add(superName);
//todo manage layout and add content to center container
//todo manage layout and add content to bottom container