我无法运行代码来创建简单的调查。我刚刚开始使用UI,BoxLayout给出了一个错误:线程“AWT-EventQueue-0”中的异常java.awt.AWTError:无法共享BoxLayout。帮助
import java.awt.Container;
import java.awt.Dimension;
import javax.swing.*;
public class UserInterface implements Runnable {
private JFrame frame;
@Override
public void run() {
frame = new JFrame("Survey");
frame.setPreferredSize(new Dimension(200, 300));
frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
createComponents(frame);
frame.pack();
frame.setVisible(true);
}
private void createComponents(Container container) {
BoxLayout bl = new BoxLayout(container, BoxLayout.Y_AXIS);
container.setLayout(bl);
container.add(new JLabel("Are you?"));
container.add(new JCheckBox("Yes!"));
container.add(new JCheckBox("No!"));
container.add(new JLabel("Why?"));
JRadioButton one = new JRadioButton("No reason.");
JRadioButton two = new JRadioButton("Because it is fun!");
ButtonGroup bg = new ButtonGroup();
bg.add(one);
bg.add(two);
container.add(one);
container.add(two);
container.add(new JButton("Done!"));
}
public JFrame getFrame() {
return frame;
}
}
答案 0 :(得分:1)
但是,有没有办法将这两种方法分开?
简单:只需将contentPane传递给方法。
{{1}}