同样,为什么不使用JOptionPane?许多人误解了这些有用的结构,以为只有在最真实的情况下才可以将它们用于最简单的GUI。使用这些方法以获得最大功效的关键是要了解大多数JOptionPane方法中的第二个参数是
Object,并且此Object可能是非常复杂的,甚至是一个大型的JPanel,它包含其他组件,包括其他JPanels,JTables,JComboBoxes等。我已经使用它向用户展示了复杂的模态输入对话框,就像您要尝试的那样。然后,在处理完JOptionPane并返回程序后,您将查询JOptionPane中显示的复杂GUI的属性并提取其信息。同样,请在此处查看我的链接,确切地了解我的意思。
例如,根据您的情况,如果您想要一个包含3个JTextField的JPanel来获取您的b-
数字,名字和姓氏信息,只需创建一个包含JTextFields及其对应的JLabel的JPanel:
import java.awt.GridBagConstraints;import java.awt.GridBagLayout;import java.awt.Insets;import java.util.HashMap;import java.util.Map;import javax.swing.BorderFactory;import javax.swing.JLabel;import javax.swing.JPanel;import javax.swing.JTextField;class PlayerEditorPanel extends JPanel { enum FieldTitle { B_NUMBER("B Number"), FIRST_NAME("First Name"), LAST_NAME("Last Name"); private String title; private FieldTitle(String title) { this.title = title; } public String getTitle() { return title; } }; private static final Insets WEST_INSETS = new Insets(5, 0, 5, 5); private static final Insets EAST_INSETS = new Insets(5, 5, 5, 0); private Map<FieldTitle, JTextField> fieldMap = new HashMap<FieldTitle, JTextField>(); public PlayerEditorPanel() { setLayout(new GridBagLayout()); setBorder(BorderFactory.createCompoundBorder( BorderFactory.createTitledBorder("Player Editor"), BorderFactory.createEmptyBorder(5, 5, 5, 5))); GridBagConstraints gbc; for (int i = 0; i < FieldTitle.values().length; i++) { FieldTitle fieldTitle = FieldTitle.values()[i]; gbc = createGbc(0, i); add(new JLabel(fieldTitle.getTitle() + ":", JLabel.LEFT), gbc); gbc = createGbc(1, i); JTextField textField = new JTextField(10); add(textField, gbc); fieldMap.put(fieldTitle, textField); } } private GridBagConstraints createGbc(int x, int y) { GridBagConstraints gbc = new GridBagConstraints(); gbc.gridx = x; gbc.gridy = y; gbc.gridwidth = 1; gbc.gridheight = 1; gbc.anchor = (x == 0) ? GridBagConstraints.WEST : GridBagConstraints.EAST; gbc.fill = (x == 0) ? GridBagConstraints.BOTH : GridBagConstraints.HORIZONTAL; gbc.insets = (x == 0) ? WEST_INSETS : EAST_INSETS; gbc.weightx = (x == 0) ? 0.1 : 1.0; gbc.weighty = 1.0; return gbc; } public String getFieldText(FieldTitle fieldTitle) { return fieldMap.get(fieldTitle).getText(); }}然后像这样在JOptionPane中显示它:
PlayerEditorPanel playerEditorPanel = new PlayerEditorPanel();int result = JOptionPane.showConfirmDialog(null, playerEditorPanel, "Edit Player JOptionPane", JOptionPane.OK_CANCEL_OPTION, JOptionPane.PLAIN_MESSAGE);
然后从JPanel中提取必要的信息:
if (result == JOptionPane.OK_OPTION) {for (PlayerEditorPanel.FieldTitle fieldTitle : PlayerEditorPanel.FieldTitle .values()) { textarea.append(String.format("%10s: %s%n", fieldTitle.getTitle(), playerEditorPanel.getFieldText(fieldTitle)));} }主类可能如下所示:
import java.awt.*;import java.awt.event.*;import java.util.HashMap;import java.util.Map;import javax.swing.*;@SuppressWarnings("serial")public class ComplexOptionPane extends JPanel { private PlayerEditorPanel playerEditorPanel = new PlayerEditorPanel(); private Jtextarea textarea = new Jtextarea(12, 30); public ComplexOptionPane() { textarea.setEditable(false); textarea.setFocusable(false); textarea.setFont(new Font(Font.MONOSPACED, Font.PLAIN, 16)); JPanel bottomPanel = new JPanel(); bottomPanel.add(new JButton(new AbstractAction("Get Player Information") { @Override public void actionPerformed(ActionEvent arg0) { int result = JOptionPane.showConfirmDialog(null, playerEditorPanel, "Edit Player JOptionPane", JOptionPane.OK_CANCEL_OPTION, JOptionPane.PLAIN_MESSAGE); if (result == JOptionPane.OK_OPTION) { for (PlayerEditorPanel.FieldTitle fieldTitle : PlayerEditorPanel.FieldTitle .values()) { textarea.append(String.format("%10s: %s%n", fieldTitle.getTitle(), playerEditorPanel.getFieldText(fieldTitle))); } } } })); setBorder(BorderFactory.createEmptyBorder(5, 5, 5, 5)); setLayout(new BorderLayout(5, 5)); add(new JScrollPane(textarea), BorderLayout.CENTER); add(bottomPanel, BorderLayout.PAGE_END); } private static void createAndShowGui() { ComplexOptionPane mainPanel = new ComplexOptionPane(); Jframe frame = new Jframe("ComplexOptionPane"); frame.setDefaultCloseOperation(Jframe.EXIT_ON_CLOSE); frame.getContentPane().add(mainPanel); frame.pack(); frame.setLocationByPlatform(true); frame.setVisible(true); } public static void main(String[] args) { SwingUtilities.invokeLater(new Runnable() { public void run() { createAndShowGui(); } }); }}


