所以我製作了一個利用JComboBox的程序。我已經加入一個項目聽者像這樣:JComboBox getSelectedIndex不能工作兩次?
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變量 井是int和選定的是一個布爾值。
現在,項目偵聽器在第一次執行時工作,沒有問題。但是,在程序結束時,我有一個選項,提示用戶看看他們是否要再次運行程序。整個代碼基本上被封裝在一個while(true)循環中,如果用戶說不,他們不想再次運行。如果第二次再次運行,則項監聽器停止工作,並且getSelectedIndex()不再返回選定的索引。有人知道爲什麼嗎?
再次運行時JCombo框會再次被初始化BTW
我希望我能提供足夠的信息來獲得的解決方案。
UPDATE:
通過一個建議給我會嘗試更好地問我的問題
我有三個公共無效方法
在第一次運行該程序,擴展了公衆「類名」的方法JFrame初始化構建第一個JPanel並將其添加到JFrame的第一個part1方法。點擊一個按鈕後,第一個JPanel將從JFrame中刪除,第一種方法將在第二種方法中構建第二個JPanel並將其添加到JFrame中,從而使第二種方法在第二種方法中引發我們第二種方法
。點擊另一個按鈕後,將從JFrame中刪除第二個JPanel,第二種方法將我們帶到第三個方法
第三種方法構建第三個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);
}
}
謝謝你的迴應,我想我自言自語很差。 IT實際上並沒有包含在一個while(true)循環中,而是效果如此。我有3種公共無效的方法,第一種將第一種帶到第二種,第二種到第三種,如果在第三種,他們再說一遍,那麼第三種將把它帶回第一種。 – AntonioK
@AntonioK:你會想改善你的問題,然後張貼足夠的相關代碼來幫助我們更好地理解你的問題,最好是[mcve](請閱讀鏈接,因爲如何創建這個鏈接的細節很重要)。祝你好運。 –
我已經延長我的問題,試圖解釋的,希望它有助於找到一個解決方案... – AntonioK