package test1;
import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
public class test5 extends JFrame {
private JLabel textLabel;
private JButton italicButton;
private JButton boldButton;
public test5() {
setTitle("Font Changer");
setSize(400, 300);
setLocationRelativeTo(null);
setDefaultCloseOperation(EXIT_ON_CLOSE);
JPanel panel = new JPanel();
textLabel = new JLabel("Hello World!");
textLabel.setFont(new Font("Serif", Font.PLAIN, 24));
panel.add(textLabel);
italicButton = new JButton("Italic");
italicButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
Font font = textLabel.getFont().deriveFont(Font.ITALIC);
textLabel.setFont(font);
}
});
panel.add(italicButton);
boldButton = new JButton("Bold");
boldButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
Font font = textLabel.getFont().deriveFont(Font.BOLD);
textLabel.setFont(font);
}
});
panel.add(boldButton);
add(panel);
}
public static void main(String[] args) {
test5 fontChanger = new test5();
fontChanger.setVisible(true);
}
}
![](https://profile-avatar.csdnimg.cn/default.jpg!4)
想实现图1的功能,但目前是图二的样子,要怎么改才可以改成图1的样子
- 写回答
- 好问题 0 提建议
- 追加酬金
- 关注问题
- 邀请回答
-
2条回答 默认 最新
- 创意程序员 2023-05-01 17:43关注
程序修改如下:
import javax.swing.*; import java.awt.*; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; public class JFrame1 extends JFrame implements ActionListener { private JLabel textLabel; private JRadioButton italicButton; private JRadioButton boldButton; public JFrame1() { super("Font Changer"); setSize(400, 300); // 创建面板和标签 JPanel panel = new JPanel(new BorderLayout()); textLabel = new JLabel("Hello World!", JLabel.CENTER); panel.add(textLabel, BorderLayout.CENTER); // 创建按钮组和单选按钮,并添加到面板中 ButtonGroup buttonGroup = new ButtonGroup(); italicButton = new JRadioButton("ITALIC"); italicButton.addActionListener(this); buttonGroup.add(italicButton); boldButton = new JRadioButton("BOLD"); boldButton.addActionListener(this); buttonGroup.add(boldButton); JPanel buttonPanel = new JPanel(); buttonPanel.add(italicButton); buttonPanel.add(boldButton); panel.add(buttonPanel, BorderLayout.SOUTH); // 将面板添加到窗口中,并设置窗口居中显示 add(panel); setLocationRelativeTo(null); } public void actionPerformed(ActionEvent e) { // 根据单选按钮的状态设置标签字体样式 Font font = textLabel.getFont(); if (italicButton.isSelected()) { font = font.deriveFont(Font.ITALIC); } else if (boldButton.isSelected()) { font = font.deriveFont(Font.BOLD); } else { font = font.deriveFont(Font.PLAIN); } textLabel.setFont(font); } public static void main(String[] args) { JFrame1 fontChanger = new JFrame1(); fontChanger.setVisible(true); } }
本回答被题主选为最佳回答 , 对您是否有帮助呢?解决 无用评论 打赏 举报 编辑记录
悬赏问题
- ¥15 而使用UE5引擎的 工具选项里 打开c++ visual studio 就会有部分显示加载失败 如图 加载失败的这张图 请问是什么原因
- ¥15 mysql 对多个字段模糊查询,返回第一个匹配的字段
- ¥15 the testing results of the whole dataset is empty
- ¥15 can问题,往哥解决
- ¥15 FFmpeg 成功推流到 Nginx RTMP 服务器但无法用 ffplay 或 VLC 播放
- ¥15 请修改以下C语言代码使其能正确输出最短路径
- ¥20 抖音商城拉码器安卓报错求解决办法或者有新的拉码脚本也可以介绍一下
- ¥15 MPLAB IDE V2.35 报错make[2]: *** [build/default/production/_ext/1472/MSSP_I2C.p1] Error 1
- ¥15 新唐M451 DTMF检测和发送代码修改
- ¥15 在国外文献网站里点击view pdf 加载异常缓慢甚至加载不出来。