Java JFrame中没有显示任何内容
我有一个[JFrame][1]的课程,但是什么都没有出现。但是,如果我删除setLayout,则会显示临时面板,但如果我添加其他组件,则不会显示临时面板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
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(临时),因为我将有更多的面板。是的,我似乎打字速度很慢;)看来,你计划了更复杂的事情。您可以使用布局管理器