Java 为什么我的JButton即使在第一次单击时也没有在JTextFrame上显示输出?

Java 为什么我的JButton即使在第一次单击时也没有在JTextFrame上显示输出?,java,swing,jbutton,actionlistener,Java,Swing,Jbutton,Actionlistener,在这里,我试图创建一个计算器,正如你所看到的,我无法为我在上面的操作编写上帝代码。 我想在我的行动上得到帮助,比如:- 1). 当我点击按钮时,它(写在上面的符号数)将出现在JTextField(tf)中, 2). 当输出出现在JTextField上时,它应该是这样的:-当我按下1,然后按下1,紧接着按下4,它变成14 package p2; import java.awt.Color; import java.awt.Font; import java.awt.event.*; import

在这里,我试图创建一个计算器,正如你所看到的,我无法为我在上面的操作编写上帝代码。 我想在我的行动上得到帮助,比如:- 1). 当我点击按钮时,它(写在上面的符号数)将出现在JTextField(tf)中, 2). 当输出出现在JTextField上时,它应该是这样的:-当我按下1,然后按下1,紧接着按下4,它变成14

package p2;
import java.awt.Color;
import java.awt.Font;
import java.awt.event.*;
import javax.swing.*;
public class Gui extends WindowAdapter implements ActionListener {
    JFrame fr;
    JPanel p1,p2;
    JLabel l1;
    JTextField tf;
    JButton b1,b2,b3,b4,b5,b6,b7,b8,b9;
    JButton b00,b11,b22,b33,b44,b55,b66,b77,b88,b99;
    public Gui(){
    //Frame 
        fr=new JFrame("Calculator");
        fr.getContentPane().setBackground(Color.LIGHT_GRAY);
        //fr.setBackground(Color.black);
    //Panel
        p1=new  JPanel();
        p2=new  JPanel();
        p1.setBounds(5, 5, 375, 90);
        p1.setBackground(Color.gray);
        p2.setBounds(5, 115, 379, 390);
        p2.setBackground(Color.darkGray);
    //Label
        l1=new JLabel("Perform operations:");
        l1.setBounds(8, 91,200,23);
    //TextField to see performing operation
        tf=new JTextField("start",50);
        tf.setBounds( 5 , 5 , 365 , 80 );       
    //Calculator Button 
        b1=new JButton("CE");
        b1.setBounds(10, 10, 85, 70);
        JButton b2=new JButton("C");
        b2.setBounds(100, 10, 85, 70);
        JButton b3=new JButton("delete");
        b3.setBounds(190, 10, 85, 70);
        JButton b4=new JButton("%");
        b4.setBounds(280, 10, 90, 70);


        JButton b99=new JButton("9");
        b99.setBounds(10, 85, 85, 70);
        JButton b88=new JButton("8");
        b88.setBounds(100, 85, 85, 70);
        JButton b77=new JButton("7");
        b77.setBounds(190, 85, 85, 70);
        JButton b5=new JButton("*");
        b5.setBounds(280, 85, 90, 70);

        JButton b66=new JButton("6");
        b66.setBounds(10, 160, 85, 70);
        JButton b55=new JButton("5");
        b55.setBounds(100, 160, 85, 70);
        JButton b44=new JButton("4");
        b44.setBounds(190, 160, 85, 70);
        JButton b6=new JButton("-");
        b6.setBounds(280, 160, 90, 70);

        JButton b33=new JButton("3");
        b33.setBounds(10, 235, 85, 70);
        JButton b22=new JButton("2");
        b22.setBounds(100, 235, 85, 70);
        JButton b11=new JButton("1");
        b11.setBounds(190, 235, 85, 70);
        JButton b7=new JButton("+");
        b7.setBounds(280, 235, 90, 70);

        JButton b8=new JButton("00");
        b8.setBounds(10, 310, 85, 70);
        JButton b00=new JButton("0");
        b00.setBounds(100, 310, 85, 70);
        JButton b9=new JButton(".");
        b9.setBounds(190, 310, 85, 70);
        JButton b10=new JButton("=");
        b10.setBounds(280, 310, 90, 70);
//Add action listeners
        b1.addActionListener(this);
        b2.addActionListener(this);
        b3.addActionListener(this);
        b4.addActionListener(this);
        b5.addActionListener(this);
        b6.addActionListener(this);
        b7.addActionListener(this);
        b8.addActionListener(this);
        b9.addActionListener(this);
        b00.addActionListener(this);
        b11.addActionListener(this);
        b22.addActionListener(this);
        b33.addActionListener(this);
        b44.addActionListener(this);
        b55.addActionListener(this);
        b66.addActionListener(this);
        b77.addActionListener(this);
        b88.addActionListener(this);
        b99.addActionListener(this);

        fr.setResizable(false);
        fr.setLayout(null);//Always before adding components..
        p1.setLayout(null);
        p2.setLayout(null);
        tf.setFont(new Font("Courier", Font.BOLD,12));
        fr.add(p1);p1.add(tf);
                   fr.add(l1);
        fr.add(p2);p2.add(b1);p2.add(b2);p2.add(b3);p2.add(b4);
                   p2.add(b99);p2.add(b88);p2.add(b77);p2.add(b5);
                   p2.add(b66);p2.add(b55);p2.add(b44);p2.add(b6);
                   p2.add(b33);p2.add(b22);p2.add(b11);p2.add(b7);
                   p2.add(b8);p2.add(b00);p2.add(b9);p2.add(b10);
        fr.setVisible(true);
        fr.setSize(405, 550);
    }
    @Override
    public void actionPerformed(ActionEvent e) {
        if(e.getSource()==b99) 
            tf.setText(tf.getText() +"9");
        if(e.getSource()==b88) 
            tf.setText(tf.getText() +"8");
        if(e.getSource()==b77) 
            tf.setText(tf.getText() +"7");
        if(e.getSource()==b66) 
            tf.setText(tf.getText() +"6");
        if(e.getSource()==b55) 
            tf.setText(tf.getText() +"5");
        if(e.getSource()==b44) 
            tf.setText(tf.getText() +"4");
        if(e.getSource()==b33) 
            tf.setText(tf.getText() +"3");
        if(e.getSource()==b22) 
            tf.setText(tf.getText() +"2");
        if(e.getSource()==b11) 
            tf.setText(tf.getText() +"1");
        if(e.getSource()==b00) 
            tf.setText(tf.getText() +"0");  
        }
public static void main(String[] args) {
        new Gui();
    }
}


您不应该在构造函数中声明新变量,而应该使用在类中声明的变量

例如:

JButton b2=new JButton("C");
应该由

b2=new JButton("C");
等等

以下是正确的示例:

package de.eurodata.commons.lookandfeel.ui.swing;

import java.awt.Color;
import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.WindowConstants;
public class Gui extends WindowAdapter implements ActionListener {
    JFrame fr;
    JPanel p1,p2;
    JLabel l1;
    JTextField tf;

    JButton b1, b2, b3, b4, b5, b6, b7, b8, b9, b10;
    JButton b00,b11,b22,b33,b44,b55,b66,b77,b88,b99;
    public Gui(){
        //Frame 
        fr=new JFrame("Calculator");
        fr.getContentPane().setBackground(Color.LIGHT_GRAY);
        //fr.setBackground(Color.black);
        //Panel
        p1=new  JPanel();
        p2=new  JPanel();
        p1.setBounds(5, 5, 375, 90);
        p1.setBackground(Color.gray);
        p2.setBounds(5, 115, 379, 390);
        p2.setBackground(Color.darkGray);
        //Label
        l1=new JLabel("Perform operations:");
        l1.setBounds(8, 91,200,23);
        //TextField to see performing operation
        tf=new JTextField("start",50);
        tf.setBounds( 5 , 5 , 365 , 80 );       
        //Calculator Button 
        b1=new JButton("CE");
        b1.setBounds(10, 10, 85, 70);
        b2 = new JButton("C");
        b2.setBounds(100, 10, 85, 70);
        b3 = new JButton("delete");
        b3.setBounds(190, 10, 85, 70);
        b4 = new JButton("%");
        b4.setBounds(280, 10, 90, 70);


        b99 = new JButton("9");
        b99.setBounds(10, 85, 85, 70);
        b88 = new JButton("8");
        b88.setBounds(100, 85, 85, 70);
        b77 = new JButton("7");
        b77.setBounds(190, 85, 85, 70);
        b5 = new JButton("*");
        b5.setBounds(280, 85, 90, 70);

        b66 = new JButton("6");
        b66.setBounds(10, 160, 85, 70);
        b55 = new JButton("5");
        b55.setBounds(100, 160, 85, 70);
        b44 = new JButton("4");
        b44.setBounds(190, 160, 85, 70);
        b6 = new JButton("-");
        b6.setBounds(280, 160, 90, 70);

        b33 = new JButton("3");
        b33.setBounds(10, 235, 85, 70);
        b22 = new JButton("2");
        b22.setBounds(100, 235, 85, 70);
        b11 = new JButton("1");
        b11.setBounds(190, 235, 85, 70);
        b7 = new JButton("+");
        b7.setBounds(280, 235, 90, 70);

        b8 = new JButton("00");
        b8.setBounds(10, 310, 85, 70);
        b00 = new JButton("0");
        b00.setBounds(100, 310, 85, 70);
        b9 = new JButton(".");
        b9.setBounds(190, 310, 85, 70);
        b10 = new JButton("=");
        b10.setBounds(280, 310, 90, 70);
        //Add action listeners
        b1.addActionListener(this);
        b2.addActionListener(this);
        b3.addActionListener(this);
        b4.addActionListener(this);
        b5.addActionListener(this);
        b6.addActionListener(this);
        b7.addActionListener(this);
        b8.addActionListener(this);
        b9.addActionListener(this);
        b00.addActionListener(this);
        b11.addActionListener(this);
        b22.addActionListener(this);
        b33.addActionListener(this);
        b44.addActionListener(this);
        b55.addActionListener(this);
        b66.addActionListener(this);
        b77.addActionListener(this);
        b88.addActionListener(this);
        b99.addActionListener(this);

        fr.setResizable(false);
        fr.setLayout(null);//Always before adding components..
        p1.setLayout(null);
        p2.setLayout(null);
        tf.setFont(new Font("Courier", Font.BOLD,12));
        fr.add(p1);p1.add(tf);
        fr.add(l1);
        fr.add(p2);p2.add(b1);p2.add(b2);p2.add(b3);p2.add(b4);
        p2.add(b99);p2.add(b88);p2.add(b77);p2.add(b5);
        p2.add(b66);p2.add(b55);p2.add(b44);p2.add(b6);
        p2.add(b33);p2.add(b22);p2.add(b11);p2.add(b7);
        p2.add(b8);p2.add(b00);p2.add(b9);p2.add(b10);
        fr.setVisible(true);
        fr.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
        fr.setSize(405, 550);
    }
    @Override
    public void actionPerformed(ActionEvent e) {
        System.out.println(tf.getText());
        System.out.println(e.getSource());
        if(e.getSource()==b99) 
            tf.setText(tf.getText() +"9");
        if(e.getSource()==b88) 
            tf.setText(tf.getText() +"8");
        if(e.getSource()==b77) 
            tf.setText(tf.getText() +"7");
        if(e.getSource()==b66) 
            tf.setText(tf.getText() +"6");
        if(e.getSource()==b55) 
            tf.setText(tf.getText() +"5");
        if(e.getSource()==b44) 
            tf.setText(tf.getText() +"4");
        if(e.getSource()==b33) 
            tf.setText(tf.getText() +"3");
        if(e.getSource()==b22) 
            tf.setText(tf.getText() +"2");
        if(e.getSource()==b11) 
            tf.setText(tf.getText() +"1");
        if(e.getSource()==b00) 
            tf.setText(tf.getText() +"0");  
        System.out.println("After: " + tf.getText());
    }
    public static void main(String[] args) {
        new Gui();
    }
}
重要:正如已经向您提出的,请了解布局管理器,因为
setLayout(null)
对于Swing来说是一种非常糟糕的做法