// 随机选择当前成语 currentChengYu = getRandomChengyu(chengYuList); currentIdiom=currentChengYu.getChengYu(); // 初始化窗口 setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); setBounds(100, 100, 450, 300); contentPane = new JPanel(); contentPane.setBorder(new EmptyBorder(5, 5, 5, 5)); setContentPane(contentPane); contentPane.setLayout(null); lblNewLabel = new JLabel("请输入一个成语:"); lblNewLabel.setBounds(10, 10, 150, 30); lblNewLabel.setFont(new Font("宋体", Font.PLAIN, 16)); contentPane.add(lblNewLabel); textField = new JTextField(); textField.setBounds(160, 10, 200, 30); textField.setFont(new Font("宋体", Font.PLAIN, 16)); contentPane.add(textField); textField.setColumns(10); lblNewLabel_1 = new JLabel("当前成语:"); lblNewLabel_1.setBounds(10, 50, 150, 30); lblNewLabel_1.setFont(new Font("宋体", Font.PLAIN, 16)); contentPane.add(lblNewLabel_1); lblNewLabel_2 = new JLabel(currentIdiom); lblNewLabel_2.setBounds(160, 50, 200, 30); lblNewLabel_2.setFont(new Font("宋体", Font.PLAIN, 16)); contentPane.add(lblNewLabel_2); btnNewButton = new JButton("提交"); btnNewButton.setBounds(160, 90, 100, 30); btnNewButton.setFont(new Font("宋体", Font.PLAIN, 16));游戏界面有以上组件,请修改代码,使它出现在游戏界面里,与首页在同一窗口
时间: 2024-03-21 22:43:45 浏览: 102
修改代码如下:
```
// 随机选择当前成语
currentChengYu = getRandomChengyu(chengYuList);
currentIdiom=currentChengYu.getChengYu();
// 初始化窗口
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setBounds(100, 100, 450, 300);
JPanel contentPane = new JPanel();
contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
setContentPane(contentPane);
contentPane.setLayout(null);
// 添加游戏界面组件
JLabel lblNewLabel = new JLabel("请输入一个成语:");
lblNewLabel.setBounds(10, 10, 150, 30);
lblNewLabel.setFont(new Font("宋体", Font.PLAIN, 16));
contentPane.add(lblNewLabel);
JTextField textField = new JTextField();
textField.setBounds(160, 10, 200, 30);
textField.setFont(new Font("宋体", Font.PLAIN, 16));
contentPane.add(textField);
textField.setColumns(10);
JLabel lblNewLabel_1 = new JLabel("当前成语:");
lblNewLabel_1.setBounds(10, 50, 150, 30);
lblNewLabel_1.setFont(new Font("宋体", Font.PLAIN, 16));
contentPane.add(lblNewLabel_1);
JLabel lblNewLabel_2 = new JLabel(currentIdiom);
lblNewLabel_2.setBounds(160, 50, 200, 30);
lblNewLabel_2.setFont(new Font("宋体", Font.PLAIN, 16));
contentPane.add(lblNewLabel_2);
JButton btnNewButton = new JButton("提交");
btnNewButton.setBounds(160, 90, 100, 30);
btnNewButton.setFont(new Font("宋体", Font.PLAIN, 16));
contentPane.add(btnNewButton);
```
这样就可以将游戏界面组件添加到同一窗口中了。
阅读全文