JComboBox 在项目选择后不更新
JComboBox not updating after item selection
我有一些代码错误(我将在下面显示),当用户从 JComboBox 中选择一个项目时,程序没有更新并认为没有选择任何项目。值得一提的是 .setSelectedIndex
确实会更新并让程序知道所选项目已更改,即使它是 .setSelectedIndex
更改的结果
这是代码:(我将把所有项目的代码放在这里,因为我不知道在这种情况下什么是最小的可重现示例,因为我不知道是什么导致了问题)
package sample;
import sun.security.mscapi.CPublicKey;
import javax.swing.*;
import java.awt.*;
import java.awt.Button;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
public class Main extends JFrame {
static final int WIDTH = 600;
static final int HEIGHT = 400;
public static final String TITLE = "Schedule Maker";
public JButton start;
//Hour array
private int[] hours = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24,};
private JTextField heading = new JTextField("Hours");
private JTextField selectedHour = new JTextField(15);
private JComboBox hourBox = new JComboBox();
public Main() {
for (int i = 0; i < hours.length; i++) {
hourBox.addItem(hours[i]);
}
hourBox.setSelectedIndex(-1);
heading.setEditable(false);
hourBox.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent event) {
selectedHour.setText("You selected : " + ((JComboBox) event.getSource()).getSelectedItem());
}
});
int item = hourBox.getSelectedIndex();
this.setLayout(new FlowLayout());
this.add(heading);
this.add(selectedHour);
this.add(hourBox);
}
public void create() {
start = new JButton();
JFrame frame = new Main();
JPanel panel = new JPanel();
frame.setTitle("Title (I'm great at names i know)");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(300, 200);
frame.setVisible(true);
frame.pack();
frame.setTitle("TITLE");
frame.setSize(300, 200);
frame.setResizable(true);
frame.setVisible(true);
frame.add(panel);
frame.add(start);
start.setText("Select an hour");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
ActionListener click = new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
if (e.getSource() == start) {
if (hourBox.getSelectedIndex() == -1) {
start.setText("Please select an hour");
} else {
start.setText("Some text since the rest of the program is not done yet");
}
}
}
};
start.addActionListener(click);
}
public static void main(String args[]) {
Main m = new Main();
m.create();
}
}
我发现您的代码有几个问题。让我列出它们,从有问题的错误到样式问题;)
- 您正在为您的 class 创建 两个实例 。
main
方法中的第一个,create()
方法中的第二个。您在 create()
中引用的 hourBox
与您在 JFrame
中显示的对象 不同!都是很纠结的。这就是导致您出现问题的原因!请允许我整理一下……以下几点是我看到的其他问题,但它们与您的问题无关。我无论如何都会列出它们来解释我所做的更改。
JComboBox
使用的是通用类型,因此您应该指定它:JComboBox<Integer> hourBox = new JComboBox<Integer>();
- 有很多不必要的代码,例如
setVisible()
/setTitle()
/setSize()
/setDefaultCloseOperation()
的重复调用或 panel
等未使用的对象。
- 您不需要完整的
int
值数组即可将您的时间输入组合框。看下面的代码!
试图将这些想法应用到您的代码中,我想出了这个:
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
public class Main extends JFrame {
private static final long serialVersionUID = 8333256750721907342L;
static final int WIDTH = 600;
static final int HEIGHT = 400;
public static final String TITLE = "Schedule Maker";
private static final int HOURS = 24;
public Main() {
this.setTitle(TITLE);
this.setResizable(true);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setLayout(new FlowLayout());
JTextField selectedHour = new JTextField(15);
JComboBox<Integer> hourBox = new JComboBox<Integer>();
for (int i = 1; i <= HOURS; i++) {
hourBox.addItem(i); // add hour to combo box
}
hourBox.setSelectedIndex(-1);
hourBox.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent event) {
selectedHour.setText(
"You selected : " + hourBox.getSelectedItem());
setTitle("HOUR: " + hourBox.getSelectedItem());
}
});
//int item = hourBox.getSelectedIndex();
this.add(new JTextField("Hours"));
this.add(selectedHour);
this.add(hourBox);
JButton start = new JButton();
start.setText("Select an hour");
this.add(start);
start.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
if (e.getSource() == start) {
if (hourBox.getSelectedIndex() == -1) {
start.setText("Please select an hour");
} else {
start.setText("gud boi!");
}
}
}
});
this.pack();
this.setSize(300, 200);
this.setLocationRelativeTo(null); // center window on screen
this.setVisible(true);
}
public static void main(String args[]) {
SwingUtilities.invokeLater(new Runnable() {
@Override
public void run() {
new Main();
}
});
}
}
...它按预期工作。
我有一些代码错误(我将在下面显示),当用户从 JComboBox 中选择一个项目时,程序没有更新并认为没有选择任何项目。值得一提的是 .setSelectedIndex
确实会更新并让程序知道所选项目已更改,即使它是 .setSelectedIndex
更改的结果
这是代码:(我将把所有项目的代码放在这里,因为我不知道在这种情况下什么是最小的可重现示例,因为我不知道是什么导致了问题)
package sample;
import sun.security.mscapi.CPublicKey;
import javax.swing.*;
import java.awt.*;
import java.awt.Button;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
public class Main extends JFrame {
static final int WIDTH = 600;
static final int HEIGHT = 400;
public static final String TITLE = "Schedule Maker";
public JButton start;
//Hour array
private int[] hours = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24,};
private JTextField heading = new JTextField("Hours");
private JTextField selectedHour = new JTextField(15);
private JComboBox hourBox = new JComboBox();
public Main() {
for (int i = 0; i < hours.length; i++) {
hourBox.addItem(hours[i]);
}
hourBox.setSelectedIndex(-1);
heading.setEditable(false);
hourBox.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent event) {
selectedHour.setText("You selected : " + ((JComboBox) event.getSource()).getSelectedItem());
}
});
int item = hourBox.getSelectedIndex();
this.setLayout(new FlowLayout());
this.add(heading);
this.add(selectedHour);
this.add(hourBox);
}
public void create() {
start = new JButton();
JFrame frame = new Main();
JPanel panel = new JPanel();
frame.setTitle("Title (I'm great at names i know)");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(300, 200);
frame.setVisible(true);
frame.pack();
frame.setTitle("TITLE");
frame.setSize(300, 200);
frame.setResizable(true);
frame.setVisible(true);
frame.add(panel);
frame.add(start);
start.setText("Select an hour");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
ActionListener click = new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
if (e.getSource() == start) {
if (hourBox.getSelectedIndex() == -1) {
start.setText("Please select an hour");
} else {
start.setText("Some text since the rest of the program is not done yet");
}
}
}
};
start.addActionListener(click);
}
public static void main(String args[]) {
Main m = new Main();
m.create();
}
}
我发现您的代码有几个问题。让我列出它们,从有问题的错误到样式问题;)
- 您正在为您的 class 创建 两个实例 。
main
方法中的第一个,create()
方法中的第二个。您在create()
中引用的hourBox
与您在JFrame
中显示的对象 不同!都是很纠结的。这就是导致您出现问题的原因!请允许我整理一下……以下几点是我看到的其他问题,但它们与您的问题无关。我无论如何都会列出它们来解释我所做的更改。 JComboBox
使用的是通用类型,因此您应该指定它:JComboBox<Integer> hourBox = new JComboBox<Integer>();
- 有很多不必要的代码,例如
setVisible()
/setTitle()
/setSize()
/setDefaultCloseOperation()
的重复调用或panel
等未使用的对象。 - 您不需要完整的
int
值数组即可将您的时间输入组合框。看下面的代码!
试图将这些想法应用到您的代码中,我想出了这个:
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
public class Main extends JFrame {
private static final long serialVersionUID = 8333256750721907342L;
static final int WIDTH = 600;
static final int HEIGHT = 400;
public static final String TITLE = "Schedule Maker";
private static final int HOURS = 24;
public Main() {
this.setTitle(TITLE);
this.setResizable(true);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setLayout(new FlowLayout());
JTextField selectedHour = new JTextField(15);
JComboBox<Integer> hourBox = new JComboBox<Integer>();
for (int i = 1; i <= HOURS; i++) {
hourBox.addItem(i); // add hour to combo box
}
hourBox.setSelectedIndex(-1);
hourBox.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent event) {
selectedHour.setText(
"You selected : " + hourBox.getSelectedItem());
setTitle("HOUR: " + hourBox.getSelectedItem());
}
});
//int item = hourBox.getSelectedIndex();
this.add(new JTextField("Hours"));
this.add(selectedHour);
this.add(hourBox);
JButton start = new JButton();
start.setText("Select an hour");
this.add(start);
start.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
if (e.getSource() == start) {
if (hourBox.getSelectedIndex() == -1) {
start.setText("Please select an hour");
} else {
start.setText("gud boi!");
}
}
}
});
this.pack();
this.setSize(300, 200);
this.setLocationRelativeTo(null); // center window on screen
this.setVisible(true);
}
public static void main(String args[]) {
SwingUtilities.invokeLater(new Runnable() {
@Override
public void run() {
new Main();
}
});
}
}
...它按预期工作。