JFrame无法正常工作 - 内容不会出现

时间:2015-12-11 06:24:12

标签: java swing jframe

不确定我的问题是什么。我创建了一个JFrame,我有一个面板,它将有4个大按钮(带有图形 - 虽然还没有编码)在框架上显示但我在尝试运行它时遇到错误面板没有显示在框架中。

更新:没有错误信息,但框架中没有面板或按钮......

public class EasyExpress {
private static JFrame frame = new JFrame("EASY BUTTONS");
private JButton WriteBTN = new JButton("Write Email");
private JButton EmailBTN = new JButton("View Emails");
private JButton SolBTN = new JButton("Play Solsuite Solitaire");
private JButton ShutBTN = new JButton("Shutdown Computer");

private JPanel btnPanel;

public EasyExpress() {


    /*  try {
            Image img = ImageIO.read(getClass().getResource("write.jpg"));
            WriteBTN.setIcon(new ImageIcon(img));         
          } catch (IOException ex) {
          }*/

    btnPanel = new JPanel(new GridLayout(1,4,1,1));
    btnPanel.setBounds(0, 0, 1200, 400);        


    WriteBTN.setPreferredSize(new Dimension(300,400));
    EmailBTN.setPreferredSize(new Dimension(300,400));
    SolBTN.setPreferredSize(new Dimension(300,400));
    ShutBTN.setPreferredSize(new Dimension(300,400));

    btnPanel.add(EmailBTN);
    btnPanel.add(WriteBTN);
    btnPanel.add(SolBTN);
    btnPanel.add(ShutBTN);


    frame.add(btnPanel);

    frame.add(frame);

}




public static void main(String[] args) {

    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.pack();
    frame.setLocationRelativeTo(null);  
    frame.setSize(1200,400);
    frame.setVisible(true);


}

2 个答案:

答案 0 :(得分:2)

基本上,您将帧添加到另一个帧中,您根本无法做到

您还没有初始化按钮,导致NullPointerException

首先删除extends JFrame,这只是令人困惑的事情,作为一般规则,您应该避免从顶级容器扩展。相反,从JPanel开始,例如......

enter image description here

import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.EventQueue;
import java.awt.GridLayout;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;

public class EasyExpress extends JPanel {

    JButton WriteBTN, EmailBTN, SolBTN, ShutBTN;
    JPanel btnPanel;

    public EasyExpress() {

        btnPanel = new JPanel(new GridLayout(1, 4, 1, 1));
        btnPanel.setBounds(0, 0, 1200, 400);

        WriteBTN = new JButton("1");
        EmailBTN = new JButton("2");
        SolBTN = new JButton("3");
        ShutBTN = new JButton("4");

        WriteBTN.setPreferredSize(new Dimension(300, 400));
        EmailBTN.setPreferredSize(new Dimension(300, 400));
        SolBTN.setPreferredSize(new Dimension(300, 400));
        ShutBTN.setPreferredSize(new Dimension(300, 400));

        btnPanel.add(EmailBTN);
        btnPanel.add(WriteBTN);
        btnPanel.add(SolBTN);
        btnPanel.add(ShutBTN);

        setLayout(new BorderLayout());
        add(btnPanel);

    }

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

                JFrame frame = new JFrame("Testing");
                frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                frame.add(new EasyExpress());
                frame.pack();
                frame.setLocationRelativeTo(null);
                frame.setVisible(true);
            }
        });
    }
}

答案 1 :(得分:1)

与上面评论中提到的@MadProgrammer一样,你不能在另一个JFrame中有一个JFrame。

但是,我建议您删除EasyExpress对象中的JFrame,而不是删除extends JFrame。您已经在main中设置了该JFrame的所有属性,因此更容易修复。

  1. 删除JFrame frame = new JFrame("EASY BUTTONS");
  2. 将“EASY BUTTONS”添加到您在主EasyExpress main = new EasyExpress("EASY BUTTONS");
  3. 中创建的EasyExpress对象
  4. 移除框架。从frame.add(btnPanel);
  5. 前面