【发布时间】:2015-09-19 12:32:12
【问题描述】:
所以我制作了一个利用 JComboBox 的程序。我添加了一个项目监听器,如下所示:
wellbox.addItemListener(
new ItemListener(){
@Override
public void itemStateChanged(ItemEvent ie) {
if (ie.getStateChange() == ItemEvent.SELECTED){
well = (wellbox.getSelectedIndex()-1);
if (well >=0){selected = true;}
}
}
}
);
wellbox 是 JComboBox 变量 well 是一个 int 并且 selected 是一个布尔值。
现在,项目监听器在第一次执行时工作,没问题。但是,在程序结束时,我有一个选项可以提示用户查看是否要再次运行该程序。整个代码本质上包裹在一个 while(true) 循环中,如果用户说不,他们不想再次运行,该循环就会中断。如果再次运行第二次,项目侦听器将停止工作,并且 getSelectedIndex() 不再返回所选索引。有人知道为什么吗?
再次运行时,JCombo Box 会再次初始化
我希望我已经提供了足够的信息来获得解决方案。
更新:
通过建议给予我会尝试更好地提出我的问题
我有三个公共的 void 方法
在第一次运行程序时,扩展 JFrame 的公共“类名”方法初始化第一个 part1 方法,该方法构建第一个 JPanel 并将其添加到 JFrame。单击按钮后,第一个 JPanel 将从 JFrame 中删除,第一个方法将我们带到第二个方法
在第二种方法中,第二个 JPanel 被构建,并添加到 JFrame。单击另一个按钮后,第二个 JPanel 将从 JFrame 中删除,第二个方法将我们带到第三个方法
第三种方法构建第三个 JPanel 并将其添加到 JFrame。然后,一旦单击按钮,所有内容都会使用以下代码删除:
part1.removeAll();
part2.removeAll();
part3.removeAll();
第三个方法然后从 JFrame 中删除第三个 JPanel。如果用户单击表示他们希望再次运行它的按钮,则第三种方法再次将我们带到第一种方法,再次重建 JPanel 并添加它们...
在第二种方法中,在第二个 JPanel 中,我有一个 JComboBox 正在初始化并添加到第二个 JPanel。第一次,它按原样运行,项目侦听器返回正确的索引。但是一旦如前所述再次运行,在删除所有内容然后重新构建之后,项目侦听器不再返回索引值。有谁知道为什么?
如果需要,这几乎是我的代码。我没有描述我正在使用的任何方法都是不重要的。我已经取出了一堆与这个问题完全无关的代码。
主类:
import java.awt.*;
import java.awt.event.*;
import java.io.*;
import javax.swing.*;
public class ReportCardGenerator {
public static void main(String[] args) {
debugReportGUI f = new debugReportGUI();
f.setVisible(true);
}
}
debugReportGUI 类
import java.awt.*;
import java.awt.event.*;
import java.io.*;
import java.util.ArrayList;
import javax.swing.*;
import javax.swing.event.*;
public class debugReportGUI extends JFrame
{
JPanel part1 = new JPanel ();
JPanel part2 = new JPanel ();
JPanel part3 = new JPanel ();
JComboBox wellbox;
JButton cont = new JButton ("Continue");
int buttonW = 110, buttonL = 30, well = -1;
Actions AL = new Actions ();
String[] skills = {"", "0", "1", "2", "3", "4", "5", "6", "7", "8", "9", "10"};
public debugReportGUI ()
{
super ("JFrame");
setLayout (new BorderLayout ());
setSize (800, 700);
setLocationRelativeTo (null);
setDefaultCloseOperation (JFrame.EXIT_ON_CLOSE);
setResizable (false);
add (part1, BorderLayout.CENTER);
Part1 ();
}
public void Part1 ()
{
part1.setLayout (null);
part1.add (cont);
cont.addActionListener (AL);
cont.setBounds (325, 630, buttonW, buttonL);
cont.setToolTipText ("Once all fields have been completed press to continue to next part of Generator");
}
private class Actions implements ActionListener
{
public void actionPerformed (ActionEvent ae)
{
if (ae.getSource () == cont)
{
part1.setVisible(false);
add (part2, BorderLayout.CENTER);
part2.setVisible (true);
Part2 ();
}
}
}
public void Part2 ()
{
part2.setLayout (null);
wellbox = new JComboBox (skills);
part2.add (wellbox);
wellbox.setLocation (75, 120);
wellbox.setSize (650, 40);
wellbox.addItemListener (
new ItemListener ()
{
//@ Override
public void itemStateChanged (ItemEvent ie)
{
if (ie.getStateChange () == ItemEvent.SELECTED)
{
well = (wellbox.getSelectedIndex () - 1);
System.out.println (well);
/*Initialized value of well is -1*/
}
}
}
);
JButton cont2 = new JButton ("Continue");
cont2.setBounds (345, 625, buttonW, buttonL);
cont2.setToolTipText ("When the skill for the ''well done'' comment and all failed items have been selected, press button ");
cont2.addActionListener (
new ActionListener ()
{
//@ Override
public void actionPerformed (ActionEvent ae)
{
if (well >= 0)
{
part2.setVisible(false);
add (part3, BorderLayout.CENTER);
part3.setVisible (true);
Part3 ();
}
else{
JOptionPane.showMessageDialog(null,"must select an option in the JComboBox","",JOptionPane.ERROR_MESSAGE);
}
}
}
);
part2.add (cont2);
}
public void Part3 ()
{
part3.setLayout (null);
JButton again = new JButton ("Write Another");
again.setBounds (530, 550, buttonW + 30, buttonL);
again.setToolTipText ("If you are finished with report card you can write another one for another student by clicking this button");
again.addActionListener (
new ActionListener ()
{
//@ Override
public void actionPerformed (ActionEvent ae)
{
well = -1;
part1.removeAll ();
part2.removeAll ();
part3.removeAll ();
remove (part3);
add (part1, BorderLayout.CENTER);
part1.setVisible (true);
Part1 ();
}
}
);
part3.add(again);
}
}
【问题讨论】:
标签: java swing jcombobox itemlistener