亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

有沒有辦法在模式對話框中創建 JList?

有沒有辦法在模式對話框中創建 JList?

犯罪嫌疑人X 2023-06-14 16:22:39
我有一個JFrame已經可見的。用戶可以加載保存的會話。這個想法是創建一個JList,這樣用戶就可以加載所選的會話并且可以更新框架。下面的代碼獲取字符串列表并將它們添加到列表中。DefaultListModel model = new DefaultListModel();JList list=new JList(model);JScrollPane pane = new JScrollPane(list);try {    for (String  part : Utils.getSessions()) {        model.addElement(part);    }} catch (IOException e1) {    e1.printStackTrace();}下一步:顯示步驟。我發現了什么:將窗格添加到當前框架我的希望:在模態對話框中顯示列表JList有沒有辦法在模態對話框中創建一個?
查看完整描述

2 回答

?
MM們

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);

? ? });

? }

}

http://img4.sycdn.imooc.com/648978ef0001aeca05630544.jpg

查看完整回答
反對 回復 2023-06-14
?
HUWWW

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();


查看完整回答
反對 回復 2023-06-14
  • 2 回答
  • 0 關注
  • 146 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號