0

我正在尝试使用 Java 设计一个计算器外壳(不起作用的计算器),但由于某种原因,我无法让程序显示我的按钮。我在这里做错了什么?

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

public class Calculator extends JFrame {

    public Calculator() {
        JPanel P1 = new JPanel();
        P1.setLayout(new GridLayout(4, 4));
        //Panel Buttons
        for (int i = 1; i <= 9; i++) {
            P1.add(new JButton("" + i));
        }
        P1.add(new JButton("" + 0));
        P1.add(new JButton("."));
        P1.add(new JButton("*"));
        P1.add(new JButton("/"));
        P1.add(new JButton("+"));
        P1.add(new JButton("-"));
        P1.add(new JButton("="));
        P1.setBackground(Color.cyan);
        P1.setForeground(new Color(100, 1, 1));
        //Content panel
        JPanel P2 = new JPanel();
        P2.setLayout(new BorderLayout());
        P2.add(new JTextField("Hello world"), BorderLayout.NORTH);
        P2.add(P1, BorderLayout.CENTER);
    }


    public static void main(String[] args) {
        Calculator frame = new Calculator();
        frame.setTitle("Simple Calculator");
        frame.setSize(250, 250);
        frame.setLocationRelativeTo(null);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setVisible(true);
    }

}

好的,这就是我现在所拥有的......

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

public class Calculator extends JFrame
{
JPanel p1 = new JPanel();
JPanel p2 = new JPanel();
Calculator()
{
    super("Wk 3 Calculator"); setBounds(100,100,300,100);
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    Container con = this.getContentPane();
    con.add(p1);
    con.add(p2);
    p1.setLayout(new GridLayout(4, 4));
    //Panel Buttons
for (int i =1; i <=9; i++){
    p1.add(new JButton ("" + i));
}
    p1.add(new JButton (""+0)); 
    p1.add(new JButton ("."));      
    p1.add(new JButton ("*"));
    p1.add(new JButton ("/"));
    p1.add(new JButton ("+"));
    p1.add(new JButton ("-"));
    p1.add(new JButton ("="));
    //Content panel
    p2.setLayout(new BorderLayout());
    p2.add (new JTextField("Hello world"),BorderLayout.NORTH);
    p2.add(p1, BorderLayout.CENTER); 
    //Frame specs
    Calculator frame = new Calculator();
    frame.setSize(250,250);
    frame.setTitle("Simple Calculator");
    frame.add(p1, BorderLayout.NORTH);
    frame.add(p2, BorderLayout.SOUTH);
    frame.setLocationRelativeTo(null);
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.pack();
    frame.setVisible(true);
}
    public static void main(String[] args){new Calculator();}           
}  

它仍然无法正常工作:(

4

5 回答 5

2

你没有在框架中添加任何东西......

尝试一些类似的东西

add(P1, BorderLayout.NORTH);
add(P2, BorderLayout.SOUTH);

对于初学者...

你可能想看看

于 2013-05-30T05:33:45.873 回答
1

在里面添加你的 P2JFrame

this.add(P2);
this.pack();
于 2013-05-30T05:34:15.667 回答
1

您没有将面板添加到框架中。将add(p1, BorderLayout.NORTH)andadd(p2, BorderLayout.SOUTH)放在构造函数的末尾。

public Calculator()
{
    //rest of the construtor
    add(p1, BorderLayout.NORTH);
    add(p2, BorderLayout.SOUTH);
}

并重命名它们,使它们以小写字母开头。大写字母用于类名。

于 2013-05-30T05:34:31.657 回答
0

this.add(P2); this.pack(); this.repain();

于 2013-05-30T05:38:21.517 回答
0

出色地:

1)。在 EDT(事件调度线程)上调用 Swing 相关的代码,而不是在主线程上。有关更多详细信息,请参见此处

2)。您没有将P1andP2面板添加到 中JFrame,因此当您显示框架时,它不包含任何内容。尝试调用add (P1)add (P2)

3)。你应该在打电话frame.pack()之前打电话frame.setVisible (true)

编辑

我重写了你的代码(你仍然有很长的路要制作一个功能性的应用程序,但这是一个开始):

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

public class Calculator
{
    private JFrame frame;

    public Calculator()
    {
        frame = new JFrame ("Simple Calculator");

        frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
        frame.setLayout (new BorderLayout ());

        JPanel panel = new JPanel(new GridLayout(4, 4));
        JPanel p2 = new JPanel(new BorderLayout());

        for (int i = 1; i <= 9; i++)
        {
            panel.add (new JButton ("" + i));
        }

        panel.add(new JButton (""+0)); 
        panel.add(new JButton ("."));      
        panel.add(new JButton ("*"));
        panel.add(new JButton ("/"));
        panel.add(new JButton ("+"));
        panel.add(new JButton ("-"));
        panel.add(new JButton ("="));

        frame.add(panel, BorderLayout.CENTER);
        frame.add (new JTextField("Hello world"), BorderLayout.SOUTH);

        frame.pack();
        frame.setLocationRelativeTo(null);
        frame.setVisible(true);
    }
    public static void main(String[] args)
    {
        SwingUtilities.invokeLater (new Runnable ()
        {
            @Override
            public void run ()
            {
                new Calculator();
            }
        });
    }
}
于 2013-05-30T05:33:41.620 回答