jbutton设置字体居中
时间: 2023-07-01 07:26:04 浏览: 81
你可以使用以下代码将JButton中的文本居中显示:
```
JButton button = new JButton("Button Text");
button.setHorizontalAlignment(SwingConstants.CENTER);
button.setVerticalAlignment(SwingConstants.CENTER);
```
这将设置按钮文本在水平和垂直方向上居中显示。
相关问题
import javax.swing.*; import java.awt.*; public class Main extends JFrame { public Main() { setLayout(________);//设置窗体为流式布局,无参数默认为居中对齐 setFont(new Font("Helvetica",________, 14));//设置窗体中显示的字体样式,加粗,14号 getContentPane().add(new JButton("Button 1"));//将按钮添加到窗体中 getContentPane().add(new JButton("Button 2")); getContentPane().add(new JButton("Button3")); getContentPane().add(new JButton("Button 4")); } public static void main(String args[]) { Main window = new Main(); window.setTitle("________");//设置窗体标题 window.pack();//该代码依据放置的组件设定窗口的大小使之正好能容纳你放置的所有组件 window.setVisible(true); window.setDefaultCloseOperation(________);//设置窗体的默认关闭方式 window.setLocationRelativeTo(null); //让窗体居中显示 } }
import javax.swing.*;
import java.awt.*;
public class Main extends JFrame {
public Main() {
setLayout(new FlowLayout());//设置窗体为流式布局,无参数默认为居中对齐
setFont(new Font("Helvetica", Font.BOLD, 14));//设置窗体中显示的字体样式,加粗,14号
getContentPane().add(new JButton("Button 1"));//将按钮添加到窗体中
getContentPane().add(new JButton("Button 2"));
getContentPane().add(new JButton("Button3"));
getContentPane().add(new JButton("Button 4"));
}
public static void main(String args[]) {
Main window = new Main();
window.setTitle("My Window");//设置窗体标题
window.pack();//该代码依据放置的组件设定窗口的大小使之正好能容纳你放置的所有组件
window.setVisible(true);
window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);//设置窗体的默认关闭方式
window.setLocationRelativeTo(null); //让窗体居中显示
}
}
java设计界面并实现猜数字游戏 要求: 1.窗体大小:长400,宽200 2.界面第一行:猜数字游戏标题字体稍微调大 3.中间界面:请输入您猜的数字和显示框(居中) 4.最下面界面:设置确定和取消按钮 5.猜数字范围1~100
好的,这是一个需要用 Java Swing 来实现的猜数字游戏界面的设计方案:
1. 创建 JFrame 对象,并设置窗口大小为 400x200。
2. 在 JFrame 中添加一个 JPanel,用于容纳猜数字游戏的界面元素。
3. 在 JPanel 中添加一个 JLabel,用于显示猜数字游戏的标题。设置字体大小为稍微调大的字体。
4. 在 JPanel 中添加一个 JTextField,用于用户输入猜测的数字。设置文本框的位置为居中。
5. 在 JPanel 中添加一个 JTextArea,用于显示猜测结果和游戏提示信息。设置文本框的位置为居中。
6. 在 JPanel 中添加一个 JButton,用于用户点击确认猜测。
7. 在 JPanel 中添加一个 JButton,用于用户点击取消猜测。
8. 设置猜测数字的范围为 1~100。
以下是 Java 代码示例:
```
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class GuessNumberGame extends JFrame {
private JPanel panel;
private JLabel titleLabel;
private JTextField guessField;
private JTextArea resultArea;
private JButton confirmButton;
private JButton cancelButton;
private int randomNumber;
public GuessNumberGame() {
super("猜数字游戏");
setSize(400, 200);
panel = new JPanel();
panel.setLayout(new BoxLayout(panel, BoxLayout.Y_AXIS));
add(panel);
titleLabel = new JLabel("猜数字游戏");
titleLabel.setFont(new Font("微软雅黑", Font.BOLD, 20));
titleLabel.setAlignmentX(Component.CENTER_ALIGNMENT);
panel.add(titleLabel);
guessField = new JTextField();
guessField.setMaximumSize(new Dimension(200, 30));
guessField.setAlignmentX(Component.CENTER_ALIGNMENT);
panel.add(guessField);
resultArea = new JTextArea();
resultArea.setEditable(false);
resultArea.setMaximumSize(new Dimension(200, 120));
resultArea.setAlignmentX(Component.CENTER_ALIGNMENT);
panel.add(resultArea);
confirmButton = new JButton("确定");
confirmButton.setAlignmentX(Component.CENTER_ALIGNMENT);
confirmButton.addActionListener(new ConfirmListener());
panel.add(confirmButton);
cancelButton = new JButton("取消");
cancelButton.setAlignmentX(Component.CENTER_ALIGNMENT);
cancelButton.addActionListener(new CancelListener());
panel.add(cancelButton);
randomNumber = (int)(Math.random() * 100) + 1;
}
private class ConfirmListener implements ActionListener {
public void actionPerformed(ActionEvent event) {
try {
int guessNumber = Integer.parseInt(guessField.getText());
if (guessNumber < 1 || guessNumber > 100) {
resultArea.setText("请输入1~100的数字!");
} else if (guessNumber < randomNumber) {
resultArea.setText("您猜的数字太小了!");
} else if (guessNumber > randomNumber) {
resultArea.setText("您猜的数字太大了!");
} else {
resultArea.setText("恭喜您,猜对了!");
}
} catch (NumberFormatException e) {
resultArea.setText("请输入1~100的数字!");
}
}
}
private class CancelListener implements ActionListener {
public void actionPerformed(ActionEvent event) {
System.exit(0);
}
}
public static void main(String[] args) {
GuessNumberGame game = new GuessNumberGame();
game.setVisible(true);
}
}
```
在这个示例中,我们使用了 BoxLayout 布局管理器,使得界面元素能够居中显示。同时,我们使用了 ActionListener 接口来处理确认和取消按钮的点击事件,使得用户能够进行猜数字游戏。
相关推荐
![ppt](https://img-home.csdnimg.cn/images/20210720083527.png)
![pptx](https://img-home.csdnimg.cn/images/20210720083543.png)
![doc](https://img-home.csdnimg.cn/images/20210720083327.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)