簡體   English   中英

檢查Java程序中是否按下了按鈕不起作用

[英]Checking if Button is pressed in Java Program not working

我已經建立了一個計算體重指數的程序。 進行計算的方法是bmi_calculator(); 在類BMI_Calculator中 我還構建了一個GUI,但是沒有從面板中拖放元素,但是我自己編寫了代碼。 GUI在類BMI_GUI中

用戶輸入重量和長度,然后單擊“計算”按鈕,結果必須在此處的“文本字段”(jresult)中顯示。 但這是行不通的。 這是我的代碼:

public class BMI_Calculator extends BMI_GUI{
  public void bmi_calculator(){
    try{
    //Text aus dem Textfeld lesen
    String sgewicht= getGewicht().getText();
    String skorpergrosse= getKorpergrosse().getText();
    String salter= getAlter().getText();

    //String in Double umwandeln
    Double gewicht = Double.valueOf(sgewicht);
    Double korpergrosse = Double.valueOf(skorpergrosse);
    Double alter = Double.valueOf(salter);
    //Rechnen Formula
    Double result=gewicht/(korpergrosse*korpergrosse);
    //Ergebniss in String umwandeln
    String sresult= result.toString();
    // Ergbnis in das Textfeld Calculate schreiben
    getResult().setText(sresult); 

    }
   catch(Exception e){

        JOptionPane.showMessageDialog(null, "Das ist ein Error");
    }
}
public void jButtonActionPerformed(){
  if (jButton.getModel().isPressed()) {
           bmi_calculator();
    }  
}
}

另一類是:

public class BMI_GUI extends JFrame {

JPanel jpMain,jpFooter;
JLabel jTitle,jGewicht, jKorpergrosse, jAlter;
private JTextField jgew;
private JTextField jkor;
private JTextField jalt;
private JTextField jresult;
 JButton jButton;
 JButton jButton2;
 JButton jButton3;

 public BMI_GUI(){
         init();
 }
   private void init(){
    //Fenstergrosse bestimmen
    this.setSize(500,400);
    //fenster in der mitte darstellen
    this.setLocationRelativeTo(null);
    //fenster schliessen mit X
    this.setDefaultCloseOperation(EXIT_ON_CLOSE);
    // fenster title festlegen
    this.setTitle("Body Mass Index");
    //verhinder dass benutzer fenster grosser kleiner machen kann
    this.setResizable(false);

    jpMain=new JPanel();
    jpMain.setLayout(null);

    //than comes the code per JLabels and JTextFields for weight,
    // height and age

    jButton=new JButton("Calculate");
    jButton.setLocation(130,210);
    jButton.setSize(110,20);

    jresult=new JTextField();
    jresult.setSize(60,20);
    jresult.setLocation(270,210);

    //Komponenten dem Panel hinzufugen
     jpMain.add(jTitle); 
     jpMain.add(jGewicht); 
     jpMain.add(jKorpergrosse);
     jpMain.add(jAlter);
     jpMain.add(jgew);
     jpMain.add(jkor);
     jpMain.add(jalt);
     jpMain.add(jresult);
     jpMain.add(jButton);
     jpMain.add(jButton2);
     jpMain.add(jButton3);


    //panels zum fenster hinzufugen
    this.add(jpMain);
    //this.add(jpFooter);

}
   //Set- und GET Methode

    public JTextField getGewicht(){
    return this.jgew;
}
    public void setGewicht(JTextField jgew){
    this.jgew=jgew;
}
    public JTextField getKorpergrosse(){
    return this.jkor;
}
    public void setKorpergrosse(JTextField jkor){
    this.jkor=jkor;
}
    public JTextField getAlter(){
    return this.jalt;
}
    public void setAlter(JTextField jalt){
    this.jalt=jalt;
}
    public JTextField getResult(){
    return this.jresult;
}
    public void setResult(JTextField jresult){
    this.jresult=jresult;
}

      public static void main(String [] args){
            new BMI_GUI().setVisible(true);

}


}

您沒有在JButton上注冊ActionListener

jButton.addActionListener(new ActionListener() {
  public void actionPerformed(ActionEvent ae) {
    System.out.println("guten tag");
  }
});

(您無需檢查JButton模型來檢查是否單擊了按鈕。如果單擊了按鈕,則將調用此方法。)

另請參見以下鏈接: 如何在Java中將ActionListener添加到JButton上

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM