Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/348.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在Java中将JList选择存储在数组字符串[]中_Java_Arrays_Swing_Jlist - Fatal编程技术网

在Java中将JList选择存储在数组字符串[]中

在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

我正在创建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 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。有关详细信息,请参阅,并根据需要对布局进行分组

您确定在获取所选项目之前选择了JList项目吗?我知道它会返回一个对象数组。我尝试了所有这些变化。实际上,我可以打印到控制台:Object[]values=statesList.getSelectedValues();System.out.println(值[0])//但问题是我无法将选定的值存储在数组中。我读了很多次文档。。。