0

我正在创建一个 GUI,但不知道如何将JList用户选择存储在一个数组中。我试过了List<<Sting>>Object[]等等……JRadioButtons其他的图形用户界面都很好,只是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);
        }
}
4

2 回答 2

2

此变体迭代Object[]返回的 bygetSelectedValues()以在控制台中显示预期值。接下来要修复的是布局。

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

尖端

  • 不要扩展框架,只需使用实例。
  • 应该在 EDT 上创建和更新 J2SE GUI(Swing 和 AWT)。请参阅Swing 中的并发 -尤其是初始线程。
  • contentPane.setLayout(null); 这在现实世界中行不通(可能将下一台 PC 读作“现实世界”)。使用布局管理器获得强大的 GUI。有关详细信息,请参阅在容器内布局组件,以及根据需要对布局进行分组的嵌套布局示例。
于 2013-03-01T01:53:50.050 回答
0

根据文档, 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]);
  }
}
于 2013-02-28T17:56:54.860 回答