无法让JTextArea出现在计算器上

时间:2016-04-29 15:10:10

标签: java swing

我正在尝试为学校项目制作一个计算器,但我无法让JTextArea出现。我需要一个窗口,以便用户可以看到他们输入计算器和输出的数字。

这是我的代码:

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

public class Calculator extends JFrame 
{

JButton[] nums = new JButton [10];

JButton num1 = new JButton ("1");
JButton num2 = new JButton ("2");
JButton num3 = new JButton ("3");
JButton num4 = new JButton ("4");
JButton num5 = new JButton ("5");
JButton num6 = new JButton ("6");
JButton num7 = new JButton ("7");
JButton num8 = new JButton ("8");
JButton num9 = new JButton ("9");
JButton num0 = new JButton ("0");
JButton decimal = new JButton (".");
JButton clear = new JButton ("C");
JButton sqrt = new JButton ("\u221A");
JButton mod = new JButton ("%");
JButton dividebyone = new JButton ("1/x");
JButton factorn = new JButton ("!n");

public Calculator ()
{
    JFrame window = new JFrame ("Calculator");
    setSize (260, 325);
    setDefaultCloseOperation (JFrame.EXIT_ON_CLOSE);
    setVisible (true);

    JTextArea screen = new JTextArea (20, 20);
    JScrollPane pls = new JScrollPane (screen);
    screen.setBackground (Color.WHITE);
    screen.setVisible (true);
    getContentPane ().add (pls);

    getContentPane ().add (stylize (num1));
    num1.setBounds (20, 75, 45, 45);
    num1.addActionListener (new ActionListener ()
    {

        public void actionPerformed (ActionEvent e)
        {
            //Execute when button is pressed
            System.out.println ("1");
        }
    }
    );

    getContentPane ().add (stylize (num2));
    num2.setBounds (70, 75, 45, 45);
    num2.addActionListener (new ActionListener ()
    {

        public void actionPerformed (ActionEvent e)
        {
            System.out.println ("2");
        }
    }
    );

    getContentPane ().add (stylize (num3));
    num3.setBounds (120, 75, 45, 45);
    num3.addActionListener (new ActionListener ()
    {

        public void actionPerformed (ActionEvent e)
        {
            System.out.println ("3");
        }
    }
    );

    getContentPane ().add (stylize (num4));
    num4.setBounds (20, 125, 45, 45);
    num4.addActionListener (new ActionListener ()
    {

        public void actionPerformed (ActionEvent e)
        {
            System.out.println ("4");
        }
    }
    );

    getContentPane ().add (stylize (num5));
    num5.setBounds (70, 125, 45, 45);
    num5.addActionListener (new ActionListener ()
    {

        public void actionPerformed (ActionEvent e)
        {
            System.out.println ("5");
        }
    }
    );

    getContentPane ().add (stylize (num6));
    num6.setBounds (120, 125, 45, 45);
    num6.addActionListener (new ActionListener ()
    {

        public void actionPerformed (ActionEvent e)
        {
            System.out.println ("6");
        }
    }
    );

    getContentPane ().add (stylize (num7));
    num7.setBounds (20, 175, 45, 45);
    num7.addActionListener (new ActionListener ()
    {

        public void actionPerformed (ActionEvent e)
        {
            System.out.println ("7");
        }
    }
    );

    getContentPane ().add (stylize (num8));
    num8.setBounds (70, 175, 45, 45);
    num8.addActionListener (new ActionListener ()
    {

        public void actionPerformed (ActionEvent e)
        {
            System.out.println ("8");
        }
    }
    );

    getContentPane ().add (stylize (num9));
    num9.setBounds (120, 175, 45, 45);
    num9.addActionListener (new ActionListener ()
    {

        public void actionPerformed (ActionEvent e)
        {
            System.out.println ("9");
        }
    }
    );

    getContentPane ().add (stylize (num0));
    num0.setBounds (20, 225, 45, 45);
    num0.addActionListener (new ActionListener ()
    {

        public void actionPerformed (ActionEvent e)
        {
            System.out.println ("0");
        }
    }
    );

    getContentPane ().add (stylize (decimal));
    decimal.setBounds (70, 225, 45, 45);
    decimal.addActionListener (new ActionListener ()
    {

        public void actionPerformed (ActionEvent e)
        {
            System.out.println (".");
        }
    }
    );

    getContentPane ().add (stylize (clear));
    clear.setBounds (120, 225, 45, 45);
    clear.addActionListener (new ActionListener ()
    {

        public void actionPerformed (ActionEvent e)
        {
            System.out.println ("c");
        }
    }
    );

    getContentPane ().add (stylize (sqrt));
    sqrt.setBounds (170, 75, 51, 45);
    sqrt.addActionListener (new ActionListener ()
    {

        public void actionPerformed (ActionEvent e)
        {
            System.out.println ("sqrt");
        }
    }
    );

    getContentPane ().add (stylize (mod));
    mod.setBounds (170, 125, 51, 45);
    mod.addActionListener (new ActionListener ()
    {

        public void actionPerformed (ActionEvent e)
        {
            System.out.println ("mod");
        }
    }
    );

    getContentPane ().add (stylize (factorn));
    factorn.setBounds (170, 175, 51, 45);
    factorn.addActionListener (new ActionListener ()
    {

        public void actionPerformed (ActionEvent e)
        {
            System.out.println ("!n");
        }
    }
    );

    getContentPane ().add (stylize (dividebyone));
    dividebyone.setBounds (170, 225, 51, 45);
    dividebyone.addActionListener (new ActionListener ()
    {

        public void actionPerformed (ActionEvent e)
        {
            System.out.println ("1/x");
        }
    }
    );

} //end Calculator


public static JButton stylize (JButton button)
{
    button.setBackground (Color.WHITE);
    return button;
} //end stylize


public static void main (String args[])
{
    new Calculator ();
} //end main
} //end Calculator class

2 个答案:

答案 0 :(得分:0)

尝试这样做:

Public static void main(String arts[])
{
    Calculator c= new Calculator();
    c.setVisible(true);
}

从构造函数中删除setVisible(true)。 希望这有效。
在您添加组件之前,您的框架可见。因此,它不会显示您的组件。

答案 1 :(得分:0)

这样的事情应该显示文本区域给出你已经开始的代码并采取@ Abhishek的建议

public class Calculator extends JFrame {

    JButton[] nums = new JButton[10];

    JButton num1 = new JButton("1");
    JButton num2 = new JButton("2");
    JButton num3 = new JButton("3");
    JButton num4 = new JButton("4");
    JButton num5 = new JButton("5");
    JButton num6 = new JButton("6");
    JButton num7 = new JButton("7");
    JButton num8 = new JButton("8");
    JButton num9 = new JButton("9");
    JButton num0 = new JButton("0");
    JButton decimal = new JButton(".");
    JButton clear = new JButton("C");
    JButton sqrt = new JButton("\u221A");
    JButton mod = new JButton("%");
    JButton dividebyone = new JButton("1/x");
    JButton factorn = new JButton("!n");

    public Calculator() {
        setSize(260, 325);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        final JPanel content = new JPanel();
        content.setLayout(null);

        JTextArea screen = new JTextArea();
        JScrollPane pls = new JScrollPane(screen);
        pls.setBounds(20, 10, 200, 45);
        screen.setBackground(Color.WHITE);
        content.add(pls);

        content.add(stylize(num1));
        num1.setBounds(20, 75, 45, 45);
        num1.addActionListener(new ActionListener() {

            public void actionPerformed(ActionEvent e) {
                // Execute when button is pressed
                System.out.println("1");
            }
        });

        content.add(stylize(num2));
        num2.setBounds(70, 75, 45, 45);
        num2.addActionListener(new ActionListener() {

            public void actionPerformed(ActionEvent e) {
                System.out.println("2");
            }
        });

        content.add(stylize(num3));
        num3.setBounds(120, 75, 45, 45);
        num3.addActionListener(new ActionListener() {

            public void actionPerformed(ActionEvent e) {
                System.out.println("3");
            }
        });

        content.add(stylize(num4));
        num4.setBounds(20, 125, 45, 45);
        num4.addActionListener(new ActionListener() {

            public void actionPerformed(ActionEvent e) {
                System.out.println("4");
            }
        });

        content.add(stylize(num5));
        num5.setBounds(70, 125, 45, 45);
        num5.addActionListener(new ActionListener() {

            public void actionPerformed(ActionEvent e) {
                System.out.println("5");
            }
        });

        content.add(stylize(num6));
        num6.setBounds(120, 125, 45, 45);
        num6.addActionListener(new ActionListener() {

            public void actionPerformed(ActionEvent e) {
                System.out.println("6");
            }
        });

        content.add(stylize(num7));
        num7.setBounds(20, 175, 45, 45);
        num7.addActionListener(new ActionListener() {

            public void actionPerformed(ActionEvent e) {
                System.out.println("7");
            }
        });

        content.add(stylize(num8));
        num8.setBounds(70, 175, 45, 45);
        num8.addActionListener(new ActionListener() {

            public void actionPerformed(ActionEvent e) {
                System.out.println("8");
            }
        });

        content.add(stylize(num9));
        num9.setBounds(120, 175, 45, 45);
        num9.addActionListener(new ActionListener() {

            public void actionPerformed(ActionEvent e) {
                System.out.println("9");
            }
        });

        content.add(stylize(num0));
        num0.setBounds(20, 225, 45, 45);
        num0.addActionListener(new ActionListener() {

            public void actionPerformed(ActionEvent e) {
                System.out.println("0");
            }
        });

        content.add(stylize(decimal));
        decimal.setBounds(70, 225, 45, 45);
        decimal.addActionListener(new ActionListener() {

            public void actionPerformed(ActionEvent e) {
                System.out.println(".");
            }
        });

        content.add(stylize(clear));
        clear.setBounds(120, 225, 45, 45);
        clear.addActionListener(new ActionListener() {

            public void actionPerformed(ActionEvent e) {
                System.out.println("c");
            }
        });

        content.add(stylize(sqrt));
        sqrt.setBounds(170, 75, 51, 45);
        sqrt.addActionListener(new ActionListener() {

            public void actionPerformed(ActionEvent e) {
                System.out.println("sqrt");
            }
        });

        content.add(stylize(mod));
        mod.setBounds(170, 125, 51, 45);
        mod.addActionListener(new ActionListener() {

            public void actionPerformed(ActionEvent e) {
                System.out.println("mod");
            }
        });

        content.add(stylize(factorn));
        factorn.setBounds(170, 175, 51, 45);
        factorn.addActionListener(new ActionListener() {

            public void actionPerformed(ActionEvent e) {
                System.out.println("!n");
            }
        });

        content.add(stylize(dividebyone));
        dividebyone.setBounds(170, 225, 51, 45);
        dividebyone.addActionListener(new ActionListener() {

            public void actionPerformed(ActionEvent e) {
                System.out.println("1/x");
            }
        });
        setContentPane(content);
    } // end Calculator

    public static JButton stylize(JButton button) {
        button.setBackground(Color.WHITE);
        return button;
    } // end stylize

    public static void main(String args[]) {
        final Calculator calc = new Calculator();
        calc.setVisible(true);
    } // end main
} // end Calculator class