我正在創建一個程序,用戶輸入他們的所有任務的等級以及每個單獨任務的權重,程序將計算總體等級。檢索和分析來自多個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);
}
保持的集合或陣列中的文本字段的引用(「未知號碼」表明的集合)。迭代集合來計算最終結果。 –
是的我正在考慮for循環來填充標記/重量的數組,但不知道如何從多個JTextFields – bob9123
..開始。你基於這個研究了什麼?你有什麼**嘗試過?** –