【发布时间】:2015-07-19 22:43:24
【问题描述】:
我正在创建一个程序,用户输入他们所有作业的成绩和每个作业的权重,然后程序计算总成绩。
在此处/互联网的大量帮助下,几乎完成了该程序,但我不知道如何执行最后一部分。
因此,当用户点击进入程序时,程序将汇总所有标记并考虑其特定权重并输出平均值。例如:
这样的公式是重量/100*马克。 (将它们加在一起)。所以... (40/100)*60 + (40/100)*80... 等等
那么我怎样才能用我的程序做到这一点呢?我需要实现某种类型的 for 循环,因为分配的数量一直在变化。
我已经在 Enter 按钮上实现了一个动作侦听器,以便将标记/重量/分配计数器传递给下一个类。
但是,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 循环来填充数组以获取标记/权重,但不确定如何为多个 JTextField 启动它
-
.. 好的。你在此基础上研究了什么?您尝试过什么?
-
比如我试过做 JTextField[] mark = new JTextField(20); .这不适用于我的代码,因为我需要它是一个直接的 JTextField 来解决格式问题。
标签: java swing actionlistener jtextfield