Java Jbutton未清除我的文本字段
我有一个按钮来清除我正在整理的程序中的文本字段,但当我单击它时,它不会清除它们。这是我的代码加上按钮的代码。我不知道怎么回事,这是我展示的设置GUI的方式,这是我一直以来的工作方式Java Jbutton未清除我的文本字段,java,swing,jbutton,actionlistener,Java,Swing,Jbutton,Actionlistener,我有一个按钮来清除我正在整理的程序中的文本字段,但当我单击它时,它不会清除它们。这是我的代码加上按钮的代码。我不知道怎么回事,这是我展示的设置GUI的方式,这是我一直以来的工作方式 public class GradeCalculator extends JFrame implements ActionListener { Container c; JTextField gradeWeight1,gradeWeight2,gradeWeight3,gradeWeight4,gradeWeight
public class GradeCalculator extends JFrame implements ActionListener {
Container c;
JTextField gradeWeight1,gradeWeight2,gradeWeight3,gradeWeight4,gradeWeight5,gradeWeight6,
gradeWeight7,gradeWeight8,gradeWeight9,gradeWeight10;
JTextField gradeAch1,gradeAch2,gradeAch3,gradeAch4,gradeAch5,gradeAch6,gradeAch7,
gradeAch8,gradeAch9,gradeAch10;
JLabel score , weight;
JButton btnGPA, btnClear,btnCalc;
JPanel northP, southP;
public void actionPerformed(ActionEvent e) {
if(e.getSource() == btnClear){
gradeAch1.setText(null);
gradeWeight1.setText(null);
gradeAch2.setText(null);
gradeWeight2.setText(null);
gradeAch3.setText(null);
gradeWeight3.setText(null);
gradeAch4.setText(null);
gradeWeight4.setText(null);
gradeAch5.setText(null);
gradeWeight5.setText(null);
gradeAch6.setText(null);
gradeWeight6.setText(null);
gradeAch7.setText(null);
gradeWeight7.setText(null);
gradeAch8.setText(null);
gradeWeight8.setText(null);
gradeAch9.setText(null);
gradeWeight9.setText(null);
gradeAch10.setText(null);
gradeWeight10.setText(null);
}
}
public GradeCalculator(){
super("Grade Calculator");
c = getContentPane();
c.setLayout(null);
JPanel northP = new JPanel();
northP.setLayout(null);
northP.setBorder(BorderFactory.createTitledBorder("Enter your grades"));
northP.setSize(330,460);
northP.setLocation(2,0);
c.add(northP);
JLabel score = new JLabel("Grade you recieved..");
score.setSize(130,20);
score.setLocation(20,30);
northP.add(score);
JLabel weight = new JLabel("Weight of the grade..");
weight.setSize(140,20);
weight.setLocation(190,30);
northP.add(weight);
JButton btnClear = new JButton("New Calculation");
btnClear.setSize(150,20);
btnClear.setLocation(90,530);
btnClear.addActionListener(this);
btnClear.setMnemonic(KeyEvent.VK_N);
c.add(btnClear);
}
public static void main(String[] args) {
GradeCalculator app = new GradeCalculator();
app.setSize(350,600);
app.setDefaultCloseOperation(DISPOSE_ON_CLOSE);
app.setVisible(true);
}
}第一条一般性建议:
- 首先,您的代码要求您使用数组或ArrayList。这将大大简化您的代码,并使其更易于增强、调试和修复
- 不要将文本设置为
,而是设置为null
,即空字符串“”
- 不要设置任何组件的大小或位置,而是使用布局管理器为您设置
- 我在这里猜测,因为您的代码没有向我们显示您的错误,但我怀疑您在错误的引用上调用了
,而jbutton不是您显示的GUI的一部分。监听器代码与显示的GUI代码在不同的类中吗?通过让侦听器代码类扩展GUI,您是否滥用了继承李>setText(…)
- 或者您的代码是否有多个btnClear变量?是否使用“shadow”变量创建按钮,该变量在类字段为空时在构造函数或方法中重新声明
编辑
解决方案:这是我的第二点,您正在隐藏btnClear变量。不要再申报了 e、 g 改为:
public GradeCalculator(){
super("Grade Calculator");
// ... etc...
// **** here ****
btnClear = new JButton("New Calculation");
// .... etc...
}
这一点很重要,通过在构造函数中重新声明变量,您可以创建一个仅在构造函数内部可见的新变量。类中的btnClear字段为null,因为您从未初始化过它。使用“”而不是null。我先尝试使用“”,然后使用null,认为它可能会工作,但我会尝试将其切换回。尝试在包含文本组件的面板上调用
validate()
(清除它们之后)。@Jared:在这种情况下不需要。曾经。我编辑过它,所以它显示了我的更多代码,我删除了我在面板中添加文本字段的地方,以缩短它,但这就是我的代码。@Kalkrin:谢谢,现在我看到了你的错误。你在跟踪一个变量。哈哈,我在哪里跟踪一个变量?哦,好吧,该死,我都没意识到我在这么做。非常感谢你@卡尔克林:见编辑。这段代码还存在很多其他问题,我已经为您概述了很多。
public GradeCalculator(){
super("Grade Calculator");
// ... etc...
// **** here ****
btnClear = new JButton("New Calculation");
// .... etc...
}