2017-01-05 144 views
1

使用此代碼,我想開發一個程序,使用戶可以在單個文本字段中輸入5個int值。這些值將被存儲在一個數組中,當按下按鈕時,將繪製一個條形圖來表示輸入的值。類BtnHandler處理按鈕單擊事件,類gegHandler處理文本框輸入事件。我無法獲得要在數組中收集的用戶輸入。這是我的代碼。這裏是按下回車鍵:Java - 通過單個文本字段將多個值輸入到數組中

package voorbeeld0805; 
import javax.swing.*; 
import java.awt.*; 
import java.awt.event.*; 

public class Vb0805 extends JFrame { 
    public static void main(String args[]) { 
     JFrame frame = new Vb0805(); 
     frame.setSize(300, 300); 
     frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
     frame.setTitle("Voorbeeld 0805"); 
     frame.setContentPane(new Tekenpaneel()); 
     frame.setVisible(true); 
    } 
} 

class Tekenpaneel extends JPanel { 
    private Staafdiagram staafdiagram; 
    private JButton knopTeken; 
    private JTextField gegevensVak; 
    private JLabel lblNo; 
    private int[] lengtes = new int [5]; 
    public Tekenpaneel() { 
     setBackground(Color.ORANGE); 

     knopTeken = new JButton("Teken"); 
     knopTeken.addActionListener(new BtnHandler()); 
     gegevensVak = new JTextField(5); 
     gegevensVak.addActionListener(new gegHandler()); 
     lblNo = new JLabel("Enter Value"); 

     add(knopTeken); 
     add (gegevensVak); 
     add (lblNo); 

    } 

    public void paintComponent(Graphics g) { 
     super.paintComponent(g);    
     if (staafdiagram != null) 
     { 
      staafdiagram.teken(g, 50, 250); 
     } 
     this.requestFocusInWindow(); 


    } 

    class BtnHandler implements ActionListener { 
     public void actionPerformed(ActionEvent e) { 

      //To test using the first constructor. Works perfectly 

      /*int [] lengtes = { 144, 98, 117, 130, 172, 173 }; 
      staafdiagram = new Staafdiagram(lengtes, 30);*/ 

      repaint(); 
     } 
    }  
    class gegHandler implements ActionListener { 
     public void actionPerformed(ActionEvent e) { 
      //Here user input should be collected, placed in an array and 
      //saved in staafdiagram.setLengtes(); 
      for (int i = 0; i < lengtes.length; i++){ 
       lblNo.setText("Next Value Is " + i++); 
       lengtes[i] = Integer.parseInt(gegevensVak.getText()); 
       gegevensVak.setText("");//clear textfield after entering a value  
      } 
     } 
    } 
} 

class Staafdiagram { 
    private int[] lengtes; 
    private int witruimte;//this generates the spaces between the bars 

    //First contructor with 2 arguments array en space 
    public Staafdiagram(int[] lengtes, int witruimte) { 
     this.lengtes = lengtes; 
     this.witruimte = witruimte; 
    } 

    //Second constructor with only 1 argument space 
    public Staafdiagram(int witruimte) { 
     this.witruimte = witruimte; 
    } 

    //With this, i want the user input to be collected in an array 
    public void setLengtes(int[] lengtes) { 
     this.lengtes = lengtes; 
    } 

    //To calculate the bar with the highest value 
    public int bepaalMax(){ 
     int maximum = lengtes [0]; 
     for (int i = 1; i < lengtes.length; i++){ 
      if (lengtes[i] > maximum) 
      { 
       maximum = lengtes[i]; 
      }   
     } 
     return maximum; 
    } 

    //To calculate the bar with the lowest value 
    public int bepaalMin(){ 
     int minimum = lengtes [0]; 
     for (int i = 1; i < lengtes.length; i++){   
      if (lengtes[i] < minimum) 
      { 
       minimum = lengtes[i]; 
      }   
     } 
     return minimum; 
    } 

    public void teken(Graphics g, int x, int y) { 
     int startX = x; 
     // Draw the bars 
     for(int lengte : lengtes) { 
      if (lengte == bepaalMax()) 
      g.setColor(Color.red); 
      else if (lengte == bepaalMin()) 
      g.setColor(Color.green); 
      else 
      g.setColor(Color.black); 

      g.fillRect(x, y - 20 - lengte, 20, lengte); 
      x += witruimte; 

     } 
     // Display the values under the bars 
     x = startX; 
     for(int lengte : lengtes) { 
      g.drawString(String.format("%d", lengte), x, y); 
      x += witruimte; 
     } 
    } 
} 

回答

1

如果您在JTextField添加ActionListener,當你的「行動」這一領域的actionPerformed()方法被調用。

如果您想將按鈕與您的操作關聯起來,您應該將您在JButton上定義的ActionListener更豐富,以獲取用戶輸入的值。這個想法被鏈接的同時作爲兩種操作應該執行的一箇中的另一個之後:

這些值將被存儲在一個陣列,並且在按壓按鈕時, 條形圖將被吸引到表示所輸入的值。

class BtnHandler implements ActionListener { 

    public void actionPerformed(ActionEvent e) { 
     // collection information from textfield  
     for (int i = 0; i < lengtes.length; i++){ 
      lblNo.setText("Next Value Is " + i++); 
      lengtes[i] = Integer.parseInt(gegevensVak.getText()); 
      gegevensVak.setText("");//clear textfield after entering a value  
     } 
     // rendering   
     staafdiagram = new Staafdiagram(lengtes, 30);*/ 
     repaint(); 
    } 
}  
+0

問題依然存在。 –

+0

這不是邏輯。你確定已經移除了另一個聽衆並且完全遵循了所提出的解決方案嗎? – davidxxx

+0

是的,我完全按照你的建議。 –

0
  1. 獲取從文本字段輸入文本
  2. 拆分到字符串數組
  3. 轉換成int

修改代碼如下。

class BtnHandler implements ActionListener { 
    public void actionPerformed(ActionEvent e) { 

     //To test using the first constructor. Works perfectly 

     /*int [] lengtes = { 144, 98, 117, 130, 172, 173 }; 
     staafdiagram = new Staafdiagram(lengtes, 30);*/ 

     String textInt=gegevensVak .getText();  //gegevensVak is the name of the textfield you mentioned 
     String[] strArray = textInt.split(); 
     //now convert the string to int and add to a new array 
     repaint(); 
    } 
} 
相關問題