gpt4 book ai didi

java - 带有可编辑文本字段和列表的 JOptionPane 对话框

转载 作者:行者123 更新时间:2023-11-30 05:10:11 25 4
gpt4 key购买 nike

是否可以创建一个对话框,用户可以在其中从列表中选择值,但也可以编辑文本?(类似于带有可编辑 JComboBox 的对话框。)

最佳答案

import java.awt.*;
import javax.swing.*;
import javax.swing.event.*;

class ShowBothInputs {

public static void main(String[] args) {

Runnable r = new Runnable() {

public void run() {
String[] items = {
"Apple",
"Banana",
"Grape",
"Cherry"
};

// what was requested
EditableListPanel elp = new EditableListPanel(items);
JOptionPane.showMessageDialog(null, elp);
System.out.println( "EditableListPanel value: " + elp.getValue() );

// probably what this UI actually needs
JComboBox jcb = new JComboBox(items);
jcb.setEditable(true);
JOptionPane.showMessageDialog(null, jcb);
System.out.println( "JComboBox value: " + jcb.getSelectedItem() );
}
};
SwingUtilities.invokeLater(r);
}
}

class EditableListPanel extends JPanel {

private JTextField value;

EditableListPanel(String[] items) {
super( new BorderLayout(5,5) );

final JList list = new JList( items );
list.addListSelectionListener( new ListSelectionListener(){
public void valueChanged(ListSelectionEvent lse) {
value.setText( (String)list.getSelectedValue() );
}
} );
add( list, BorderLayout.CENTER );

value = new JTextField("", 20);
add( value, BorderLayout.NORTH );
}

public String getValue() {
return value.getText();
}
}

关于java - 带有可编辑文本字段和列表的 JOptionPane 对话框,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3694789/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com