2015-07-19 60 views
0

我正在創建一個程序,用戶輸入他們的所有任務的等級以及每個單獨任務的權重,程序將計算總體等級。檢索和分析來自多個JTextFields的數據

幾乎完成程序與從這裏/互聯網的幫助負載,但我不知道如何做這最後一節。

因此,當用戶點擊進入該程序將聚集考慮到它們的比重和輸出的平均所有標記。例如:

方程式這樣的事情是重量/ 100 *大關。 (將它們加在一起)。所以...(40/100)* 60 +(40/100)* 80 ...等

那麼我怎麼用我的程序做到這一點?我需要實現某種類型的for循環,因爲分配的數量總是在變化。

如果Mark/Weight/Assignment計數器傳遞給下一個類,我已經在Enter按鈕上實現了一個actionlistener。

但是actionlistener只記錄最後的標記/重量。在上面的例子中是50和20。這是我面臨的另一個問題。

所以基本上我要求一種方法來獲取來自所有JTextField的輸入並分析它們以創建一個彙總所有分數的輸出。 (考慮到它們的重量)

代碼:

public class test{ 

public JButton button; 

private static final Insets normalInsets = new Insets(10,10,0,10); 
private static final Insets finalInsets = new Insets(10,10,10,10); 

private static JPanel createMainPanel(){ 
    GridBagConstraints gbc = new GridBagConstraints(); 

    //Adding the JPanels. Panel for instructions 
    JPanel panel = new JPanel(); 
    panel.setLayout(new GridBagLayout()); 

    int gridy = 1; 

    //JLabel for the Instructions 
    JTextArea instructionTextArea = new JTextArea(5, 30); 
    instructionTextArea.setEditable(false); 
    instructionTextArea.setLineWrap(true); 
    instructionTextArea.setWrapStyleWord(true); 

    JScrollPane instructionScrollPane = new JScrollPane(instructionTextArea); 
    addComponent(panel, instructionScrollPane, 0, gridy++, 3, 1, finalInsets, GridBagConstraints.CENTER, GridBagConstraints.HORIZONTAL); 

    //JLabels for Assignment 
    JLabel label1 = new JLabel("Assignment"); 
    label1.setHorizontalAlignment(JLabel.CENTER); 
    addComponent(panel, label1, 0, gridy, 1, 1, finalInsets,GridBagConstraints.CENTER,GridBagConstraints.HORIZONTAL); 

    //JLabel for Mark 
    JLabel label2 = new JLabel("Mark"); 
    label2.setHorizontalAlignment(JLabel.CENTER); 
    addComponent(panel, label2, 1, gridy, 1, 1, finalInsets, GridBagConstraints.CENTER, GridBagConstraints.HORIZONTAL); 

    //JLabel for Weight. 
    JLabel label3 = new JLabel("Weight"); 
    label3.setHorizontalAlignment(JLabel.CENTER); 
    addComponent(panel, label3, 2, gridy++, 1, 1, finalInsets, GridBagConstraints.CENTER, GridBagConstraints.HORIZONTAL); 

    //JLabel Number for the number list of assignments at the side. 
    gbc.gridy = 3; 

    for(int i = 1; i<=3; i++){ 
     String kok = String.valueOf(i); 
    JLabel number = new JLabel(kok); 
    number.setHorizontalAlignment(JLabel.CENTER); 
    addComponent(panel, number, 0, gbc.gridy++, 1, 1, normalInsets, GridBagConstraints.CENTER, GridBagConstraints.HORIZONTAL); 
    } 
    gbc.gridy =3; 

    //JTextfield for Mark 
    for(int i=0; i<3; i++){ 
    JTextField mark = new JTextField(20); 
    mark.setHorizontalAlignment(JLabel.CENTER); 
    addComponent(panel, mark, 1, gbc.gridy++, 1, 1, normalInsets, GridBagConstraints.CENTER,GridBagConstraints.NONE); 
    } 
    gbc.gridy = 3; 

    //JTextfield for Weight  
    for(int i=0; i<3; i++){  
    JTextField weight = new JTextField(20); 
    weight.setHorizontalAlignment(JLabel.CENTER); 
    addComponent(panel, weight, 2, gbc.gridy++, 1, 1, normalInsets, GridBagConstraints.CENTER,GridBagConstraints.NONE); 
    } 

    JButton button = new JButton("Enter"); 
    button.setHorizontalAlignment(SwingConstants.CENTER); 
    addComponent(panel, button, 3, gbc.gridy++, 1, 1, normalInsets, GridBagConstraints.CENTER,GridBagConstraints.CENTER); 

return panel; 
} 

private static void addComponent(Container container, Component component, int gridx, int gridy, int gridwidth, int gridheight, Insets insets, int anchor, int fill){ 
    GridBagConstraints grid = new GridBagConstraints(gridx, gridy, gridwidth, gridheight, 1.0D,1.0D,anchor,fill,insets,0,0); 
    container.add(component,grid); 
} 


    public static void main(String[] args) {  
    JFrame frame = new JFrame(); 
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
    frame.add(createMainPanel()); 
    frame.pack(); 
    frame.setVisible(true); 
    new test(); 
} 

具有標記/重量信息下一堂課。

public class test1 implements ActionListener { 

private final JTextField mark; 
private final JTextField weight; 



public test1(JTextField mark, JTextField weight) { 
    this.mark = mark; 
    this.weight = weight; 

} 


@Override 
public void actionPerformed(ActionEvent e) { 

String markstring = mark.getText(); 
String weightstring = weight.getText(); 

double markdouble = Double.parseDouble(markstring); 
double weightdouble = Double.parseDouble(weightstring); 

} 
+0

保持的集合或陣列中的文本字段的引用(「未知號碼」表明的集合)。迭代集合來計算最終結果。 –

+0

是的我正在考慮for循環來填充標記/重量的數組,但不知道如何從多個JTextFields – bob9123

+0

..開始。你基於這個研究了什麼?你有什麼**嘗試過?** –

回答

1
  • 你只需要保存JTextFields在一個載體,所以以後可以訪問它們。
  • 你不需要第二堂課。
  • 您的按鈕偵聽器也被創建。

    import java.awt.Component; 
    import java.awt.Container; 
    import java.awt.GridBagConstraints; 
    import java.awt.GridBagLayout; 
    import java.awt.Insets; 
    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.JOptionPane; 
    import javax.swing.JPanel; 
    import javax.swing.JScrollPane; 
    import javax.swing.JTextArea; 
    import javax.swing.JTextField; 
    import javax.swing.SwingConstants; 
    
    public class Test { 
        public JButton button; 
    
        private static final Insets normalInsets = new Insets(10,10,0,10); 
        private static final Insets finalInsets = new Insets(10,10,10,10); 
    
        public final static int NUM_OF_FIELDS = 3; 
    
        private static JTextField[] markTextFields = new JTextField[NUM_OF_FIELDS]; 
        private static JTextField[] weightTextFields = new JTextField[NUM_OF_FIELDS]; 
    
        private static JPanel createMainPanel() { 
         GridBagConstraints gbc = new GridBagConstraints(); 
    
         //Adding the JPanels. Panel for instructions 
         JPanel panel = new JPanel(); 
         panel.setLayout(new GridBagLayout()); 
    
         int gridy = 1; 
    
         //JLabel for the Instructions 
         JTextArea instructionTextArea = new JTextArea(5, 30); 
         instructionTextArea.setEditable(false); 
         instructionTextArea.setLineWrap(true); 
         instructionTextArea.setWrapStyleWord(true); 
    
         JScrollPane instructionScrollPane = new JScrollPane(instructionTextArea); 
         addComponent(panel, instructionScrollPane, 0, gridy++, 3, 1, finalInsets, GridBagConstraints.CENTER, GridBagConstraints.HORIZONTAL); 
    
         //JLabels for Assignment 
         JLabel label1 = new JLabel("Assignment"); 
         label1.setHorizontalAlignment(JLabel.CENTER); 
         addComponent(panel, label1, 0, gridy, 1, 1, finalInsets,GridBagConstraints.CENTER,GridBagConstraints.HORIZONTAL); 
    
         //JLabel for Mark 
         JLabel label2 = new JLabel("Mark"); 
         label2.setHorizontalAlignment(JLabel.CENTER); 
         addComponent(panel, label2, 1, gridy, 1, 1, finalInsets, GridBagConstraints.CENTER, GridBagConstraints.HORIZONTAL); 
    
         //JLabel for Weight. 
         JLabel label3 = new JLabel("Weight"); 
         label3.setHorizontalAlignment(JLabel.CENTER); 
         addComponent(panel, label3, 2, gridy++, 1, 1, finalInsets, GridBagConstraints.CENTER, GridBagConstraints.HORIZONTAL); 
    
         //JLabel Number for the number list of assignments at the side. 
         gbc.gridy = 3; 
    
         for (int i = 1; i <= 3; i++) { 
          String kok = String.valueOf(i); 
          JLabel number = new JLabel(kok); 
          number.setHorizontalAlignment(JLabel.CENTER); 
          addComponent(panel, number, 0, gbc.gridy++, 1, 1, normalInsets, GridBagConstraints.CENTER, GridBagConstraints.HORIZONTAL); 
         } 
         gbc.gridy = 3; 
    
         //JTextfield for Mark 
         for (int i = 0; i < NUM_OF_FIELDS; i++) { 
          JTextField mark = new JTextField(20); 
          markTextFields[i] = mark; 
          mark.setHorizontalAlignment(JLabel.CENTER); 
          addComponent(panel, mark, 1, gbc.gridy++, 1, 1, normalInsets, GridBagConstraints.CENTER, GridBagConstraints.NONE); 
         } 
         gbc.gridy = 3; 
    
         //JTextfield for Weight  
         for (int i = 0; i < NUM_OF_FIELDS; i++) {  
          JTextField weight = new JTextField(20); 
          weightTextFields[i] = weight; 
          weight.setHorizontalAlignment(JLabel.CENTER); 
          addComponent(panel, weight, 2, gbc.gridy++, 1, 1, normalInsets, GridBagConstraints.CENTER,GridBagConstraints.NONE); 
         } 
    
         JButton button = new JButton("Enter"); 
         button.setHorizontalAlignment(SwingConstants.CENTER); 
         addComponent(panel, button, 3, gbc.gridy++, 1, 1, normalInsets, GridBagConstraints.CENTER,GridBagConstraints.CENTER); 
         button.addActionListener(new ActionListener() { 
          @Override 
          public void actionPerformed(ActionEvent e) { 
           double finalScore = 0; 
           for (int i = 0; i < NUM_OF_FIELDS; i++) { 
            double weight = Double.parseDouble(weightTextFields[i].getText()); 
            double mark = Double.parseDouble(markTextFields[i].getText()); 
            finalScore += weight/100 * mark; 
           } 
           JOptionPane.showMessageDialog(null, "Final score: " + finalScore, "Final Score", JOptionPane.OK_OPTION); 
          } 
         }); 
         return panel; 
        } 
    
        private static void addComponent(Container container, Component component, int gridx, int gridy, int gridwidth, int gridheight, Insets insets, int anchor, int fill) { 
         GridBagConstraints grid = new GridBagConstraints(gridx, gridy, gridwidth, gridheight, 1.0D, 1.0D, anchor, fill, insets, 0, 0); 
         container.add(component, grid); 
        } 
    
        public static void main(String[] args) {  
         JFrame frame = new JFrame(); 
         frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
         frame.add(createMainPanel()); 
         frame.pack(); 
         frame.setVisible(true); 
         new Test(); 
        } 
    } 
    
+0

非常感謝。正在觀察矢量,這爲我節省了不可思議的時間。再次感謝。 – bob9123

+1

'Vector'?我們住在哪一年?另外'靜態'不是你的朋友,這會咬你後排 – MadProgrammer

+0

如果他正在學習爲什麼他不能從學習矢量是什麼開始?我試圖抓住他的代碼並做出簡單的改變,這樣事情可以按照他想要的方式工作。爲什麼不給他一個更好的解決方案,而不是給他一個更好的解決方案,而不是給他如何使用列表和非靜態方法/變量?:) – Lourenco