Java 为什么不是';我的JButton是否工作,JTextField是否不显示?

Java 为什么不是';我的JButton是否工作,JTextField是否不显示?,java,swing,Java,Swing,我的程序不显示计算器的JTextField部分,清除JButton无法重置计算器,因此当我清除按钮并尝试执行新计算时,会出现运行时错误。顺便说一句,布尔值s应该是OperationSelected import java.awt.event.*; import java.awt.*; import javax.swing.*; public class calculator extends JFrame { private JButton num1; private JButton

我的程序不显示计算器的JTextField部分,清除JButton无法重置计算器,因此当我清除按钮并尝试执行新计算时,会出现运行时错误。顺便说一句,布尔值s应该是OperationSelected

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


public class calculator extends JFrame 
 {
private JButton num1;
private JButton num2;
private JButton num3;
private JButton num4;
private JButton num5;
private JButton num6;
private JButton num7;
private JButton num8;
private JButton num9;
private JButton num0;
private JButton equal;
private JButton addition;
private JButton sub;
private JButton mult;
private JButton divide;
private JButton modulus;
private JButton solve;
private JButton clear;
private boolean s=false;
private boolean equalsClicked=false;
private double val;
private String temp1,temp2,answer;
private JTextField answerfield;
private JPanel contentPane;
private char operation=' ';


public calculator(String x)
{
    super(x);
    answerfield=new JTextField();
    answerfield.setEditable(false);

    answerfield.setPreferredSize(new Dimension(160, 20));
    answerfield.setBackground(Color.WHITE);
    answerfield.setEnabled(false);
    answerfield.setHorizontalAlignment(4);
    answerfield.setDisabledTextColor(Color.BLACK);

    contentPane = new JPanel();
    contentPane.setLayout(null);
    contentPane.add(answerfield,BorderLayout.NORTH);




    setBounds(400,300,255,400);
    this.setContentPane(contentPane);


    Numbers n=new Numbers();
    Calculation c=new Calculation();

    num1=new JButton("1");
    num1.setBounds(10, 130, 52, 37);
    add(num1);
    num1.setActionCommand("1");
    num1.addActionListener(n);


    num2=new JButton("2");
    num2.setBounds(72, 130, 52, 37);
    add(num2);
    num2.setActionCommand("2");
    num2.addActionListener(n);


    num3=new JButton("3");
    num3.setBounds(134, 130, 52, 37);
    add(num3);
    num3.setActionCommand("3");
    num3.addActionListener(n);


    num4=new JButton("4");
    num4.setBounds(10, 178, 52, 37);
    add(num4);
    num4.setActionCommand("4");
    num4.addActionListener(n);


    num5=new JButton("5");
    num5.setBounds(72, 178, 52, 37);
    add(num5);
    num5.setActionCommand("5");
    num5.addActionListener(n);


    num6=new JButton("6");
    num6.setBounds(134, 178, 52, 37);
    add(num6);
    num6.setActionCommand("6");
    num6.addActionListener(n);


    num7=new JButton("7");
    num7.setBounds(10, 226, 52, 37);
    add(num7);
    num7.setActionCommand("7");
    num7.addActionListener(n);


    num8=new JButton("8");
    num8.setBounds(72, 226, 52, 37);
    add(num8);
    num8.setActionCommand("8");
    num8.addActionListener(n);



    num9=new JButton("9");
    num9.setBounds(134, 226, 52, 37);
    add(num9);
    num9.setActionCommand("9");
    num9.addActionListener(n);


    num0=new JButton("0");
    num0.setBounds(10, 274, 52, 37);
    add(num0);
    num0.setActionCommand("0");
    num0.addActionListener(n);


    equal=new JButton("=");
    equal.setBounds(72, 274, 52, 37);
    add(equal);
    equal.setActionCommand("=");
    equal.addActionListener(c);


    addition=new JButton("+");
    addition.setBounds(193, 130,45, 35);
    add(addition);
    addition.setActionCommand("+");

    addition.addActionListener(c);

    sub=new JButton("-");
    sub.setBounds(193, 179, 45, 35);
    add(sub);
    sub.setActionCommand("-");

    sub.addActionListener(c);

    mult=new JButton("*");
    mult.setBounds(193,226, 45, 35);
    add(mult);
    mult.setActionCommand("*");

    mult.addActionListener(c);

    divide=new JButton("/");
    divide.setBounds(193, 274, 45, 35);
    add(divide);
    divide.setActionCommand("/");

    divide.addActionListener(c);

    modulus=new JButton("%");
    modulus.setBounds(193,322, 45, 35);
    add(modulus);
    modulus.setActionCommand("%");

    modulus.addActionListener(c);


    clear=new JButton("C");
    clear.setBounds(53, 343, 89, 23);
    add(clear);
    clear.setActionCommand("C");
    clear.addActionListener(c);







    setVisible(true);
    show();
    setResizable(false);


}
private class Numbers implements ActionListener
{
    public void actionPerformed(ActionEvent event)
    {
        JButton x=(JButton)event.getSource();
        if(x.equals(num1))
        {
            if(s==false)
            {
                if(temp1==null)
                {
                    temp1="1";
                }
                else
                    temp1+="1";
            }
            else
            {
                if(temp2==null)
                {
                    temp2="1";
                }
                else
                    temp2+="1";
            }

        }
        if(x.equals(num2))
        {
            if(s==false)
            {
                if(temp1==null)
                {
                    temp1="2";
                }
                else
                    temp1+="2";
            }
            else
            {
                if(temp2==null)
                {
                    temp2="2";
                }
                else
                    temp2+="2";
            }

        }
        if(x.equals(num3))
        {
            if(s==false)
            {
                if(temp1==null)
                {
                    temp1="3";
                }
                else
                    temp1+="3";
            }
            else
            {
                if(temp2==null)
                {
                    temp2="3";
                }
                else
                    temp2+="3";
            }

        }
        if(x.equals(num4))
        {
            if(s==false)
            {
                if(temp1==null)
                {
                    temp1="4";
                }
                else
                    temp1+="4";
            }
            else
            {
                if(temp2==null)
                {
                    temp2="4";
                }
                else
                    temp2+="4";
            }

        }
        if(x.equals(num5))
        {
            if(s==false)
            {
                if(temp1==null)
                {
                    temp1="5";
                }
                else
                    temp1+="5";
            }
            else
            {
                if(temp2==null)
                {
                    temp2="5";
                }
                else
                    temp2+="5";
            }

        }
        if(x.equals(num6))
        {
            if(s==false)
            {
                if(temp1==null)
                {
                    temp1="6";
                }
                else
                    temp1+="6";
            }
            else
            {
                if(temp2==null)
                {
                    temp2="6";
                }
                else
                    temp2+="6";
            }

        }
        if(x.equals(num7))
        {
            if(s==false)
            {
                if(temp1==null)
                {
                    temp1="7";
                }
                else
                    temp1+="7";
            }
            else
            {
                if(temp2==null)
                {
                    temp2="7";
                }
                else
                    temp2+="7";
            }

        }
        if(x.equals(num8))
        {
            if(s==false)
            {
                if(temp1==null)
                {
                    temp1="8";
                }
                else
                    temp1+="8";
            }
            else
            {
                if(temp2==null)
                {
                    temp2="8";
                }
                else
                    temp2+="8";
            }

        }
        if(x.equals(num9))
        {
            if(s==false)
            {
                if(temp1==null)
                {
                    temp1="9";
                }
                else
                    temp1+="9";
            }
            else
            {
                if(temp2==null)
                {
                    temp2="9";
                }
                else
                    temp2+="9";
            }

        }
        if(x.equals(num0))
        {
            if(s==false)
            {
                if(temp1==null)
                {
                    temp1="0";
                }
                else
                    temp1+="0";
            }
            else
            {
                if(temp2==null)
                {
                    temp2="0";
                }
                else
                    temp2+="0";
            }

        }
        if(equalsClicked==false)
        {
            if(s==false)
            {
                answerfield.setText(temp1);
            }
            else
            {
                answerfield.setText(temp2);
            }
        }



    }
}
public void Clear()
{
    temp1="";
    temp2="";
    s=false;
    operation=' ';
    val=0;
    answer="";
    equalsClicked=false;
    System.out.println("hey");
}
private class Calculation implements ActionListener
{
    public void actionPerformed(ActionEvent event)
    {
        JButton x=(JButton)event.getSource();
        if(x.equals(addition))
        {
            if(temp1==null)
                System.out.println("choose numbers");
            else
            {
                if(temp1!=null&&temp2==null)
                {
                    s=true;
                    operation='+';

                }

            }
        }
        if(x.equals(sub))
        {
            if(temp1==null)
                System.out.println("choose numbers");
            else
            {
                if(temp1!=null&&temp2==null)
                {
                    s=true;
                    operation='-';

                }

            }
        }
        if(x.equals(mult))
        {
            if(temp1==null)
                System.out.println("choose numbers");
            else
            {
                if(temp1!=null&&temp2==null)
                {
                    s=true;
                    operation='*';

                }

            }
        }
        if(x.equals(divide))
        {
            if(temp1==null)
                System.out.println("choose numbers");
            else
            {
                if(temp1!=null&&temp2==null)
                {
                    s=true;
                    operation='/';

                }

            }
        }
        if(x.equals(modulus))
        {
            if(temp1==null)
                System.out.println("choose numbers");
            else
            {
                if(temp1!=null&&temp2==null)
                {
                    s=true;
                    operation='%';

                }

            }
        }
        if(x.equals(equal))
        {
            if(temp1==null)
                System.out.println("choose numbers");
            else
            {
                if(temp1!=null&&temp2==null)
                    System.out.println("choose numbers");


            }
            if(temp1!=null&&temp2!=null)
            {
                double d=0.0;
                double s=0.0;
                d=Double.parseDouble(temp1);
                s=Double.parseDouble(temp2);
                switch(operation)
                {
                    case '+':
                    {
                        val=d+s;
                        break;
                    }
                    case '-':
                    {
                        val=d-s;
                        break;
                    }
                    case'*':
                    {
                        val=d*s;
                        break;
                    }
                    case'/':
                    {
                        val=d/s;
                        break;
                    }
                    case'%':
                    {
                        val=d%s;
                        break;
                    }

                }
                answer=Double.toString(val);
                answerfield.setText(answer);
                System.out.println(answer);
            }
        }
        if(x.equals(clear))
        {
            Clear();
        }
    }
}

public static void main(String args[])
{
    new calculator("math");
}
}

首先要做的是

answerfield = new JTextField();
answerfield.setEditable(false);

answerfield.setPreferredSize(new Dimension(160, 20));
answerfield.setBackground(Color.WHITE);
answerfield.setEnabled(false);
answerfield.setHorizontalAlignment(4);
answerfield.setDisabledTextColor(Color.BLACK);
contentPane = new JPanel();
contentPane.setLayout(null);
contentPane.add(answerfield, BorderLayout.NORTH);
这一切似乎都很酷

但当你这么做的时候

answerfield = new JTextField();
answerfield.setEditable(false);

answerfield.setPreferredSize(new Dimension(160, 20));
answerfield.setBackground(Color.WHITE);
answerfield.setEnabled(false);
answerfield.setHorizontalAlignment(4);
answerfield.setDisabledTextColor(Color.BLACK);
contentPane = new JPanel();
contentPane.setLayout(null);
contentPane.add(answerfield, BorderLayout.NORTH);
answerfield
没有定义的大小或位置,因此它的大小是
0x0
,位置是
0x0
。Swing足够聪明,不会画它

答案是,不要使用
null
layouts

避免使用
null
布局,像素完美的布局在现代ui设计中是一种错觉。影响零部件单个尺寸的因素太多,您无法控制。Swing的设计初衷是与布局管理器一起工作,丢弃这些布局管理器将导致无止境的问题,您将花费越来越多的时间来纠正这些问题

查看和了解更多详细信息

使用布局示例进行更新

本例使用了
GridBagLayout
,这可能是最强大的默认布局管理器之一,但也是最难使用的布局管理器之一

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.EventQueue;
import java.awt.Font;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JComponent;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;
import javax.swing.border.EmptyBorder;

public class Calculator extends JFrame {

    private boolean s = false;
    private boolean equalsClicked = false;
    private double val;
    private String temp1, temp2, answer;
    private JTextField answerfield;
    private char operation = ' ';

    public Calculator(String x) {
        super(x);
        answerfield = new JTextField();
        answerfield.setEditable(false);

        answerfield.setBackground(Color.WHITE);
        answerfield.setEnabled(false);
        answerfield.setHorizontalAlignment(4);
        answerfield.setDisabledTextColor(Color.BLACK);
        answerfield.setFont(answerfield.getFont().deriveFont(Font.BOLD, 24));

        JPanel field = new JPanel(new BorderLayout());
        field.setBorder(new EmptyBorder(4, 4, 0, 4));
        field.add(answerfield);
        add(field, BorderLayout.NORTH);

        JPanel numbers = new JPanel(new GridBagLayout());

        Numbers n = new Numbers();
        Calculation c = new Calculation();

        addButtonTo(numbers, "1", n, 0, 0);
        addButtonTo(numbers, "2", n, 1, 0);
        addButtonTo(numbers, "3", n, 2, 0);
        addButtonTo(numbers, "+", c, 3, 0);
        addButtonTo(numbers, "4", n, 0, 1);
        addButtonTo(numbers, "5", n, 1, 1);
        addButtonTo(numbers, "6", n, 2, 1);
        addButtonTo(numbers, "-", c, 3, 1);
        addButtonTo(numbers, "7", n, 0, 2);
        addButtonTo(numbers, "8", n, 1, 2);
        addButtonTo(numbers, "9", n, 2, 2);
        addButtonTo(numbers, "*", c, 3, 2);
        addButtonTo(numbers, "0", n, 0, 3);
        addButtonTo(numbers, "=", c, 1, 3);
        addButtonTo(numbers, "", null, 2, 3);
        addButtonTo(numbers, "/", n, 3, 3);
        addButtonTo(numbers, "C", c, 0, 4, 3);
        addButtonTo(numbers, "%", c, 3, 4);

        add(numbers);

        setDefaultCloseOperation(EXIT_ON_CLOSE);
        pack();
        setVisible(true);

    }

    protected void addButtonTo(JPanel panel, String text, ActionListener listener, int gridX, int gridY) {

        addButtonTo(panel, text, listener, gridX, gridY, 1);

    }

    protected void addButtonTo(JPanel panel, String text, ActionListener listener, int gridX, int gridY, int gridWidth) {

        JComponent comp = null;
        if (text == null || text.trim().isEmpty()) {
            comp = new JPanel();
        } else {

            JButton button = new JButton(text);
            button.setActionCommand(text);
            button.addActionListener(listener);
            comp = button;

        }

        GridBagConstraints gbc = new GridBagConstraints();
        gbc.gridx = gridX;
        gbc.gridy = gridY;
        gbc.gridwidth = gridWidth;
        gbc.insets = new Insets(4, 4, 4, 4);
        gbc.fill = GridBagConstraints.BOTH;

        panel.add(comp, gbc);

    }

    private class Numbers implements ActionListener {

        public void actionPerformed(ActionEvent event) {
            JButton x = (JButton) event.getSource();
            String text = x.getActionCommand();

            if (s == false) {
                if (temp1 == null) {
                    temp1 = text;
                } else {
                    temp1 += text;
                }
            } else {
                if (temp2 == null) {
                    temp2 = text;
                } else {
                    temp2 += text;
                }
            }

            if (equalsClicked == false) {
                if (s == false) {
                    answerfield.setText(temp1);
                } else {
                    answerfield.setText(temp2);
                }
            }

        }
    }

    public void Clear() {
        temp1 = "";
        temp2 = "";
        s = false;
        operation = ' ';
        val = 0;
        answer = "";
        equalsClicked = false;
        System.out.println("hey");
        answerfield.setText("");
    }

    private class Calculation implements ActionListener {

        public void actionPerformed(ActionEvent event) {
            JButton x = (JButton) event.getSource();
            String text = x.getActionCommand();
            System.out.println(text);
            switch (text) {
                case "+":
                case "-":
                case "*":
                case "/":
                case "%":
                    if (temp1 == null) {
                        System.out.println("choose numbers");
                    } else {
                        if (temp1 != null && temp2 == null) {
                            s = true;
                            operation = text.charAt(0);
                        }
                    }
                    break;
                case "=":
                    if (temp1 == null) {
                        System.out.println("choose numbers");
                    } else {
                        if (temp1 != null && temp2 == null) {
                            System.out.println("choose numbers");
                        }

                    }
                    if (temp1 != null && temp2 != null) {
                        double d = 0.0;
                        double s = 0.0;
                        d = Double.parseDouble(temp1);
                        s = Double.parseDouble(temp2);
                        switch (operation) {
                            case '+': {
                                val = d + s;
                                break;
                            }
                            case '-': {
                                val = d - s;
                                break;
                            }
                            case '*': {
                                val = d * s;
                                break;
                            }
                            case '/': {
                                val = d / s;
                                break;
                            }
                            case '%': {
                                val = d % s;
                                break;
                            }

                        }
                        answer = Double.toString(val);
                        answerfield.setText(answer);
                        System.out.println(answer);
                    }
                    break;
                case "C":
                    Clear();
                    break;
            }
        }
    }

    public static void main(String args[]) {
        EventQueue.invokeLater(new Runnable() {
            @Override
            public void run() {
                try {
                    UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
                } catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
                    ex.printStackTrace();
                }

                new Calculator("math");
            }
        });
    }
}


神圣的$h | t这是一个问题中的大量代码-我没有否决投票,只是为了record@AbdulAhmad我只是不知道哪里出了问题。
contentPane.setLayout(null)将很好地指示您的问题…对于jtextfield,请确保您使用了正确的布局管理器来显示作业对象correctly@AbdulAdhmad你是什么意思?对于这个特殊的赋值,我必须将布局设置为null…那么,你建议我如何更改我的代码以适应我必须设置的这个特殊赋值布局设置为null…那么您建议我如何将代码更改为“对于此特定分配,我必须将布局设置为null”-为什么?你可以使用
BorderLayout
GridLayout
的组合,或者如果你准备挑战,
gridbagloayout
…那么我应该在我的程序中删除什么呢……以及为什么清除按钮不起作用的想法work@user4216980“清除”按钮似乎工作正常,但从未清除“应答”字段