2012-08-30 28 views
0

我想创建一个gui程序,JButtons在一个循环中,他们随着每次迭代而变化。我无法实现这一点,我不明白为什么,它似乎不应该是一个问题,但无论我尝试什么,我的“q”变量不会增加...JButton GUI程序其中按钮更改基于循环迭代

在某些情况下,我能够摆脱所有错误,但我的do-while循环从不执行。在下面的例子中,我收到以下错误:“不能引用在不同方法中定义的内部类中的非最终变量q”。

我一直在试图弄清楚这一点,现在我很欣赏任何建议。

在此先感谢。

这里是我的代码(注意我的q变量):

public class MyClass extends JFrame{ 
     private JTextField item1; 
     private JTextField item2; 
     private JTextField item3; 
     private JTextField item4; 
     private JTextField item5; 
     private JButton button1 = new JButton("Process Item #1"); 
     private JButton button2 = new JButton("Confirm Item #1"); 
     private JButton button3 = new JButton("View Order"); 
     private JButton button4 = new JButton("Finish Order"); 
     private JButton button5 = new JButton("New Order"); 
     private JButton button6 = new JButton("Exit"); 
     private Scanner x; 
     private int exitFlag = 0; 
     public String[] idArray = new String[10]; 
     public String[] recordArray = new String[10]; 
     public String[] priceArray = new String[10]; 

     public void openFile(){ 
      try{ 
       x = new Scanner(new File("inventory.txt")); 
       x.useDelimiter(",|" + System.getProperty("line.separator")); 
      } 
      catch(Exception e){ 
       System.out.println("Could not find file"); 
      } 
     } 
     public void readFile(){ 
      int i=0; 
      while(x.hasNext()){ 
       idArray[i] = x.next(); 
       recordArray[i] = x.next(); 
       priceArray[i] = x.next(); 
       i++; 
      } 
     } 
     public MyClass(){ 
      super("Matt's World of Music"); 
      setLayout(new FlowLayout()); 
      Box itemBox = Box.createVerticalBox(); 
      Box itemBox2 = Box.createHorizontalBox(); 

      JLabel label1 = new JLabel("Enter number of items in this order:"); 
      JLabel label2 = new JLabel("Enter CD ID for Item #1:"); 
      JLabel label3 = new JLabel("Enter quantity for Item #1:"); 
      JLabel label4 = new JLabel("Item #1 info:"); 
      JLabel label5 = new JLabel("Order subtotal for 0 item(s):"); 

      item1 = new JTextField(10); 
      item2 = new JTextField(10); 
      item3 = new JTextField(10); 
      item4 = new JTextField(10); 
      item5 = new JTextField(10); 

      itemBox.add(label1); 
      itemBox.add(item1); 
      itemBox.add(label2); 
      itemBox.add(item2); 
      itemBox.add(label3); 
      itemBox.add(item3); 
      itemBox.add(label4); 
      itemBox.add(item4); 
      itemBox.add(label5); 
      itemBox.add(item5); 
      itemBox2.add(button1); 
      itemBox2.add(button2); 
      itemBox2.add(button3); 
      itemBox2.add(button4); 
      itemBox2.add(button5); 
      itemBox2.add(button6); 
      itemBox.add(itemBox2); 
      add(itemBox); 
      button2.setEnabled(false); 
      button3.setEnabled(false); 
      button4.setEnabled(false); 
      item4.setEditable(false); 
      item5.setEditable(false); 

      //*** NOTE Q VARIABLE ****// 
      int q=0; 


      do{ 
       //Process Item 

       button1.addActionListener(new ActionListener(){ 
        public void actionPerformed(ActionEvent e){ 
         String y = item1.getText(); 
         MyClass obj = new MyClass(); 
         button1.setEnabled(false); 
         button2.setEnabled(true); 

         obj.openFile(); 
         obj.readFile(); 


         //start loop 
         for(int i=0; i < obj.idArray.length; i++){ 
          if(item2.getText().equals(obj.idArray[i])==true){ 
           //set item4 text field to price id and other details 
           item4.setText(obj.idArray[i] + " " + obj.recordArray[i] + " $" + obj.priceArray[i].replaceAll("\\s","")); 

           //add to cart 

          } 
         } 
         //*** NOTE Q VARIABLE ****// 
         q++; 

        } 
       }); 
       //Confirm Item 
       button2.addActionListener(new ActionListener(){ 
        public void actionPerformed(ActionEvent e){ 
         //Execute when button is pressed 
         JOptionPane.showMessageDialog(null, "Item #1 Accepted"); 
        } 
       }); 
       //View Order 
       button3.addActionListener(new ActionListener(){ 
        public void actionPerformed(ActionEvent e){ 
         //Execute when button is pressed 
         System.out.println("View Order"); 
        } 
       }); 
       //Finish Order 
       button4.addActionListener(new ActionListener(){ 
        public void actionPerformed(ActionEvent e){ 
         //Execute when button is pressed 
         System.out.println("Finish Order"); 
        } 
       }); 
       //New Order 
       button5.addActionListener(new ActionListener(){ 
        public void actionPerformed(ActionEvent e){ 
         //Execute when button is pressed 
         System.out.println("New Order"); 
        } 
       }); 
       //Quit 
       button6.addActionListener(new ActionListener(){ 
        public void actionPerformed(ActionEvent e){ 

         //exit program 

        } 
       }); 
      }while(q < Integer.parseInt(item1.getText())); 
     }  
} 
+0

到底什么是'q'的目的是什么?我真的不明白你在这里做什么。 –

+0

拍摄,你的代码甚至编译? –

+0

如果我是正确的,你不能使用声明为* Listener方法外部的变量,然后使用这些方法。因此,您需要在每个侦听器方法中声明更多的变量。 – manix

回答

1

考虑制作一个字段,该字段保存您希望按钮反映的任何状态的值,按下按钮更改此字段,然后使用此字段更改按钮的文本。例如:

import java.awt.event.ActionEvent; 
import java.awt.event.ActionListener; 

import javax.swing.*; 

public class ChangeButton extends JPanel { 
    private int buttonCount = 1; 
    private JButton button = new JButton(); 

    public ChangeButton() { 
     add(button); 
     button.setText(makeButtonText()); 

     button.addActionListener(new ActionListener() { 

     @Override 
     public void actionPerformed(ActionEvent evt) { 
      JButton btn = (JButton) evt.getSource(); 
      btn.setText(makeButtonText()); 
     } 
     }); 
    } 

    private String makeButtonText() { 
     String buttonText = "Process number " + buttonCount; 
     buttonCount++; 
     return buttonText; 
    } 

    private static void createAndShowGui() { 
     ChangeButton mainPanel = new ChangeButton(); 

     JFrame frame = new JFrame("ChangeButton"); 
     frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
     frame.getContentPane().add(mainPanel); 
     frame.pack(); 
     frame.setLocationByPlatform(true); 
     frame.setVisible(true); 
    } 

    public static void main(String[] args) { 
     SwingUtilities.invokeLater(new Runnable() { 
     public void run() { 
      createAndShowGui(); 
     } 
     }); 
    } 
} 

或者你可以按住ActionListener的自身内部的领域:

button2.setAction(new AbstractAction("Process item 1") { 
    private int button2Count = 2; 

    @Override 
    public void actionPerformed(ActionEvent evt) { 
     JButton btn = (JButton) evt.getSource(); 
     btn.setText(makeButton2Text()); 
    } 

    private String makeButton2Text() { 
     String button2Text = "Process item " + button2Count; 
     button2Count++; 
     return button2Text; 
    } 


    }); 
1

下面的代码是处理按钮按下事件的方式。

button1.addActionListener(new ActionListener(){ 
    public void actionPerformed(ActionEvent e){ 
    ... 
    } 
}); 

但是,这是位于类构造函数内部的内部类。在这种情况下,当在内部类中使用在构造函数或方法内初始化的局部变量时,变量必须声明为final。但是,您不能最终声明它,因为您想增加它。

无论如何,在actionPerformed方法中增加它是错误的。正如我之前所说的,当按下按钮时,代码就会执行。

+0

你是对的@丹,那是我的意思 – manix

+0

哦!现在我明白了:),那么在gui加载后,如何更改Jbutton呢? – AnchovyLegend

+0

你想更改按钮的属性? –