ActionListener和按钮

时间:2016-05-26 17:10:22

标签: java actionlistener

我遇到了这个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);

}
}
}

3 个答案:

答案 0 :(得分:0)

将您的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());

但是,这是不好的形式,因为您使用相同的名称来引用两个单独的实体。

答案 1 :(得分:0)

你的变量不是静态的你不能调用外部的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());
}

不要忘记静态修饰符和接口的基本规则。

答案 2 :(得分:0)

就像肖恩斯科特说的那样。

首先,addActionListener()的参数应该是实现ActionListner的内部类。 此外,您不应同时为变量和类赋予相同的名称。

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