2014-03-03 30 views
0

我應該使用GUI創建一個簡單的乘法表,並且在大多數情況下,它正在做它應該做的事情。但我只是無法弄清楚它的底部,如果你點擊一個特定的按鈕,文本會說出這兩個數字相乘和答案。一個簡單的GUI乘法表

無論我點擊哪個按鈕,它只會告訴我輸入的數字並將它們相乘。這裏是我的代碼:

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

import javax.swing.JButton; 
import javax.swing.JFrame; 
import javax.swing.JLabel; 
import javax.swing.JPanel; 
import javax.swing.JOptionPane; 

public class MultiplicationTable { 
    int clickedCount; 
    int rowCount; 
    int colCount; 

    JFrame frame; 
    JPanel buttonPanel, countPanel; 
    JLabel countLabel; 


    private void createAndShowGui(){ 

     rowCount = Integer.parseInt(JOptionPane 
       .showInputDialog("How many rows do you want your multiplication table to be? ")); 

     colCount = Integer 
       .parseInt(JOptionPane 
         .showInputDialog("How many columns do you want your multiplication table to be? ")); 



     frame = new JFrame ("Simple Multiplication Table"); 
     frame.setLayout(new BorderLayout()); 

     buttonPanel = new JPanel(); 

     buttonPanel.setLayout(new GridLayout(rowCount, colCount)); 


     countPanel = new JPanel();  
     countLabel = new JLabel("? x ? = ?"); 

     for(int rowCounter = 1; rowCounter <= rowCount; rowCounter++) 
      for(int colCounter = 1; colCounter <= colCount; colCounter++){ 

       final JButton j = new JButton(Integer.toString(rowCounter * colCounter)); 

       j.addActionListener(new ActionListener(){ 


        @Override 
        public void actionPerformed(ActionEvent event) { 
         for(int rc = 1; rc <= rowCount; rc++) 
          for(int cc = 1; cc <= colCount; cc++){ 
           countLabel.setText(rc + " x " + cc + " = " + String.valueOf(rc * cc)); 
          } 
        } 
       }); 

       buttonPanel.add(j); 
      } 

     frame.add(buttonPanel, BorderLayout.NORTH); 
     countPanel.add(countLabel); 
     frame.add(countPanel, BorderLayout.SOUTH); 

     frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
     frame.pack(); 
     frame.setVisible(true); 
    } 

    public static void main(String[] args){ 
     MultiplicationTable c = new MultiplicationTable(); 
     c.createAndShowGui(); 
    } 
} 
+2

什麼你真的希望看到申請呢? –

回答

2

您當前ActionListener沒有上下文,它不知道使用了什麼樣的兩個值生成你給該按鈕的值。

你可以做的是創建一個ActionListener,它接受用於生成按鈕文本的值,然後就可以相應地更新標籤。例如...

public class MultiplerActionListener implements ActionListener { 
    private int[] values; 
    private JLabel label; 
    public MultiplerActionListener(JLabel label, int... valuesToMultiple) { 
     values = valuesToMultiple; 
     this.label = label; 
    } 

    @Override 
    public void actionPerformed(ActionEvent event) {   
     int answer = 1; 
     StringBuilder sb = new StringBuilder(64); 
     for (int value : values) { 
      if (sb.length() > 0) { 
       sb.append(" x "); 
      } 
      sb.append(value); 
      answer *= value; 
     } 
     sb.append(" = "); 
     sb.append(answer); 
     label.setText(sb.toString()); 
    } 
} 

,那麼只需在需要時創建按鈕

for(int rowCounter = 1; rowCounter <= rowCount; rowCounter++) { 
    for(int colCounter = 1; colCounter <= colCount; colCounter++){ 
     final JButton j = new JButton(Integer.toString(rowCounter * colCounter)); 
     j.addActionListener(new MultiplerActionListener(countLabel, rowCounter, colCounter)); 
     buttonPanel.add(j); 
    } 
}