2 回答
TA贡献1886条经验 获得超2个赞
事实证明,JOptionPane
已经建立了列表选择,无需使用您自己的JList
.
这是使用的调用:JOptionPane.showInputDialog
这是一个非常简单的示例:显示带有选项列表的对话框
这是一个完整的工作示例,可让您选择字体名称(使用 Andrew 在评论中提供的便捷字体列表片段)。
import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.GraphicsEnvironment;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextPane;
import javax.swing.SwingUtilities;
import javax.swing.WindowConstants;
public class ListChooserDemo extends JFrame {
JTextPane textPane = new JTextPane();
String lastChoice = null;
public ListChooserDemo() {
setTitle("List Chooser Demo");
setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
setSize(new Dimension(500, 500));
add(new JScrollPane(textPane), BorderLayout.CENTER);
JPanel buttonPanel = new JPanel(new FlowLayout());
add(buttonPanel, BorderLayout.SOUTH);
JButton b = new JButton("Choose it!");
textPane.setText("Click the button...");
b.addActionListener(this::doChooseFont);
buttonPanel.add(b);
}
public void doChooseFont(ActionEvent e) {
// a handy way to get a nontrivial list of choices for a demo
String[] choices = GraphicsEnvironment.getLocalGraphicsEnvironment().getAvailableFontFamilyNames();
// Show a list of options with no effort on our part.
String input = (String) JOptionPane.showInputDialog(
this, // optional reference to frame/window or null
"Choose a font...", // prompt displayed over list
"Font Chooser", // title
JOptionPane.QUESTION_MESSAGE, // message style
null, // Use default icon for message style
choices, // array of choices
lastChoice); // initial choice or null
if (input == null) {
// Handle case when user canceled, didn't select anything, or hit escape
textPane.setText(textPane.getText() + "\r\nCanceled!");
} else {
// Do stuff that happens when a selection was made
textPane.setText(textPane.getText() + "\r\nSelected " + input);
lastChoice = input;
}
}
public static final void main(String[] args) {
// Run in GUI thread
SwingUtilities.invokeLater(() -> {
ListChooserDemo frame = new ListChooserDemo();
// Center in screen and show
Dimension dim = Toolkit.getDefaultToolkit().getScreenSize();
frame.setLocation(dim.width / 2 - frame.getSize().width / 2, dim.height / 2 - frame.getSize().height / 2);
frame.setVisible(true);
});
}
}
TA贡献1874条经验 获得超12个赞
您可以使用JOptionPane
的功能来显示任何组件。使用
JOptionPane.showMessageDialog(frame, list);
获得显示您的JList
. 您可以通过添加更多参数来进一步自定义此对话框。
一个完整的例子:
JFrame frame = new JFrame();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
String[] options = new String[] {"a", "b", "c"};
JList<String> list = new JList<>(options);
// Shows the dialog
JOptionPane.showMessageDialog(frame, list);
// Do whatever you want with the selection, for example
frame.add(new JLabel(list.getSelectedValue()));
frame.pack();
添加回答
举报