我正在学习数学,我想做一个计算器,告诉我在期末考试中我需要做的每一个笔记来得到每个字母的分数。教学大纲告诉我们,我们的笔记是这样计算的:
MT1 -第一次期中考试%26.6MT2-第二次期中考试%26.6F-最终百分比26.6Q -测验% 10HW-家庭作业% 10
这些是等级间隔:80 - 100A - 75 - 80B + 70 - 75B 65 - 70B - 60 - 65C +则高达55 -C 50 - 55C - 45 - 50D +降价D + 30 - 40F 0 30
您在JTextFields中输入MT1, MT2, Q, HW,当您单击按钮时,它将更新字母等级附近的jlabel。当我第一次运行代码时,它可以工作,但当我想更改它时,程序不会更新数字。你能帮忙吗?
Final_Calculator:
import javax.swing.*;
import java.awt.*;
public class Final_Calculator extends JFrame
{
public static void main(String[] args)
{
JFrame frame = new JFrame("Final Calculator");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.getContentPane().add(new Results());
frame.pack();
frame.setVisible(true);
}
}
等次
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.util.*;
public class Grades extends JPanel
{
private JLabel l1, l2, l3, l4;
private JTextField t1, t2, t3, t4;
private double sum,result, MT1, MT2, Q, HW;
private JButton button;
public Grades()
{
setPreferredSize(new Dimension(200,100));
l1 = new JLabel("MT1");
l2 = new JLabel("MT2");
l3 = new JLabel("Q");
l4 = new JLabel("HW");
t1 = new JTextField("100");
t2 = new JTextField("100");
t3 = new JTextField("100");
t4 = new JTextField("100");
button = new JButton("Click!");
TextListener listener = new TextListener();
button.addActionListener(listener);
add(l1);
add(l2);
add(l3);
add(l4);
add(t1);
add(t2);
add(t3);
add(t4);
add(button);
}
public double getMT1()
{
return Double.parseDouble(t1.getText());
}
public double getMT2()
{
return Double.parseDouble(t2.getText());
}
public double getQ()
{
return Double.parseDouble(t3.getText());
}
public double getHW()
{
return Double.parseDouble(t4.getText());
}
public void sum()
{
MT1 = getMT1();
MT2 = getMT2();
Q = getQ();
HW = getHW();
sum = (MT1 * 26.6) + (MT2 * 26.6) + (Q * 10) + (HW * 10);
}
public double getA()
{
sum();
result = (8000 - sum) / 26.6;
if (result <= 0)
{
return 0;
}
return result;
}
public double getAminus()
{
sum();
result = (7500 - sum) / 26.6;
if (result <= 0)
{
return 0;
}
return result;
}
public double getBplus()
{
sum();
result = (7000 - sum) / 26.6;
if (result <= 0)
{
return 0;
}
return result;
}
public double getB()
{
sum();
result = (6500 - sum) / 26.6;
if (result <= 0)
{
return 0;
}
return result;
}
public double getBminus()
{
sum();
result = (6000 - sum) / 26.6;
if (result <= 0)
{
return 0;
}
return result;
}
public double getCplus()
{
sum();
result = (5500 - sum) / 26.6;
if (result <= 0)
{
return 0;
}
return result;
}
public double getC()
{
sum();
result = (5000 - sum) / 26.6;
if (result <= 0)
{
return 0;
}
return result;
}
public double getCminus()
{
sum();
result = (4500 - sum) / 26.6;
if (result <= 0)
{
return 0;
}
return result;
}
public double getDplus()
{
sum();
result = (4000 - sum) / 26.6;
if (result <= 0)
{
return 0;
}
return result;
}
public double getD()
{
sum();
result = (3000 - sum) / 26.6;
if (result <= 0)
{
return 0;
}
return result;
}
private class TextListener implements ActionListener
{
public void actionPerformed(ActionEvent e)
{
Object source = e.getSource();
if(source == button)
{
MT1 = Double.parseDouble(t1.getText());
MT2 = Double.parseDouble(t2.getText());
Q = Double.parseDouble(t3.getText());
HW = Double.parseDouble(t4.getText());
sum();
getA();
getAminus();
getBplus();
getB();
getBminus();
getCplus();
getC();
getCminus();
getDplus();
getD();
}
}
}
}
结果:
import javax.swing.*;
import java.awt.*;
import java.util.*;
public class Results extends JPanel
{
private JLabel f1,f2,f3,f4,f5,f6,f7,f8,f9,f10,f11,g1,g2,g3,g4,g5,g6,g7,g8,g9,g10,g11;
private JPanel panel1, panel2;
private Grades grades;
public Results()
{
grades = new Grades();
setPreferredSize(new Dimension(800,800));
setLayout(new BorderLayout());
panel1 = new JPanel();
panel1.setLayout(new GridLayout(11,2));
panel1.setPreferredSize(new Dimension(200,200));
panel2 = new JPanel();
panel2.setPreferredSize(new Dimension(400,400));
f1 = new JLabel("Final");
g1 = new JLabel("Grade");
f2 = new JLabel(Double.toString(grades.getA()));
g2 = new JLabel(" A");
f3 = new JLabel(Double.toString(grades.getAminus()));
g3 = new JLabel(" A-");
f4 = new JLabel(Double.toString(grades.getBplus()));
g4 = new JLabel(" B+");
f5 = new JLabel(Double.toString(grades.getB()));
g5 = new JLabel(" B");
f6 = new JLabel(Double.toString(grades.getBminus()));
g6 = new JLabel(" B-");
f7 = new JLabel(Double.toString(grades.getCplus()));
g7 = new JLabel(" C+");
f8 = new JLabel(Double.toString(grades.getC()));
g8 = new JLabel(" C");
f9 = new JLabel(Double.toString(grades.getCminus()));
g9 = new JLabel(" C-");
f10 = new JLabel(Double.toString(grades.getDplus()));
g10 = new JLabel(" D+");
f11 = new JLabel(Double.toString(grades.getD()));
g11 = new JLabel(" D");
panel1.add(f1);
panel1.add(g1);
panel1.add(f2);
panel1.add(g2);
panel1.add(f3);
panel1.add(g3);
panel1.add(f4);
panel1.add(g4);
panel1.add(f5);
panel1.add(g5);
panel1.add(f6);
panel1.add(g6);
panel1.add(f7);
panel1.add(g7);
panel1.add(f8);
panel1.add(g8);
panel1.add(f9);
panel1.add(g9);
panel1.add(f10);
panel1.add(g10);
panel1.add(f11);
panel1.add(g11);
panel2.add(grades);
add(panel2, BorderLayout.NORTH);
add(panel1, BorderLayout.CENTER);
}
}
从它的外观/只工作一次,它看起来像你没有更新标签后单击按钮。标签文本必须通过Label . settext(")更新;