在Java中将JList选择存储在数组字符串[]中
我正在创建GUI,不知道如何将在Java中将JList选择存储在数组字符串[]中,java,arrays,swing,jlist,Java,Arrays,Swing,Jlist,我正在创建GUI,不知道如何将JList用户选择存储在数组中。我尝试了列表,对象[]等等JRadioButtons和其他GUI都很好,只有JList不工作 import java.awt.*; import java.awt.event.*; import javax.swing.*; public class Test extends JFrame { private JTextField num3; private JLabel label3; private J
JList
用户选择存储在数组中。我尝试了列表
,对象[]
等等JRadioButtons
和其他GUI都很好,只有JList
不工作
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class Test extends JFrame {
private JTextField num3;
private JLabel label3;
private JButton button;
private JRadioButton radio2;
private JRadioButton radio3;
private ButtonGroup radioGroup;
private JList statesList;
String[] states = {"Alabama", "Alaska", "Wyoming"};
String expression;
String frequency;
// no args constructor
public Test() {
createUI();
}
private void createUI() {
Container contentPane = getContentPane();
contentPane.setLayout(null);
label3 = new JLabel();
label3.setText("Search Expression");
label3.setBounds(16, 120, 200, 21);
contentPane.add(label3);
num3 = new JTextField();
num3.setText("(any expression)");
num3.setBounds(16, 144, 150, 21);
num3.setHorizontalAlignment(JTextField.LEFT);
contentPane.add(num3);
button = new JButton("Start!");
button.setBounds(90,430,126,24);
contentPane.add(button);
button.addActionListener(
new ActionListener() {
public void actionPerformed(ActionEvent event)
{
buttonActionPerformed(event);
}
}
);
// States Selection
statesList = new JList(states);
statesList.setSelectionMode(ListSelectionModel.MULTIPLE_INTERVAL_SELECTION);
statesList.setVisibleRowCount(5);
statesList.setBounds(400, 16, 100, 50);
JScrollPane statesScroll = new JScrollPane(statesList);
statesScroll.setBounds(180, 16, 135, 400);
contentPane.add(statesScroll);
// Radio Buttons
radio2 = new JRadioButton();
radio3 = new JRadioButton();
radio3.setSelected(true);
radioGroup = new ButtonGroup();
radioGroup.add(radio2);
radioGroup.add(radio3);
radio2.setText("Quarterly");
radio3.setText("Yearly");
radio2.setBounds(16,360,90,23);
radio3.setBounds(16,385,75,23);
contentPane.add(radio2);
contentPane.add(radio3);
// set the content Pane window
setTitle("Search Engine");
setSize(750,500);
setVisible(true);
}
// Getting the user's TextField and JRadioButton input
private void buttonActionPerformed(ActionEvent event) {
expression = num3.getText();
if (radio2.isSelected())
frequency = "quarterly";
else frequency = "yearly";
System.out.println(expression+","+frequency);
// The above "expression" and "frequency" work fine. But JList does not
// work. What am I doing wrong? I tried Object[] instead of List<String>...
List<String> values = statesList.getSelectedValues();
return values==null ? null : values.toArray(new String[values.size()]);
}
// main thread
public static void main(String[] args) {
Test application = new Test();
application.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
}
import java.awt.*;
导入java.awt.event.*;
导入javax.swing.*;
公共类测试扩展了JFrame{
私有JTextField num3;
私人JLabel标签3;
私人按钮;
私人jradiobuttonradio2;
私人jradiobuttonradio3;
私有按钮组;
私人集权主义政治家;
String[]states={“阿拉巴马州”、“阿拉斯加州”、“怀俄明州”};
字符串表达式;
串频;
//无参数构造函数
公开考试(){
createUI();
}
私有void createUI(){
容器contentPane=getContentPane();
contentPane.setLayout(null);
label3=新的JLabel();
标签3.setText(“搜索表达式”);
标签3.立根(16,120,200,21);
contentPane.add(label3);
num3=新的JTextField();
num3.setText(“(任何表达式)”);
立方英尺(16,144,150,21);
num3.setHorizontalAlignment(JTextField.LEFT);
contentPane.add(num3);
按钮=新的JButton(“开始!”);
按钮.立根(90430126,24);
contentPane.add(按钮);
button.addActionListener(
新建ActionListener(){
已执行的公共无效操作(操作事件)
{
按钮执行(事件);
}
}
);
//州选择
statesList=新的JLList(州);
statesList.setSelectionMode(ListSelectionModel.MULTIPLE\u INTERVAL\u SELECTION);
statesList.setVisibleRowCount(5);
州列表立根(400,16,100,50);
JScrollPane statescroll=新的JScrollPane(statesList);
州立法院(180、16、135、400);
contentPane.add(statescroll);
//单选按钮
radio2=新的JRadioButton();
radio3=新的JRadioButton();
radio3.1(正确);
radioGroup=新按钮组();
添加放射组(radio2);
添加放射组(radio3);
radio2.setText(“季度”);
radio3.setText(“年度”);
无线电2.立根(16360,90,23);
无线电3.立根(16385,75,23);
contentPane.add(radio2);
contentPane.add(radio3);
//设置内容窗格窗口
setTitle(“搜索引擎”);
设置大小(750500);
setVisible(真);
}
//获取用户的TextField和JRadioButton输入
私有无效按钮已执行(ActionEvent事件){
表达式=num3.getText();
如果(radio2.isSelected())
频率=“每季度”;
else frequency=“每年”;
System.out.println(表达式+“,”+频率);
//上面的“表达式”和“频率”可以很好地工作,但JList不能
//工作。我做错了什么?我尝试了Object[]而不是List。。。
列表值=statesList.getSelectedValues();
返回值==null?null:values.toArray(新字符串[values.size()]);
}
//主线
公共静态void main(字符串[]args){
测试应用=新测试();
application.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
}
根据,getSelectedValues返回一个对象数组
Object[] values = statesList.getSelectedValues();
如果你确定它们都是字符串,你可以直接输入cast
String[] values = (String[]) statesList.getSelectedValues();
编辑:尝试以下操作:
Object[] values = statesList.getSelectedValues();
String[] strings = new String[values.length];
for(int i = 0; i < values.length; i++) {
if(values[i] instanceof String) {
strings[i] = ((String) values[i]);
}
}
Object[]values=statesList.getSelectedValues();
String[]strings=新字符串[values.length];
对于(int i=0;i
此变量迭代getSelectedValues()
返回的对象[]
,以在控制台中显示预期值。接下来要修复的是布局
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
//public class Test extends JFrame {
public class Test {
private JTextField num3;
private JLabel label3;
private JButton button;
private JRadioButton radio2;
private JRadioButton radio3;
private ButtonGroup radioGroup;
private JList statesList;
String[] states = {"Alabama", "Alaska", "Wyoming"};
String expression;
String frequency;
// no args constructor
public Test() {
createUI();
}
private void createUI() {
JFrame f = new JFrame("Search Engine");
Container contentPane = f.getContentPane();
// This needs fixing NEXT!
contentPane.setLayout(null);
label3 = new JLabel();
label3.setText("Search Expression");
label3.setBounds(16, 120, 200, 21);
contentPane.add(label3);
num3 = new JTextField();
num3.setText("(any expression)");
num3.setBounds(16, 144, 150, 21);
num3.setHorizontalAlignment(JTextField.LEFT);
contentPane.add(num3);
button = new JButton("Start!");
button.setBounds(90,430,126,24);
contentPane.add(button);
button.addActionListener(
new ActionListener() {
public void actionPerformed(ActionEvent event) {
buttonActionPerformed(event);
}
});
// States Selection
statesList = new JList(states);
statesList.setSelectionMode(ListSelectionModel.MULTIPLE_INTERVAL_SELECTION);
statesList.setVisibleRowCount(5);
statesList.setBounds(400, 16, 100, 50);
JScrollPane statesScroll = new JScrollPane(statesList);
statesScroll.setBounds(180, 16, 135, 400);
contentPane.add(statesScroll);
// Radio Buttons
radio2 = new JRadioButton();
radio3 = new JRadioButton();
radio3.setSelected(true);
radioGroup = new ButtonGroup();
radioGroup.add(radio2);
radioGroup.add(radio3);
radio2.setText("Quarterly");
radio3.setText("Yearly");
radio2.setBounds(16,360,90,23);
radio3.setBounds(16,385,75,23);
contentPane.add(radio2);
contentPane.add(radio3);
// set the content Pane window
f.setSize(750,500);
//f.pack();
f.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
f.setLocationByPlatform(true);
f.setVisible(true);
}
// Getting the user's TextField and JRadioButton input
private void buttonActionPerformed(ActionEvent event) {
expression = num3.getText();
if (radio2.isSelected())
frequency = "quarterly";
else frequency = "yearly";
System.out.println(expression+","+frequency);
Object[] values = statesList.getSelectedValues();
for (Object state : values) {
System.out.println(state);
}
}
// main thread
public static void main(String[] args) {
Runnable r = new Runnable() {
@Override
public void run() {
Test application = new Test();
}
};
// Swing GUIs should be created and updated on the EDT
// http://docs.oracle.com/javase/tutorial/uiswing/concurrency/initial.html
SwingUtilities.invokeLater(r);
}
}
提示
- 不要扩展框架,只使用一个实例
- J2SE GUI(Swing和AWT)应该在EDT上创建和更新。特别是看
contentPane.setLayout(null)代码>这在现实世界中不起作用(可能将下一台PC称为“真实世界”)。使用布局管理器实现健壮的GUI。有关详细信息,请参阅,并根据需要对布局进行分组