Java GUI按钮不会初始化

Java GUI按钮不会初始化,java,Java,因此,我正在创建一个程序,它的作用类似于一个计算器,用于销售特定商品。我有一个计算按钮,但按下该按钮不会初始化其中的代码。我正在为gui使用Swing designer。这是带按钮的代码 JButton btnNewButton = new JButton("Calculate Total"); btnNewButton.setFont(new Font("Tahoma", Font.PLAIN, 43)); btnNewButton.setBounds(37, 1113, 33

因此,我正在创建一个程序,它的作用类似于一个计算器,用于销售特定商品。我有一个计算按钮,但按下该按钮不会初始化其中的代码。我正在为gui使用Swing designer。这是带按钮的代码

JButton btnNewButton = new JButton("Calculate Total");
    btnNewButton.setFont(new Font("Tahoma", Font.PLAIN, 43));
    btnNewButton.setBounds(37, 1113, 330, 77);
    frame.getContentPane().add(btnNewButton);

    if(btnNewButton.getModel().isPressed())
    {

        // Sets value for # of gloves per type
        LA = Integer.parseInt(textField_1.getText());
        LB = Integer.parseInt(textField_8.getText());
        SA = Integer.parseInt(textField_12.getText());
        SB = Integer.parseInt(textField_13.getText());
        total = LA + LB + SA + SB;

        // Sets value for percentage of gloves compared to total 
        percLA = LA / total;
        percLB = LB / total;
        percSA = SA / total;
        percSB = SB / total;
        double percTotal = percLA + percLB + percSA + percSB;

        //21;
        // Sets value for total # of gloves
        textField_22.setText(Double.toString(total));

        // Sets value for percentage 
        textField_2.setText(Double.toString(percLA));
        textField_7.setText(Double.toString(percLB));
        textField_14.setText(Double.toString(percSA));
        textField_15.setText(Double.toString(percSB));
        textField_21.setText(Double.toString(percTotal));

        // Sets value for cost per pair
        costLA = Integer.parseInt(textField_3.getText());
        costLB = Integer.parseInt(textField_6.getText());
        costSA = Integer.parseInt(textField_16.getText());
        costSB = Integer.parseInt(textField_17.getText());  
    } 
如果需要,这里有完整的代码

import java.awt.EventQueue;

import javax.swing.JFrame;
import javax.swing.Box;
import javax.swing.JTextField;
import java.awt.BorderLayout;
import java.awt.Font;
import javax.swing.JLabel;
import javax.swing.JSlider;
import javax.swing.JTextArea;
import javax.swing.JButton;

public class Program {

private JFrame frame;
private JTextField textField_1;
private JTextField textField_2;
private JTextField textField_3;
private JTextField textField_4;
private JTextField textField_5;
private JTextField textField_6;
private JTextField textField_7;
private JTextField textField_8;
private JTextField textField_12;
private JTextField textField_13;
private JTextField textField_14;
private JTextField textField_15;
private JTextField textField_16;
private JTextField textField_17;
private JTextField textField_18;
private JTextField textField_19;
private JTextField textField_20;
private JTextField textField_21;
private JTextField textField_22;

/**
 * Launch the application.
 */
public static void main(String[] args) {
    EventQueue.invokeLater(new Runnable() {
        public void run() {
            try {
                Program window = new Program();
                window.frame.setVisible(true);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    });
}

/**
 * Create the application.
 */
public Program() {
    initialize();
}

/**
 * Initialize the contents of the frame.
 */
private void initialize() {
    frame = new JFrame();
    frame.setBounds(100, 100, 1109, 1400);
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.getContentPane().setLayout(null);

    // Total # of pairs per glove
    double LA = 0;
    double LB = 0;
    double SA = 0;
    double SB = 0;
    double total = 0;

    // Percentage of gloves compared to total
    double percLA = 0;
    double percLB = 0;
    double percSA = 0;
    double percSB = 0;

    // Cost per glove type
    double costLA = 0;
    double costLB = 0;
    double costSA = 0;
    double costSB = 0;


    textField_1 = new JTextField();
    textField_1.setFont(new Font("Tahoma", Font.PLAIN, 40));
    textField_1.setColumns(10);
    textField_1.setBounds(231, 74, 200, 64);
    frame.getContentPane().add(textField_1);


    textField_2 = new JTextField();
    textField_2.setEditable(false);
    textField_2.setFont(new Font("Tahoma", Font.PLAIN, 40));
    textField_2.setColumns(10);
    textField_2.setBounds(440, 74, 200, 64);
    frame.getContentPane().add(textField_2);


    textField_3 = new JTextField();
    textField_3.setFont(new Font("Tahoma", Font.PLAIN, 40));
    textField_3.setColumns(10);
    textField_3.setBounds(650, 74, 200, 64);
    frame.getContentPane().add(textField_3);

    textField_4 = new JTextField();
    textField_4.setEditable(false);
    textField_4.setFont(new Font("Tahoma", Font.PLAIN, 40));
    textField_4.setColumns(10);
    textField_4.setBounds(860, 74, 200, 64);
    frame.getContentPane().add(textField_4);

    textField_5 = new JTextField();
    textField_5.setEditable(false);
    textField_5.setFont(new Font("Tahoma", Font.PLAIN, 40));
    textField_5.setColumns(10);
    textField_5.setBounds(860, 147, 200, 64);
    frame.getContentPane().add(textField_5);

    textField_6 = new JTextField();
    textField_6.setFont(new Font("Tahoma", Font.PLAIN, 40));
    textField_6.setColumns(10);
    textField_6.setBounds(650, 147, 200, 64);
    frame.getContentPane().add(textField_6);

    textField_7 = new JTextField();
    textField_7.setEditable(false);
    textField_7.setFont(new Font("Tahoma", Font.PLAIN, 40));
    textField_7.setColumns(10);
    textField_7.setBounds(440, 147, 200, 64);
    frame.getContentPane().add(textField_7);

    textField_8 = new JTextField();
    textField_8.setFont(new Font("Tahoma", Font.PLAIN, 40));
    textField_8.setColumns(10);
    textField_8.setBounds(231, 147, 200, 64);
    frame.getContentPane().add(textField_8);

    textField_12 = new JTextField();
    textField_12.setFont(new Font("Tahoma", Font.PLAIN, 40));
    textField_12.setColumns(10);
    textField_12.setBounds(231, 219, 200, 64);
    frame.getContentPane().add(textField_12);

    textField_13 = new JTextField();
    textField_13.setFont(new Font("Tahoma", Font.PLAIN, 40));
    textField_13.setColumns(10);
    textField_13.setBounds(231, 293, 200, 64);
    frame.getContentPane().add(textField_13);

    textField_14 = new JTextField();
    textField_14.setEditable(false);
    textField_14.setFont(new Font("Tahoma", Font.PLAIN, 40));
    textField_14.setColumns(10);
    textField_14.setBounds(440, 219, 200, 64);
    frame.getContentPane().add(textField_14);

    textField_15 = new JTextField();
    textField_15.setEditable(false);
    textField_15.setFont(new Font("Tahoma", Font.PLAIN, 40));
    textField_15.setColumns(10);
    textField_15.setBounds(440, 293, 200, 64);
    frame.getContentPane().add(textField_15);

    textField_16 = new JTextField();
    textField_16.setFont(new Font("Tahoma", Font.PLAIN, 40));
    textField_16.setColumns(10);
    textField_16.setBounds(650, 219, 200, 64);
    frame.getContentPane().add(textField_16);

    textField_17 = new JTextField();
    textField_17.setFont(new Font("Tahoma", Font.PLAIN, 40));
    textField_17.setColumns(10);
    textField_17.setBounds(650, 293, 200, 64);
    frame.getContentPane().add(textField_17);

    textField_18 = new JTextField();
    textField_18.setEditable(false);
    textField_18.setFont(new Font("Tahoma", Font.PLAIN, 40));
    textField_18.setColumns(10);
    textField_18.setBounds(860, 219, 200, 64);
    frame.getContentPane().add(textField_18);


    textField_19 = new JTextField();
    textField_19.setEditable(false);
    textField_19.setFont(new Font("Tahoma", Font.PLAIN, 40));
    textField_19.setColumns(10);
    textField_19.setBounds(860, 293, 200, 64);
    frame.getContentPane().add(textField_19);

    textField_20 = new JTextField();
    textField_20.setEditable(false);
    textField_20.setFont(new Font("Tahoma", Font.PLAIN, 40));
    textField_20.setColumns(10);
    textField_20.setBounds(860, 386, 200, 64);
    frame.getContentPane().add(textField_20);

    textField_21 = new JTextField();
    textField_21.setEditable(false);
    textField_21.setFont(new Font("Tahoma", Font.PLAIN, 40));
    textField_21.setColumns(10);
    textField_21.setBounds(440, 386, 200, 64);
    frame.getContentPane().add(textField_21);

    textField_22 = new JTextField();
    textField_22.setEditable(false);
    textField_22.setFont(new Font("Tahoma", Font.PLAIN, 40));
    textField_22.setColumns(10);
    textField_22.setBounds(231, 386, 200, 64);
    frame.getContentPane().add(textField_22);

    Box horizontalBox = Box.createHorizontalBox();
    horizontalBox.setBounds(213, 74, -187, 64);
    frame.getContentPane().add(horizontalBox);

    Box horizontalBox_1 = Box.createHorizontalBox();
    horizontalBox_1.setBounds(22, 74, 200, 64);
    frame.getContentPane().add(horizontalBox_1);

    JLabel lblTest = new JLabel("Latex A");
    lblTest.setFont(new Font("Tahoma", Font.PLAIN, 35));
    horizontalBox_1.add(lblTest);

    Box horizontalBox_2 = Box.createHorizontalBox();
    horizontalBox_2.setBounds(22, 147, 200, 64);
    frame.getContentPane().add(horizontalBox_2);

    JLabel lblLatexB = new JLabel("Latex B");
    lblLatexB.setFont(new Font("Tahoma", Font.PLAIN, 35));
    horizontalBox_2.add(lblLatexB);

    Box horizontalBox_3 = Box.createHorizontalBox();
    horizontalBox_3.setBounds(22, 219, 200, 64);
    frame.getContentPane().add(horizontalBox_3);

    JLabel lblSyntheticA = new JLabel("Synthetic A");
    lblSyntheticA.setFont(new Font("Tahoma", Font.PLAIN, 35));
    horizontalBox_3.add(lblSyntheticA);

    Box horizontalBox_4 = Box.createHorizontalBox();
    horizontalBox_4.setBounds(22, 293, 200, 64);
    frame.getContentPane().add(horizontalBox_4);

    JLabel lblSyntheticB = new JLabel("Synthetic B");
    lblSyntheticB.setFont(new Font("Tahoma", Font.PLAIN, 35));
    horizontalBox_4.add(lblSyntheticB);

    Box horizontalBox_5 = Box.createHorizontalBox();
    horizontalBox_5.setBounds(231, 0, 200, 64);
    frame.getContentPane().add(horizontalBox_5);

    JLabel lblPairsYear = new JLabel("Pairs / Year");
    lblPairsYear.setFont(new Font("Tahoma", Font.PLAIN, 35));
    horizontalBox_5.add(lblPairsYear);

    Box horizontalBox_6 = Box.createHorizontalBox();
    horizontalBox_6.setBounds(440, 0, 200, 64);
    frame.getContentPane().add(horizontalBox_6);

    JLabel lblOfTotal = new JLabel("% Of Total");
    lblOfTotal.setFont(new Font("Tahoma", Font.PLAIN, 35));
    horizontalBox_6.add(lblOfTotal);

    Box horizontalBox_7 = Box.createHorizontalBox();
    horizontalBox_7.setBounds(650, 0, 200, 64);
    frame.getContentPane().add(horizontalBox_7);

    JLabel lblPricePerPair = new JLabel("Price Per Pair");
    lblPricePerPair.setFont(new Font("Tahoma", Font.PLAIN, 33));
    horizontalBox_7.add(lblPricePerPair);

    Box horizontalBox_8 = Box.createHorizontalBox();
    horizontalBox_8.setBounds(860, 0, 200, 64);
    frame.getContentPane().add(horizontalBox_8);

    JLabel lblTotalSpent = new JLabel("Total Spent");
    lblTotalSpent.setFont(new Font("Tahoma", Font.PLAIN, 35));
    horizontalBox_8.add(lblTotalSpent);

    JSlider slider = new JSlider();
    slider.setToolTipText("h");
    slider.setPaintTicks(true);
    slider.setPaintLabels(true);
    slider.setFont(new Font("Tahoma", Font.PLAIN, 72));
    slider.setBounds(39, 592, 1021, 88);
    frame.getContentPane().add(slider);

    JLabel lblAverageSalePrice = new JLabel("Average Sale Price");
    lblAverageSalePrice.setFont(new Font("Tahoma", Font.PLAIN, 35));
    lblAverageSalePrice.setBounds(39, 536, 384, 43);
    frame.getContentPane().add(lblAverageSalePrice);

    JTextArea textArea = new JTextArea();
    textArea.setFont(new Font("Monospaced", Font.PLAIN, 43));
    textArea.setBounds(468, 525, 172, 61);
    frame.getContentPane().add(textArea);

    JTextArea textArea_1 = new JTextArea();
    textArea_1.setFont(new Font("Monospaced", Font.PLAIN, 43));
    textArea_1.setBounds(468, 714, 172, 61);
    frame.getContentPane().add(textArea_1);

    JLabel lblTotalNumberOf = new JLabel("Total Number Of Pairs");
    lblTotalNumberOf.setFont(new Font("Tahoma", Font.PLAIN, 35));
    lblTotalNumberOf.setBounds(39, 725, 384, 43);
    frame.getContentPane().add(lblTotalNumberOf);

    JSlider slider_1 = new JSlider();
    slider_1.setToolTipText("h");
    slider_1.setPaintTicks(true);
    slider_1.setPaintLabels(true);
    slider_1.setFont(new Font("Tahoma", Font.PLAIN, 72));
    slider_1.setBounds(39, 783, 1021, 88);
    frame.getContentPane().add(slider_1);

    JTextArea textArea_2 = new JTextArea();
    textArea_2.setFont(new Font("Monospaced", Font.PLAIN, 43));
    textArea_2.setBounds(468, 895, 172, 61);
    frame.getContentPane().add(textArea_2);

    JLabel lblPriceReduction = new JLabel("Price Reduction");
    lblPriceReduction.setFont(new Font("Tahoma", Font.PLAIN, 35));
    lblPriceReduction.setBounds(39, 917, 384, 43);
    frame.getContentPane().add(lblPriceReduction);

    JSlider slider_2 = new JSlider();
    slider_2.setToolTipText("h");
    slider_2.setPaintTicks(true);
    slider_2.setPaintLabels(true);
    slider_2.setFont(new Font("Tahoma", Font.PLAIN, 72));
    slider_2.setBounds(39, 968, 1021, 88);
    frame.getContentPane().add(slider_2);

    JTextArea textArea_3 = new JTextArea();
    textArea_3.setEditable(false);
    textArea_3.setFont(new Font("Monospaced", Font.PLAIN, 43));
    textArea_3.setBounds(417, 1129, 292, 61);
    frame.getContentPane().add(textArea_3);

    JButton btnNewButton = new JButton("Calculate Total");
    btnNewButton.setFont(new Font("Tahoma", Font.PLAIN, 43));
    btnNewButton.setBounds(37, 1113, 330, 77);
    frame.getContentPane().add(btnNewButton);

    if(btnNewButton.getModel().isPressed())
    {

        // Sets value for # of gloves per type
        LA = Integer.parseInt(textField_1.getText());
        LB = Integer.parseInt(textField_8.getText());
        SA = Integer.parseInt(textField_12.getText());
        SB = Integer.parseInt(textField_13.getText());
        total = LA + LB + SA + SB;

        // Sets value for percentage of gloves compared to total 
        percLA = LA / total;
        percLB = LB / total;
        percSA = SA / total;
        percSB = SB / total;
        double percTotal = percLA + percLB + percSA + percSB;

        //21;
        // Sets value for total # of gloves
        textField_22.setText(Double.toString(total));

        // Sets value for percentage 
        textField_2.setText(Double.toString(percLA));
        textField_7.setText(Double.toString(percLB));
        textField_14.setText(Double.toString(percSA));
        textField_15.setText(Double.toString(percSB));
        textField_21.setText(Double.toString(percTotal));

        // Sets value for cost per pair
        costLA = Integer.parseInt(textField_3.getText());
        costLB = Integer.parseInt(textField_6.getText());
        costSA = Integer.parseInt(textField_16.getText());
        costSB = Integer.parseInt(textField_17.getText());






    }
}
}

更新:为侦听器创建了一个匿名类,但我的代码不会针对我放置按钮要执行的代码的位置进行初始化。这是密码

    JTextArea textArea_2 = new JTextArea();
    textArea_2.setFont(new Font("Monospaced", Font.PLAIN, 43));
    textArea_2.setBounds(468, 895, 172, 61);
    frame.getContentPane().add(textArea_2);

    JLabel lblPriceReduction = new JLabel("Price Reduction");
    lblPriceReduction.setFont(new Font("Tahoma", Font.PLAIN, 35));
    lblPriceReduction.setBounds(39, 917, 384, 43);
    frame.getContentPane().add(lblPriceReduction);

    JSlider slider_2 = new JSlider();
    slider_2.setToolTipText("h");
    slider_2.setPaintTicks(true);
    slider_2.setPaintLabels(true);
    slider_2.setFont(new Font("Tahoma", Font.PLAIN, 72));
    slider_2.setBounds(39, 968, 1021, 88);
    frame.getContentPane().add(slider_2);

    JTextArea textArea_3 = new JTextArea();
    textArea_3.setEditable(false);
    textArea_3.setFont(new Font("Monospaced", Font.PLAIN, 43));
    textArea_3.setBounds(417, 1129, 292, 61);
    frame.getContentPane().add(textArea_3);


    JButton btnNewButton = new JButton("Calculate Total");
    btnNewButton.setFont(new Font("Tahoma", Font.PLAIN, 43));
    btnNewButton.setBounds(37, 1113, 330, 77);
    frame.getContentPane().add(btnNewButton);
    btnNewButton.addActionListener(new Action());

}

    static class Action implements ActionListener {

        public void actionPerformed (ActionEvent e){
            textArea_1.setText("This is a test"); 
        }
    }


}

然而,在我键入//试图将代码放在这里的地方,我不能调用例如textfield_3.setText();。不会初始化任何内容

您必须将actionListener添加到JButton并实现该接口的actionPerformed方法。请看一下,你会得到一些提示。您的代码只是没有附加到按钮。

我不确定真正的问题是什么,但我建议您在类中实现ActionListener,因此您必须覆盖
ActionPerformed
方法,而不是只使用匿名类

例如:

public class Programs implements ActionListener{

    // When you create your button
    JButton btnNewButton = new JButton("Calculate Total");
    btnNewButton.setFont(new Font("Tahoma", Font.PLAIN, 43));
    // This line tells the button where the actionListener will be 
    // handlded
    btnNewButton.addActionListener(this);



   //all your code...

   // I will catch all the actions you send me
   public void actionPerformed(ActionEvent e){
     // Here goes the name of the instance, remember that.
     if(e.getSource(btnNewButton)){
        //code to execute when you click the button ...
     }
   }

}
第二种选择是只添加一个匿名类,我不建议这样做,因为代码可能会变得有点脏

btnNewButton.addActionListener(new ActionListener(){
      public void actionPerformed(ActionEvent e)
      {
        // Code to execute when clicked
      }
    });
在本例中,您不需要实现ActionListener接口,只需创建一个匿名类并在内部分配一个方法


祝你好运,如果你有任何问题,我会在这里。

在swing中点击按钮的方法是使用ActionListener
if(btnNewButton.getModel().isPressed()){
这在实践中永远不会是真的,因为它是在按钮可见之前执行的,这是有意义的,多亏了你们俩^。我会研究使用ActionListener。实际上我有一个问题,请参阅我文章的更新部分。我更新了我的编辑,在我的listener类中写了一行关于textArea_1的内容,它在一个错误中说“无法初始化textArea_1。”编辑:将textArea更改为静态,现在它可以工作了。我知道问题是什么,因为swing组件是在您的方法中声明的Java不确定它是否会被实例化,所以它要求您将其设置为静态,这样它就不需要实例。也许您应该在方法之外声明它。注意:静态可以工作,祝您好运。