簡體   English   中英

ActionListener和按鈕

[英]ActionListener and button

我在使用此Java代碼時遇到麻煩。 我無法使按鈕正常工作,並且在調整按鈕時無法識別我的變量(Test1,Test2等)。 我在此上花了好幾個小時,無論我做了什么更改,我似乎都無法正確解決。 有任何想法嗎?

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;

//Main class
public class gui12{
//Declare variables

public static void main (String args[]){



JFrame frame1;
Container pane;
JButton btnCalculate;
JLabel lblTest1,lblWeight1, lblWeight2, lblWeight3, lblWeight4,        lblTest2, lblTest3, lblTest4;
JTextField txtTest1, txtTest2, txtTest3, txtTest4,  txtWeight1, txtWeight2, txtWeight3, txtWeight4;  
Insets insets;
double Average;

    //Set Look and Feel
    try    {UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());}
    catch (ClassNotFoundException e) {}
    catch (InstantiationException e) {}
    catch (IllegalAccessException e) {}
    catch (UnsupportedLookAndFeelException e) {}

    //Create the frame
    frame1 = new JFrame ("Sample GUI Application");
    frame1.setSize (800,200);
    pane = frame1.getContentPane();
    insets = pane.getInsets();
    pane.setLayout (null);

    //Create controls
    btnCalculate = new JButton ("Calculate");
    lblTest1 = new JLabel ("Test1:");
    lblTest2 = new JLabel ("Test2");
    lblTest3 = new JLabel ("Test3");
    lblTest4 = new JLabel ("Test4");
  lblWeight1 = new JLabel ("Weight1");
  lblWeight2 = new JLabel ("Weight2");
  lblWeight3 = new JLabel ("Weight3");
  lblWeight4 = new JLabel ("Weight4");
    txtTest1 = new JTextField (5);
    txtTest2 = new JTextField  (5);
    txtTest3 = new JTextField  (5);
    txtWeight1 = new JTextField  (5);
  txtWeight2 = new JTextField (5);
    txtWeight3 = new JTextField  (5);
    txtWeight4 = new JTextField  (5);
  txtTest4 = new JTextField (5);



    //Add all components to panel
    pane.add (lblTest1);
    pane.add (lblTest2);
    pane.add (lblTest3);
    pane.add (lblTest4);
    pane.add (txtTest1);
    pane.add (txtTest2);
    pane.add (txtTest3);
    pane.add (txtTest4);
    pane.add (btnCalculate);
  pane.add (lblWeight1);
  pane.add (lblWeight2);
  pane.add (lblWeight3);
  pane.add (lblWeight4);
  pane.add (txtWeight1);
  pane.add (txtWeight2);
  pane.add (txtWeight3);
  pane.add (txtWeight4);

    //Place all components
    lblTest1.setBounds (insets.left + 5, insets.top + 5, lblTest1.getPreferredSize().width, lblTest1.getPreferredSize().height);
    txtTest1.setBounds (lblTest1.getX() + lblTest1.getWidth() + 5, insets.top + 5, txtTest1.getPreferredSize().width, txtTest1.getPreferredSize().height);
    lblTest2.setBounds (txtTest1.getX() + txtTest1.getWidth() + 8, insets.top + 5, lblTest2.getPreferredSize().width, lblTest2.getPreferredSize().height);
    txtTest2.setBounds (lblTest2.getX() + lblTest2.getWidth() + 8, insets.top + 5, txtTest2.getPreferredSize().width, txtTest2.getPreferredSize().height);
    lblTest3.setBounds (txtTest2.getX() + txtTest2.getWidth() + 5, insets.top + 5, lblTest3.getPreferredSize().width, lblTest3.getPreferredSize().height);
    txtTest3.setBounds (lblTest3.getX() + lblTest3.getWidth() + 5, insets.top + 5, txtTest3.getPreferredSize().width, txtTest3.getPreferredSize().height);
    lblTest4.setBounds (txtTest3.getX() + txtTest3.getWidth() + 5, insets.top + 5, lblTest4.getPreferredSize().width, lblTest4.getPreferredSize().height);
    txtTest4.setBounds (lblTest4.getX() + lblTest4.getWidth() + 5, insets.top + 5, txtTest4.getPreferredSize().width, txtTest4.getPreferredSize().height);
    btnCalculate.setBounds (txtTest4.getX() + txtTest4.getWidth() + 5, insets.top + 5, btnCalculate.getPreferredSize().width, btnCalculate.getPreferredSize().height);
    lblWeight1.setBounds (insets.left + 5, insets.top + 30, lblWeight1.getPreferredSize().width, lblWeight1.getPreferredSize().height);
    txtWeight1.setBounds (lblWeight1.getX() + lblWeight1.getWidth() + 5, insets.top + 30, txtWeight1.getPreferredSize().width, txtWeight1.getPreferredSize().height);
  lblWeight2.setBounds (txtWeight1.getX() + txtWeight1.getWidth() + 5, insets.top + 30, txtWeight1.getPreferredSize().width, txtWeight1.getPreferredSize().height);
   txtWeight2.setBounds (lblWeight2.getX() + lblWeight2.getWidth() + 10, insets.top + 30, txtWeight2.getPreferredSize().width, txtWeight2.getPreferredSize().height);
  lblWeight3.setBounds (txtWeight2.getX() + txtWeight2.getWidth() + 10, insets.top + 30, txtWeight2.getPreferredSize().width, txtWeight2.getPreferredSize().height);
   txtWeight3.setBounds (lblWeight3.getX() + lblWeight3.getWidth() + 10, insets.top + 30, txtWeight3.getPreferredSize().width, txtWeight3.getPreferredSize().height);
  lblWeight4.setBounds (txtWeight3.getX() + txtWeight3.getWidth() + 15, insets.top + 30, txtWeight4.getPreferredSize().width, txtWeight3.getPreferredSize().height);
   txtWeight4.setBounds (lblWeight4.getX() + lblWeight4.getWidth() + 15, insets.top + 30, txtWeight4.getPreferredSize().width, txtWeight4.getPreferredSize().height);

    //Set frame visible
    frame1.setVisible (true);

    //Button's action
    btnCalculate.addActionListener(new ActionListener()); //Register action
  double Test1 = Double.parseDouble(txtTest1.getText());
  double Test2 = Double.parseDouble(txtTest2.getText());
  double Test3 = Double.parseDouble(txtTest3.getText());
  double Test4 = Double.parseDouble(txtTest4.getText());
  double Weight1 = Double.parseDouble (txtWeight1.getText());
  double Weight2 = Double.parseDouble(txtWeight2.getText());
  double Weight3 = Double.parseDouble (txtWeight3.getText());
  double Weight4 = Double.parseDouble(txtWeight4.getText());
  }

  public class btnCalculate implements ActionListener{

    public void actionPerformed (ActionEvent e){
        Average = ((Test1*Weight1)+(Test2*Weight2)+(Test3*Weight3)+(Test4*Weight4));
        Average = ((Test1*Weight1)+(Test2*Weight2)+(Test3*Weight3)+(Test4*Weight4));

     System.out.println(Average);

}
}
}

將您的actionPerformed()方法放在偵聽器構造函數旁邊,如下所示:

btnCalculate.addActionListener(new ActionListener() { 
    public void actionPerformed(ActionEvent e) { 
        Average = ((Test1*Weight1)+(Test2*Weight2)+(Test3*Weight3)+(Test4*Weight4));
    } 
} );

您當前的代碼正在創建一個未被使用的名為btnCalculate的新 您想要為Jbutton對象 btnCalculate分配一個偵聽器。

另外,我相信你可以這樣寫:

btnCalculate.addActionListener(new btnCalculate());

但是,這是錯誤的形式,因為您使用相同的名稱來引用兩個單獨的實體。

您的變量不是靜態的,無法調用main方法的外部,不能簡單地調用接口的構造函數,必須在調用它的位置覆蓋它,因此請刪除該類並將其添加到新的ActionListener()所在的位置。

public static void main(String args[]) {

    JFrame frame1;
    Container pane;
    JButton btnCalculate;
    JLabel lblTest1, lblWeight1, lblWeight2, lblWeight3, lblWeight4, lblTest2, lblTest3, lblTest4;
    JTextField txtTest1, txtTest2, txtTest3, txtTest4, txtWeight1, txtWeight2, txtWeight3, txtWeight4;
    Insets insets;
    double Average;

    //Set Look and Feel
    try {
        UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
    } catch (ClassNotFoundException e) {
    } catch (InstantiationException e) {
    } catch (IllegalAccessException e) {
    } catch (UnsupportedLookAndFeelException e) {
    }
    //Create the frame
    frame1 = new JFrame("Sample GUI Application");
    frame1.setSize(800, 200);
    pane = frame1.getContentPane();
    insets = pane.getInsets();
    pane.setLayout(null);

    //Create controls
    btnCalculate = new JButton("Calculate");
    lblTest1 = new JLabel("Test1:");
    lblTest2 = new JLabel("Test2");
    lblTest3 = new JLabel("Test3");
    lblTest4 = new JLabel("Test4");
    lblWeight1 = new JLabel("Weight1");
    lblWeight2 = new JLabel("Weight2");
    lblWeight3 = new JLabel("Weight3");
    lblWeight4 = new JLabel("Weight4");
    txtTest1 = new JTextField(5);
    txtTest2 = new JTextField(5);
    txtTest3 = new JTextField(5);
    txtWeight1 = new JTextField(5);
    txtWeight2 = new JTextField(5);
    txtWeight3 = new JTextField(5);
    txtWeight4 = new JTextField(5);
    txtTest4 = new JTextField(5);

    //Add all components to panel
    pane.add(lblTest1);
    pane.add(lblTest2);
    pane.add(lblTest3);
    pane.add(lblTest4);
    pane.add(txtTest1);
    pane.add(txtTest2);
    pane.add(txtTest3);
    pane.add(txtTest4);
    pane.add(btnCalculate);
    pane.add(lblWeight1);
    pane.add(lblWeight2);
    pane.add(lblWeight3);
    pane.add(lblWeight4);
    pane.add(txtWeight1);
    pane.add(txtWeight2);
    pane.add(txtWeight3);
    pane.add(txtWeight4);

    //Place all components
    lblTest1.setBounds(insets.left + 5, insets.top + 5, lblTest1.getPreferredSize().width, lblTest1.getPreferredSize().height);
    txtTest1.setBounds(lblTest1.getX() + lblTest1.getWidth() + 5, insets.top + 5, txtTest1.getPreferredSize().width, txtTest1.getPreferredSize().height);
    lblTest2.setBounds(txtTest1.getX() + txtTest1.getWidth() + 8, insets.top + 5, lblTest2.getPreferredSize().width, lblTest2.getPreferredSize().height);
    txtTest2.setBounds(lblTest2.getX() + lblTest2.getWidth() + 8, insets.top + 5, txtTest2.getPreferredSize().width, txtTest2.getPreferredSize().height);
    lblTest3.setBounds(txtTest2.getX() + txtTest2.getWidth() + 5, insets.top + 5, lblTest3.getPreferredSize().width, lblTest3.getPreferredSize().height);
    txtTest3.setBounds(lblTest3.getX() + lblTest3.getWidth() + 5, insets.top + 5, txtTest3.getPreferredSize().width, txtTest3.getPreferredSize().height);
    lblTest4.setBounds(txtTest3.getX() + txtTest3.getWidth() + 5, insets.top + 5, lblTest4.getPreferredSize().width, lblTest4.getPreferredSize().height);
    txtTest4.setBounds(lblTest4.getX() + lblTest4.getWidth() + 5, insets.top + 5, txtTest4.getPreferredSize().width, txtTest4.getPreferredSize().height);
    btnCalculate.setBounds(txtTest4.getX() + txtTest4.getWidth() + 5, insets.top + 5, btnCalculate.getPreferredSize().width, btnCalculate.getPreferredSize().height);
    lblWeight1.setBounds(insets.left + 5, insets.top + 30, lblWeight1.getPreferredSize().width, lblWeight1.getPreferredSize().height);
    txtWeight1.setBounds(lblWeight1.getX() + lblWeight1.getWidth() + 5, insets.top + 30, txtWeight1.getPreferredSize().width, txtWeight1.getPreferredSize().height);
    lblWeight2.setBounds(txtWeight1.getX() + txtWeight1.getWidth() + 5, insets.top + 30, txtWeight1.getPreferredSize().width, txtWeight1.getPreferredSize().height);
    txtWeight2.setBounds(lblWeight2.getX() + lblWeight2.getWidth() + 10, insets.top + 30, txtWeight2.getPreferredSize().width, txtWeight2.getPreferredSize().height);
    lblWeight3.setBounds(txtWeight2.getX() + txtWeight2.getWidth() + 10, insets.top + 30, txtWeight2.getPreferredSize().width, txtWeight2.getPreferredSize().height);
    txtWeight3.setBounds(lblWeight3.getX() + lblWeight3.getWidth() + 10, insets.top + 30, txtWeight3.getPreferredSize().width, txtWeight3.getPreferredSize().height);
    lblWeight4.setBounds(txtWeight3.getX() + txtWeight3.getWidth() + 15, insets.top + 30, txtWeight4.getPreferredSize().width, txtWeight3.getPreferredSize().height);
    txtWeight4.setBounds(lblWeight4.getX() + lblWeight4.getWidth() + 15, insets.top + 30, txtWeight4.getPreferredSize().width, txtWeight4.getPreferredSize().height);

    //Set frame visible
    frame1.setVisible(true);

    //Button's action
    btnCalculate.addActionListener(new ActionListener()
    {

        @Override
        public void actionPerformed(ActionEvent e) {
            // you won't have access to your variables here unless they are final, because this is an inner class, I sugest you do not make a gui inside the static main.
        }
    }); //Register action
    double Test1 = Double.parseDouble(txtTest1.getText());
    double Test2 = Double.parseDouble(txtTest2.getText());
    double Test3 = Double.parseDouble(txtTest3.getText());
    double Test4 = Double.parseDouble(txtTest4.getText());
    double Weight1 = Double.parseDouble(txtWeight1.getText());
    double Weight2 = Double.parseDouble(txtWeight2.getText());
    double Weight3 = Double.parseDouble(txtWeight3.getText());
    double Weight4 = Double.parseDouble(txtWeight4.getText());
}

不要忘記static修飾符和接口的基本規則。

就像肖恩·斯科特(Sean Scott)所說。

首先,addActionListener()的參數應該是實現ActionListner的內部類。 同樣,您不應該同時給變量和類起相同的名字。

btnCalculate.addActionListener(new btnCalculate()) // Again, you shouldn't name inner class the same way as JButton variable... 

暫無
暫無

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

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