SimpleButton.setBounds(120, 180, 200, 50);想把按钮改小一点
时间: 2024-03-21 07:42:32 浏览: 23
如果你想让按钮变小,可以调整 setBounds() 方法中的后两个参数,即宽度和高度。例如,将宽度和高度都减小 10:
```
SimpleButton.setBounds(120, 180, 190, 40);
```
这样按钮的大小就会变为 190 x 40。你可以根据需要调整参数值,使按钮大小符合你的要求。
相关问题
// 设置背景图片 ImageIcon background = new ImageIcon("D:\\yy\\java\\成语接龙\\background.png"); backgroundLabel = new JLabel(background); backgroundLabel.setBounds(0, 0, background.getIconWidth(), background.getIconHeight()); panel.add(backgroundLabel); // 创建娱乐模式按钮 SimpleButton = new JButton("娱乐模式"); SimpleButton.setBounds(120, 180, 180,50); SimpleButton.setFont(new Font("楷体", Font.BOLD, 20)); SimpleButton.setForeground(Color.WHITE); SimpleButton.setBackground(new Color(88,33,22)); SimpleButton.setBorder(BorderFactory.createLineBorder(new Color(100,33,100), 2)); SimpleButton.addActionListener(this); backgroundLabel.add(SimpleButton); // 创建挑战模式按钮 HardButton = new JButton("挑战模式"); HardButton.setBounds(120, 260,180, 50); HardButton.setFont(new Font("楷体", Font.BOLD, 20)); HardButton.setForeground(Color.WHITE); HardButton.setBackground(new Color(88,33,22)); HardButton.setBorder(BorderFactory.createLineBorder(new Color(100,33,100), 2)); HardButton.addActionListener(this); backgroundLabel.add(HardButton);修改这串代码,要求添上大标题“成语接龙”,标题下方滚动一行字幕为“欢迎来到成语接龙智力游戏,你可以选择两个模式:”,再下面竖着排列“娱乐模式”和“挑战模式”按钮,还有“退出”按钮点击会关闭窗口,
你可以按照以下方式修改代码,来满足你的需求:
```
// 设置背景图片
ImageIcon background = new ImageIcon("D:\\yy\\java\\成语接龙\\background.png");
backgroundLabel = new JLabel(background);
backgroundLabel.setBounds(0, 0, background.getIconWidth(), background.getIconHeight());
panel.add(backgroundLabel);
// 添加大标题“成语接龙”
JLabel titleLabel = new JLabel("成语接龙", SwingConstants.CENTER);
titleLabel.setFont(new Font("楷体", Font.BOLD, 30));
titleLabel.setBounds(0, 20, 600, 50);
backgroundLabel.add(titleLabel);
// 添加欢迎词
JLabel welcomeLabel = new JLabel("欢迎来到成语接龙智力游戏,你可以选择两个模式:", SwingConstants.CENTER);
welcomeLabel.setFont(new Font("楷体", Font.PLAIN, 18));
welcomeLabel.setBounds(0, 80, 600, 30);
backgroundLabel.add(welcomeLabel);
// 创建娱乐模式按钮
SimpleButton = new JButton("娱乐模式");
SimpleButton.setBounds(200, 140, 180, 50);
SimpleButton.setFont(new Font("楷体", Font.BOLD, 20));
SimpleButton.setForeground(Color.WHITE);
SimpleButton.setBackground(new Color(88,33,22));
SimpleButton.setBorder(BorderFactory.createLineBorder(new Color(100,33,100), 2));
SimpleButton.addActionListener(this);
backgroundLabel.add(SimpleButton);
// 创建挑战模式按钮
HardButton = new JButton("挑战模式");
HardButton.setBounds(200, 200, 180, 50);
HardButton.setFont(new Font("楷体", Font.BOLD, 20));
HardButton.setForeground(Color.WHITE);
HardButton.setBackground(new Color(88,33,22));
HardButton.setBorder(BorderFactory.createLineBorder(new Color(100,33,100), 2));
HardButton.addActionListener(this);
backgroundLabel.add(HardButton);
// 创建退出按钮
JButton exitButton = new JButton("退出");
exitButton.setBounds(260, 280, 80, 30);
exitButton.setFont(new Font("楷体", Font.PLAIN, 16));
exitButton.setForeground(Color.WHITE);
exitButton.setBackground(new Color(88,33,22));
exitButton.setBorder(BorderFactory.createLineBorder(new Color(100,33,100), 2));
exitButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
System.exit(0);
}
});
backgroundLabel.add(exitButton);
```
在这段代码中,我添加了一个大标题和欢迎词,用 JLabel 组件实现。然后将娱乐模式和挑战模式按钮移到了标题下方,并按照需求进行了样式设置。最后添加了一个退出按钮,点击会关闭窗口。
注意,如果你没有找到原来的退出按钮代码,可能需要自己添加一个 JFrame 的 WindowListener,来监听窗口关闭事件,然后在事件处理函数中调用 System.exit(0) 来关闭窗口。
// 设置背景图片 ImageIcon background = new ImageIcon("D:\\yy\\java\\成语接龙\\background.png"); backgroundLabel = new JLabel(background); backgroundLabel.setBounds(0, 0, background.getIconWidth(), background.getIconHeight()); panel.add(backgroundLabel); SimpleButton = new JButton("娱乐模式"); SimpleButton.setBounds(100, 100, 200, 50); SimpleButton.addActionListener(this); backgroundLabel.add(SimpleButton); HardButton = new JButton("挑战模式"); HardButton.setBounds(100, 200, 200, 50); HardButton.addActionListener(this); backgroundLabel.add(HardButton); add(panel); setSize(background.getIconWidth(), background.getIconHeight()); setLocationRelativeTo(null); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); setResizable(false); setVisible(true);修改这串代码,要求把界面缩小
您可以在最后添加如下代码来缩小界面:
```
setSize(background.getIconWidth()/2, background.getIconHeight()/2);
```
这样就可以把界面缩小一半了。完整代码如下:
```
// 设置背景图片
ImageIcon background = new ImageIcon("D:\\yy\\java\\成语接龙\\background.png");
backgroundLabel = new JLabel(background);
backgroundLabel.setBounds(0, 0, background.getIconWidth(), background.getIconHeight());
panel.add(backgroundLabel);
SimpleButton = new JButton("娱乐模式");
SimpleButton.setBounds(100, 100, 200, 50);
SimpleButton.addActionListener(this);
backgroundLabel.add(SimpleButton);
HardButton = new JButton("挑战模式");
HardButton.setBounds(100, 200, 200, 50);
HardButton.addActionListener(this);
backgroundLabel.add(HardButton);
add(panel);
setSize(background.getIconWidth()/2, background.getIconHeight()/2);
setLocationRelativeTo(null);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setResizable(false);
setVisible(true);
```