Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/349.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java JFrame中没有显示任何内容_Java_Swing - Fatal编程技术网

Java JFrame中没有显示任何内容

Java JFrame中没有显示任何内容,java,swing,Java,Swing,我有一个[JFrame][1]的课程,但是什么都没有出现。但是,如果我删除setLayout,则会显示临时面板,但如果我添加其他组件,则不会显示临时面板 public class GenerateQuestions extends JFrame { List<ButtonGroup> btngp; GenerateQuestions() { setSize(400, 400); btngp = new LinkedList<ButtonGrou

我有一个[JFrame][1]的课程,但是什么都没有出现。但是,如果我删除setLayout,则会显示临时面板,但如果我添加其他组件,则不会显示临时面板

public class GenerateQuestions extends JFrame {
    List<ButtonGroup> btngp;

    GenerateQuestions() {
    setSize(400, 400);
    btngp = new LinkedList<ButtonGroup>();

    String[] contents =
    {
        "Test1", "Test2", "Test3", "Test4"
    };
    SpringLayout z = new SpringLayout();
    setLayout(z); 
    JPanel temp = new JPanel(); 
    SpringLayout sl = new SpringLayout();
    temp.setLayout(sl);
    JLabel x = new JLabel("This is a test question generator for online assesment ");
    temp.add(x);
    sl.putConstraint(SpringLayout.NORTH, x, 5, SpringLayout.NORTH, temp);
    sl.putConstraint(SpringLayout.WEST, x, 5, SpringLayout.WEST, temp); 

    ButtonGroup btn = new ButtonGroup();
    int counterNorth = 15;
    int counterWest = 15;
    for (int i = 0; i < contents.length; i++) {
        JRadioButton t = new JRadioButton(contents[i]);
        btn.add(t);
        temp.add(t);
        sl.putConstraint(SpringLayout.NORTH, t, counterNorth, SpringLayout.NORTH, x);
        sl.putConstraint(SpringLayout.WEST, t, counterWest, SpringLayout.WEST, temp);
        if (i % 2 == 0) {
        counterWest += 75;
        } else {
        counterWest -= 75;
        counterNorth += 25;
        }
    }
    btngp.add(btn);
    z.putConstraint(SpringLayout.NORTH, temp, 10, SpringLayout.NORTH, this);
    z.putConstraint(SpringLayout.WEST, temp, 10, SpringLayout.WEST, this);
    setVisible(true);
    getContentPane().add(temp);
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
    }

    public static void main(String[] args) {
        new GenerateQuestions();
    }
}
公共类GenerateQuestions扩展了JFrame{
列出btngp;
GenerateQuestions(){
设置大小(400400);
btngp=新链接列表();
字符串[]内容=
{
“测试1”、“测试2”、“测试3”、“测试4”
};
SpringLayout z=新的SpringLayout();
设置布局(z);
JPanel temp=新的JPanel();
SpringLayout sl=新的SpringLayout();
温度设置布局(sl);
JLabel x=新的JLabel(“这是一个用于在线评估的试题生成器”);
温度加成(x);
sl.putConstraint(SpringLayout.NORTH,x,5,SpringLayout.NORTH,temp);
sl.putConstraint(SpringLayout.WEST,x,5,SpringLayout.WEST,temp);
ButtonGroup btn=新的ButtonGroup();
int反向北=15;
int反西方=15;
for(int i=0;i
您将所有组件添加到
temp
面板,但从未将该面板添加到
JFrame
。添加此行
setContentPane(临时)
设置可见之前(true)和所有将工作

编辑:如果要向
JFrame
添加多个面板,请使用
LayoutManager
例如
GridLayout

    getContentPane().setLayout(new GridLayout(2,2));
    getContentPane().add(temp);
    getContentPane().add(new JLabel("test"));
或者您可以像这样使用
BorderLayout
getContentPane().setLayout(newborderlayout())它还可以帮助您

默认情况下,您的
getContentPane()
面板具有
FlowLayout
,无法显示具有preferedSize(0,0)的组件。如果要使用它,必须指定组件的大小,例如:
temp.setPreferredSize(新维度(100100))


您将所有组件添加到
temp
面板,但从未将该面板添加到
JFrame
。添加此行
setContentPane(临时)
设置可见之前(true)和所有将工作

编辑:如果要向
JFrame
添加多个面板,请使用
LayoutManager
例如
GridLayout

    getContentPane().setLayout(new GridLayout(2,2));
    getContentPane().add(temp);
    getContentPane().add(new JLabel("test"));
或者您可以像这样使用
BorderLayout
getContentPane().setLayout(newborderlayout())它还可以帮助您

默认情况下,您的
getContentPane()
面板具有
FlowLayout
,无法显示具有preferedSize(0,0)的组件。如果要使用它,必须指定组件的大小,例如:
temp.setPreferredSize(新维度(100100))


您将所有组件添加到
temp
面板,但从未将该面板添加到
JFrame
。添加此行
setContentPane(临时)
设置可见之前(true)和所有将工作

编辑:如果要向
JFrame
添加多个面板,请使用
LayoutManager
例如
GridLayout

    getContentPane().setLayout(new GridLayout(2,2));
    getContentPane().add(temp);
    getContentPane().add(new JLabel("test"));
或者您可以像这样使用
BorderLayout
getContentPane().setLayout(newborderlayout())它还可以帮助您

默认情况下,您的
getContentPane()
面板具有
FlowLayout
,无法显示具有preferedSize(0,0)的组件。如果要使用它,必须指定组件的大小,例如:
temp.setPreferredSize(新维度(100100))


您将所有组件添加到
temp
面板,但从未将该面板添加到
JFrame
。添加此行
setContentPane(临时)
设置可见之前(true)和所有将工作

编辑:如果要向
JFrame
添加多个面板,请使用
LayoutManager
例如
GridLayout

    getContentPane().setLayout(new GridLayout(2,2));
    getContentPane().add(temp);
    getContentPane().add(new JLabel("test"));
或者您可以像这样使用
BorderLayout
getContentPane().setLayout(newborderlayout())它还可以帮助您

默认情况下,您的
getContentPane()
面板具有
FlowLayout
,无法显示具有preferedSize(0,0)的组件。如果要使用它,必须指定组件的大小,例如:
temp.setPreferredSize(新维度(100100))


您应该将临时面板设置为JFrame的内容窗格


在显示带有
setVisible(true)的帧之前

您应该将临时面板设置为JFrame的内容窗格


在显示带有
setVisible(true)的帧之前

您应该将临时面板设置为JFrame的内容窗格


在显示带有
setVisible(true)的帧之前

您应该将临时面板设置为JFrame的内容窗格


在显示带有
setVisible(true)的帧之前

我错过了这一部分。对不起,我已经添加了它,但什么也没有发生。而且我无法设置ContentPane(临时),因为我将有更多的面板。是的,我似乎打字速度很慢;)看来,你计划了更复杂的事情。您可以使用布局管理器