2017-09-20 90 views
0

我似乎无法弄清楚为什么我的JFrame是空的。我哪里错了?为什么我的JFrame是空的?

import javax.swing。*; import java.awt.FlowLayout;

公共类GUIExample扩展的JFrame {

JCheckBox box1 = new JCheckBox("Satellite Radio"); 
JCheckBox box2 = new JCheckBox("Air Conditioning"); 
JCheckBox box3 = new JCheckBox("Manual Tranmission"); 
JCheckBox box4 = new JCheckBox("Leather Seats"); 
JRadioButton radio1 = new JRadioButton("Car"); 
JRadioButton radio2 = new JRadioButton("Pickup Truck"); 
JRadioButton radio3 = new JRadioButton("Minivan"); 
JTextField text = new JTextField(); 
ButtonGroup group = new ButtonGroup(); 

public void newGUI() { 

    setLayout(new FlowLayout()); 
    JPanel panel = new JPanel(); 
    JPanel textPanel = new JPanel(); 

    add(textPanel); 
    add(panel); 

    panel.add(box1); 
    panel.add(box2); 
    panel.add(box3); 
    panel.add(radio1); 
    panel.add(radio2); 
    panel.add(radio3); 
    group.add(radio1); 
    group.add(radio2); 
    group.add(radio3); 

} 

public static void main(String[] args) { 

    JFrame frame = new JFrame("GUI Example"); 
    frame.setDefaultCloseOperation(frame.EXIT_ON_CLOSE); 
    frame.setSize(500, 500); 
    frame.setVisible(true); 

} 

}

回答

0

你忘了添加的contentPane在你的JFrame,像这样

frame.setContentPane(panel); 

我你使用继承通知构建你的jFrame,所以在这种情况下你需要实例化你自己的类。我用minimun重构了你的代码来运行一个jFrame。

public class GUIExample extends JFrame { 

    JCheckBox box1 = new JCheckBox("Satellite Radio"); 

    public static void main(String[] args) { 
     JFrame frame = new GUIExample("GUI Example"); 
     JPanel panel = new JPanel(); 
     panel.setLayout(new FlowLayout()); 
     panel.add(box1); 

     frame.setContentPane(panel); 
     frame.setDefaultCloseOperation(frame.EXIT_ON_CLOSE); 
     frame.setSize(500, 500); 
     frame.setVisible(true); 
    } 
} 

基本上你创建的JFrame,创建一个JPanel中,添加组件此面板和面板与setContentPane(panel)设置为你的框架。

我很抱歉,我现在不能测试此权利,所以如果有人可以,如果需要修复,会很感激,但这样的事情。

+0

不客气=) – renanpallin

0

这一切首先是一个相当好的尝试你正在尝试做的。但是,对于如何编写GUI,似乎存在一些基本的误解。

在java中有两种制作GUI的方法。您可以创建框架和面板对象,然后添加您的组件,或者您可以创建和扩展自己的类extends JFrame。但在这种情况下,你已经试图做到这一点。您已经扩展了JFrame并且已经创建了这些对象。

其实我挺喜欢你如何试图扩展JFrame的,所以我一直在继续这一点,并提出,添加的东西到你的屏幕代码!

见下文 - GUIExample.java:

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

public class GUIExample extends JFrame { 

    JCheckBox box1 = new JCheckBox("Satellite Radio"); 
    JCheckBox box2 = new JCheckBox("Air Conditioning"); 
    JCheckBox box3 = new JCheckBox("Manual Tranmission"); 
    JCheckBox box4 = new JCheckBox("Leather Seats"); 
    JRadioButton radio1 = new JRadioButton("Car"); 
    JRadioButton radio2 = new JRadioButton("Pickup Truck"); 
    JRadioButton radio3 = new JRadioButton("Minivan"); 
    JTextField text = new JTextField(); 
    ButtonGroup group = new ButtonGroup(); 

    GUIExample() { 
     setDefaultCloseOperation(EXIT_ON_CLOSE); 
     setSize(500, 500); 
     setVisible(true); 

     newGUI(); 
    } 


    public void newGUI() { 

     setLayout(new FlowLayout()); 
     JPanel panel = new JPanel(); 
     JPanel textPanel = new JPanel(); 

     add(textPanel); 
     add(panel); 

     panel.add(box1); 
     panel.add(box2); 
     panel.add(box3); 
     panel.add(radio1); 
     panel.add(radio2); 
     panel.add(radio3); 
     group.add(radio1); 
     group.add(radio2); 
     group.add(radio3); 

    } 

} 

test.java:

public class test { 

    public static void main(String[] args) { 
     GUIExample e = new GUIExample(); 
    } 
} 

得到这个工作。编译这两个文件,然后通过test.java运行你的程序。

而且你原来的解决方案,无处你添加一个newGUI()调用,这样所有的代码变得多余。但是,由于主要方法是静态的,因此无论如何您都无法调用它。

我希望这有助于!

+0

有道理,谢谢。 –

+0

不客气。 –