无法让面板显示在JFrame中

时间:2013-05-30 05:28:47

标签: java swing

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

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();}           
}  

它仍然无效:(

5 个答案:

答案 0 :(得分:2)

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

尝试一下

的内容
add(P1, BorderLayout.NORTH);
add(P2, BorderLayout.SOUTH);

首先......

您可能希望看看

答案 1 :(得分:1)

JFrame

中添加您的P2
this.add(P2);
this.pack();

答案 2 :(得分:1)

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

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

将它们重命名,以便从一个小写字母开始。大写字母用于类名。

答案 3 :(得分:0)

好:

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

2)。您没有将P1P2面板添加到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();
            }
        });
    }
}

答案 4 :(得分:0)

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