如何禁用 JButton
How to Disable JButton
我希望在所有字段都有输入之前禁用我的 JButton。
package Grade;
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class GradeGUI extends JFrame {
private JLabel quizL,exerL,partL,partLL,examL,finalSL,totalIL,totalL,blankL,blankL2,blankL3,blankL4,blankL5,blankL6,blankL7,blankL8;
private JTextField quizTF,exerTF,examTF,partTF,quizTF1,exerTF1,examTF1,total1,total2,total3,total4,totalF,letterTF;
private JButton calculateB,exitB;
private CalculateButtonHandler cbHandler;
private ExitButtonHandler exitHandler;
public GradeGUI(){
blankL8=new JLabel("");
blankL7=new JLabel("");
blankL6=new JLabel("");
blankL5=new JLabel("");
blankL4=new JLabel("");
blankL3=new JLabel("");
blankL2=new JLabel("");
blankL=new JLabel("");
partLL=new JLabel("");
quizL=new JLabel("Quiz: ");
exerL=new JLabel("Exercise: ");
partL=new JLabel("Participation: ");
examL=new JLabel("Exams: ");
finalSL=new JLabel("Final Score");
totalIL=new JLabel("Total # of Items");
totalL=new JLabel("Final Score");
letterTF=new JTextField(25);
quizTF=new JTextField(25);
exerTF=new JTextField(25);
examTF=new JTextField(25);
partTF=new JTextField(25);
quizTF1=new JTextField(25);
exerTF1=new JTextField(25);
examTF1=new JTextField(25);
total1=new JTextField(25);
total2=new JTextField(25);
total3=new JTextField(25);
total4=new JTextField(25);
totalF=new JTextField(25);
total1.setEditable(false);
total2.setEditable(false);
total3.setEditable(false);
total4.setEditable(false);
totalF.setEditable(false);
letterTF.setEditable(false);
calculateB=new JButton("Calculate");
exitB=new JButton("Exit");
calculateB=new JButton("Calculate");
cbHandler= new CalculateButtonHandler();
calculateB.addActionListener(cbHandler);
exitB=new JButton("Exit");
exitHandler= new ExitButtonHandler();
exitB.addActionListener(exitHandler);
setTitle("Grade Calculator");
Container p = getContentPane();
p.setLayout(new GridLayout(6,5));
p.add(blankL);
p.add(finalSL);
p.add(totalIL);
p.add(totalL);
p.add(blankL4);
p.add(quizL);
p.add(quizTF);
p.add(quizTF1);
p.add(total1);
p.add(blankL5);
p.add(exerL);
p.add(exerTF);
p.add(exerTF1);
p.add(total2);
p.add(blankL6);
p.add(partL);
p.add(partTF);
p.add(blankL2);
p.add(total3);
p.add(blankL7);
p.add(examL);
p.add(examTF);
p.add(examTF1);
p.add(total4);
p.add(blankL8);
p.add(blankL3);
p.add(calculateB);
p.add(exitB);
p.add(totalF);
p.add(letterTF);
setSize(500,400);
setVisible(true);
setDefaultCloseOperation(EXIT_ON_CLOSE);
}
private class CalculateButtonHandler implements ActionListener{
public void actionPerformed(ActionEvent e){
try{
double quiz,exam,exer,part,quizT,examT,exerT;
double total11,total22,total33,total44,totalFf;
double quizTT,examTT,partTT,exerTT;
quiz=Integer.parseInt(quizTF.getText());
exam=Integer.parseInt(examTF.getText());
exer=Integer.parseInt(exerTF.getText());
part=Integer.parseInt(partTF.getText());
quizT=Integer.parseInt(quizTF1.getText());
examT=Integer.parseInt(examTF1.getText());
exerT=Integer.parseInt(exerTF1.getText());
quizTT=((quiz/quizT)*100)*0.20;
examTT=((exam/examT)*100)*0.30;
partTT=part*0.10;
exerTT=((exer/exerT)*100)*0.40;
total1.setText(""+quizTT);
total2.setText(""+examTT);
total3.setText(""+partTT);
total4.setText(""+exerTT);
total11=Double.parseDouble(total1.getText());
total22=Double.parseDouble(total2.getText());
total33=Double.parseDouble(total3.getText());
total44=Double.parseDouble(total4.getText());
totalFf=total11+total22+total33+total44;
totalF.setText(""+totalFf);
if(totalFf>=95){
letterTF.setText("A");
}else if(totalFf>=90){
letterTF.setText("B");
}else if(totalFf>=85){
letterTF.setText("C");
}else if(totalFf>=80){
letterTF.setText("D");
}else if(totalFf>=75){
letterTF.setText("E");
}else{
letterTF.setText("F");
}
}catch(Exception s){
JOptionPane.showMessageDialog(null,"Please Try Again!!","ERROR",JOptionPane.ERROR_MESSAGE);
}
}
}
private class ExitButtonHandler implements ActionListener{
public void actionPerformed(ActionEvent e){
System.exit(0);
}
}
public static void main(String[]args){
GradeGUI a=new GradeGUI();
}
}
使用 JTextField
class 的 addCaretListener
方法,您可以检查每个文本字段中的更改。当在文本字段中输入字符时,插入符号的位置会发生变化,并且会触发 javax.swing.event.CaretEvent
,您可以使用它来检查每个文本字段是否为空;基于此,您可以将按钮设置为启用或禁用。例如:
quizTF.addCaretListener(new CaretListener() {
public void caretUpdate(CaretEvent ev) {
calculateB.setEnabled(!quizTF.getText().isEmpty() && !exerTF.getText().isEmpty() && ...);
}
});
// code here
就个人而言,我会将所有 JTextField 对象放在 ArrayList
或类似的东西中,这样您就可以遍历它们并对其执行操作,并且编写这么多代码不会乏味。
我希望在所有字段都有输入之前禁用我的 JButton。
package Grade;
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class GradeGUI extends JFrame {
private JLabel quizL,exerL,partL,partLL,examL,finalSL,totalIL,totalL,blankL,blankL2,blankL3,blankL4,blankL5,blankL6,blankL7,blankL8;
private JTextField quizTF,exerTF,examTF,partTF,quizTF1,exerTF1,examTF1,total1,total2,total3,total4,totalF,letterTF;
private JButton calculateB,exitB;
private CalculateButtonHandler cbHandler;
private ExitButtonHandler exitHandler;
public GradeGUI(){
blankL8=new JLabel("");
blankL7=new JLabel("");
blankL6=new JLabel("");
blankL5=new JLabel("");
blankL4=new JLabel("");
blankL3=new JLabel("");
blankL2=new JLabel("");
blankL=new JLabel("");
partLL=new JLabel("");
quizL=new JLabel("Quiz: ");
exerL=new JLabel("Exercise: ");
partL=new JLabel("Participation: ");
examL=new JLabel("Exams: ");
finalSL=new JLabel("Final Score");
totalIL=new JLabel("Total # of Items");
totalL=new JLabel("Final Score");
letterTF=new JTextField(25);
quizTF=new JTextField(25);
exerTF=new JTextField(25);
examTF=new JTextField(25);
partTF=new JTextField(25);
quizTF1=new JTextField(25);
exerTF1=new JTextField(25);
examTF1=new JTextField(25);
total1=new JTextField(25);
total2=new JTextField(25);
total3=new JTextField(25);
total4=new JTextField(25);
totalF=new JTextField(25);
total1.setEditable(false);
total2.setEditable(false);
total3.setEditable(false);
total4.setEditable(false);
totalF.setEditable(false);
letterTF.setEditable(false);
calculateB=new JButton("Calculate");
exitB=new JButton("Exit");
calculateB=new JButton("Calculate");
cbHandler= new CalculateButtonHandler();
calculateB.addActionListener(cbHandler);
exitB=new JButton("Exit");
exitHandler= new ExitButtonHandler();
exitB.addActionListener(exitHandler);
setTitle("Grade Calculator");
Container p = getContentPane();
p.setLayout(new GridLayout(6,5));
p.add(blankL);
p.add(finalSL);
p.add(totalIL);
p.add(totalL);
p.add(blankL4);
p.add(quizL);
p.add(quizTF);
p.add(quizTF1);
p.add(total1);
p.add(blankL5);
p.add(exerL);
p.add(exerTF);
p.add(exerTF1);
p.add(total2);
p.add(blankL6);
p.add(partL);
p.add(partTF);
p.add(blankL2);
p.add(total3);
p.add(blankL7);
p.add(examL);
p.add(examTF);
p.add(examTF1);
p.add(total4);
p.add(blankL8);
p.add(blankL3);
p.add(calculateB);
p.add(exitB);
p.add(totalF);
p.add(letterTF);
setSize(500,400);
setVisible(true);
setDefaultCloseOperation(EXIT_ON_CLOSE);
}
private class CalculateButtonHandler implements ActionListener{
public void actionPerformed(ActionEvent e){
try{
double quiz,exam,exer,part,quizT,examT,exerT;
double total11,total22,total33,total44,totalFf;
double quizTT,examTT,partTT,exerTT;
quiz=Integer.parseInt(quizTF.getText());
exam=Integer.parseInt(examTF.getText());
exer=Integer.parseInt(exerTF.getText());
part=Integer.parseInt(partTF.getText());
quizT=Integer.parseInt(quizTF1.getText());
examT=Integer.parseInt(examTF1.getText());
exerT=Integer.parseInt(exerTF1.getText());
quizTT=((quiz/quizT)*100)*0.20;
examTT=((exam/examT)*100)*0.30;
partTT=part*0.10;
exerTT=((exer/exerT)*100)*0.40;
total1.setText(""+quizTT);
total2.setText(""+examTT);
total3.setText(""+partTT);
total4.setText(""+exerTT);
total11=Double.parseDouble(total1.getText());
total22=Double.parseDouble(total2.getText());
total33=Double.parseDouble(total3.getText());
total44=Double.parseDouble(total4.getText());
totalFf=total11+total22+total33+total44;
totalF.setText(""+totalFf);
if(totalFf>=95){
letterTF.setText("A");
}else if(totalFf>=90){
letterTF.setText("B");
}else if(totalFf>=85){
letterTF.setText("C");
}else if(totalFf>=80){
letterTF.setText("D");
}else if(totalFf>=75){
letterTF.setText("E");
}else{
letterTF.setText("F");
}
}catch(Exception s){
JOptionPane.showMessageDialog(null,"Please Try Again!!","ERROR",JOptionPane.ERROR_MESSAGE);
}
}
}
private class ExitButtonHandler implements ActionListener{
public void actionPerformed(ActionEvent e){
System.exit(0);
}
}
public static void main(String[]args){
GradeGUI a=new GradeGUI();
}
}
使用 JTextField
class 的 addCaretListener
方法,您可以检查每个文本字段中的更改。当在文本字段中输入字符时,插入符号的位置会发生变化,并且会触发 javax.swing.event.CaretEvent
,您可以使用它来检查每个文本字段是否为空;基于此,您可以将按钮设置为启用或禁用。例如:
quizTF.addCaretListener(new CaretListener() {
public void caretUpdate(CaretEvent ev) {
calculateB.setEnabled(!quizTF.getText().isEmpty() && !exerTF.getText().isEmpty() && ...);
}
});
// code here
就个人而言,我会将所有 JTextField 对象放在 ArrayList
或类似的东西中,这样您就可以遍历它们并对其执行操作,并且编写这么多代码不会乏味。