呸。我在这个问题上卡了这么久。我正在做一个模拟流行测验的 GUI 程序。但是当我希望我的程序像这样时,我不知道要放什么代码......
当我点击开始按钮时,面板应该是这样的......
到目前为止,这就是我的启动菜单...
public static void main (String []args){
JFrame f = new JFrame("Pop Quiz");
f.setSize(400,300);
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.setLayout(null);
f.setResizable(false);
JPanel p1 = new JPanel();
p1.setSize(400,100);
p1.setLocation(0,0);
p1.setLayout(new GridLayout(3,1));
f.add(p1);
JLabel l1 = new JLabel("Welcome to POP Quiz!");
p1.add(l1);
JLabel l2 = new JLabel("Enter your name:");
p1.add(l2);
final JTextField name = new JTextField ();
p1.add(name);
JPanel p2 = new JPanel();
p2.setSize(400,50);
p2.setLocation(0,225);
f.add(p2);
JButton start = new JButton ("Start");
p2.add(start);
start.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
String player = name.getText();
//what should be added here to change the contents of the panel?
}
});
f.show();
}
而对于问题...
public static void main(String[] args){
JFrame f = new JFrame("Pop Quiz");
f.setSize(400,300);
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.setLayout(null);
f.setResizable(false);
JPanel p1 = new JPanel();
p1.setSize(400,100);
p1.setBackground(Color.PINK);
f.add(p1);
JLabel question = new JLabel();
question.setText("In computers, what is the smallest and basic unit of information storage?");
p1.add(question);
JPanel p2 = new JPanel();
p2.setSize(400,175);
p2.setLocation(0,100);
p2.setLayout(new GridLayout(2,4));
f.add(p2);
JButton a = new JButton("a. Bit");
p2.add(a);
JButton b = new JButton("b. Byte");
p2.add(b);
JButton c = new JButton("c. Data");
p2.add(c);
JButton d = new JButton("d. Newton");
p2.add(d);
f.show();
}
我任何人都可以提供帮助,我将不胜感激。提前致谢!祝你今天过得愉快!:)