如何创建具有多个列表选择的 JOptionPane 并提取所有选择?
How to create JOptionPane with multiple list selections and extract all the choices?
我有兴趣创建一个 JOptionPane 或任何包含多个列表选择的可交互弹出窗格。我也想提取用户所做的选择。
下面的代码显示了一个 MRE,我在其中生成了两个不同的 JOptionPanes 和列表选择,并从每个中提取选择。 本质上,我试图将两者结合起来。
import javax.swing.JFrame;
import javax.swing.JOptionPane;
public class JOptionPaneTest {
public static void main(String[] a) {
JFrame frame = new JFrame();
String bigList[] = new String[30];
String smallList[] = new String[5];
for (int i = 0; i < bigList.length; i++) {
bigList[i] = Integer.toString(i);
}
for (int i = 0; i < smallList.length; i++) {
smallList[i] = Integer.toString(i);
}
String choice = (String) JOptionPane.showInputDialog(frame, "Pick the first number", "Number 1", JOptionPane.QUESTION_MESSAGE,
null, bigList, "Titan");
String choice2 = (String) JOptionPane.showInputDialog(frame, "Pick the second number", "Number 2", JOptionPane.QUESTION_MESSAGE,
null, smallList, "Titan");
System.out.println(choice);
System.out.println(choice2);
}
}
其中一个长什么样子:
JOptionPane
其实很灵活。您可以构建一个包含任意数量组件的容器,然后使用 JOptionPane
来显示它,例如...
import java.awt.EventQueue;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import javax.swing.DefaultComboBoxModel;
import javax.swing.JComboBox;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
public class Test {
public static void main(String[] args) {
new Test();
}
public Test() {
EventQueue.invokeLater(new Runnable() {
@Override
public void run() {
String bigList[] = new String[30];
String smallList[] = new String[5];
for (int i = 0; i < bigList.length; i++) {
bigList[i] = Integer.toString(i);
}
for (int i = 0; i < smallList.length; i++) {
smallList[i] = Integer.toString(i);
}
JComboBox<String> bigListComboBox = new JComboBox<>(new DefaultComboBoxModel<String>(bigList));
JComboBox<String> smallListComboBox = new JComboBox<>(new DefaultComboBoxModel<String>(smallList));
JPanel panel = new JPanel(new GridBagLayout());
GridBagConstraints gbc = new GridBagConstraints();
gbc.gridx = 0;
gbc.gridy = 0;
gbc.anchor = GridBagConstraints.LINE_END;
gbc.insets = new Insets(4, 4, 4, 4);
panel.add(new JLabel("Pick the first number"), gbc);
gbc.gridy++;
panel.add(new JLabel("Pick the second number"), gbc);
gbc.gridx++;
gbc.gridy = 0;
gbc.anchor = GridBagConstraints.LINE_START;
panel.add(bigListComboBox, gbc);
gbc.gridy++;
panel.add(smallListComboBox, gbc);
JOptionPane.showMessageDialog(null, panel, "Pick two numbers", JOptionPane.QUESTION_MESSAGE);
System.out.println("First number = " + bigListComboBox.getSelectedItem());
System.out.println("Second number = " + smallListComboBox.getSelectedItem());
}
});
}
}
我有兴趣创建一个 JOptionPane 或任何包含多个列表选择的可交互弹出窗格。我也想提取用户所做的选择。
下面的代码显示了一个 MRE,我在其中生成了两个不同的 JOptionPanes 和列表选择,并从每个中提取选择。 本质上,我试图将两者结合起来。
import javax.swing.JFrame;
import javax.swing.JOptionPane;
public class JOptionPaneTest {
public static void main(String[] a) {
JFrame frame = new JFrame();
String bigList[] = new String[30];
String smallList[] = new String[5];
for (int i = 0; i < bigList.length; i++) {
bigList[i] = Integer.toString(i);
}
for (int i = 0; i < smallList.length; i++) {
smallList[i] = Integer.toString(i);
}
String choice = (String) JOptionPane.showInputDialog(frame, "Pick the first number", "Number 1", JOptionPane.QUESTION_MESSAGE,
null, bigList, "Titan");
String choice2 = (String) JOptionPane.showInputDialog(frame, "Pick the second number", "Number 2", JOptionPane.QUESTION_MESSAGE,
null, smallList, "Titan");
System.out.println(choice);
System.out.println(choice2);
}
}
其中一个长什么样子:
JOptionPane
其实很灵活。您可以构建一个包含任意数量组件的容器,然后使用 JOptionPane
来显示它,例如...
import java.awt.EventQueue;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import javax.swing.DefaultComboBoxModel;
import javax.swing.JComboBox;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
public class Test {
public static void main(String[] args) {
new Test();
}
public Test() {
EventQueue.invokeLater(new Runnable() {
@Override
public void run() {
String bigList[] = new String[30];
String smallList[] = new String[5];
for (int i = 0; i < bigList.length; i++) {
bigList[i] = Integer.toString(i);
}
for (int i = 0; i < smallList.length; i++) {
smallList[i] = Integer.toString(i);
}
JComboBox<String> bigListComboBox = new JComboBox<>(new DefaultComboBoxModel<String>(bigList));
JComboBox<String> smallListComboBox = new JComboBox<>(new DefaultComboBoxModel<String>(smallList));
JPanel panel = new JPanel(new GridBagLayout());
GridBagConstraints gbc = new GridBagConstraints();
gbc.gridx = 0;
gbc.gridy = 0;
gbc.anchor = GridBagConstraints.LINE_END;
gbc.insets = new Insets(4, 4, 4, 4);
panel.add(new JLabel("Pick the first number"), gbc);
gbc.gridy++;
panel.add(new JLabel("Pick the second number"), gbc);
gbc.gridx++;
gbc.gridy = 0;
gbc.anchor = GridBagConstraints.LINE_START;
panel.add(bigListComboBox, gbc);
gbc.gridy++;
panel.add(smallListComboBox, gbc);
JOptionPane.showMessageDialog(null, panel, "Pick two numbers", JOptionPane.QUESTION_MESSAGE);
System.out.println("First number = " + bigListComboBox.getSelectedItem());
System.out.println("Second number = " + smallListComboBox.getSelectedItem());
}
});
}
}