Java窗体中加入字体样式菜单,有多种字体可选择

时间: 2024-01-28 22:04:42 浏览: 22
要在Java窗体中加入字体样式菜单,可以使用JMenuBar和JMenu类创建菜单栏和菜单项。然后,使用Font类设置字体样式,将其应用于所选文本。 以下是一个简单的示例代码,演示如何在Java窗体中添加字体样式菜单: ``` import java.awt.*; import java.awt.event.*; import javax.swing.*; public class FontMenuExample extends JFrame { private JTextArea textArea; private JMenuBar menuBar; private JMenu fontMenu; private JMenu styleMenu; private JMenu sizeMenu; public FontMenuExample() { setTitle("Font Menu Example"); setSize(500, 500); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); textArea = new JTextArea(); JScrollPane scrollPane = new JScrollPane(textArea); getContentPane().add(scrollPane, BorderLayout.CENTER); menuBar = new JMenuBar(); setJMenuBar(menuBar); fontMenu = new JMenu("Font"); menuBar.add(fontMenu); String[] fontNames = GraphicsEnvironment.getLocalGraphicsEnvironment().getAvailableFontFamilyNames(); for (String fontName : fontNames) { JMenuItem fontItem = new JMenuItem(fontName); fontItem.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { Font currentFont = textArea.getFont(); Font newFont = new Font(fontName, currentFont.getStyle(), currentFont.getSize()); textArea.setFont(newFont); } }); fontMenu.add(fontItem); } styleMenu = new JMenu("Style"); menuBar.add(styleMenu); JMenuItem plainItem = new JMenuItem("Plain"); plainItem.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { Font currentFont = textArea.getFont(); Font newFont = new Font(currentFont.getName(), Font.PLAIN, currentFont.getSize()); textArea.setFont(newFont); } }); styleMenu.add(plainItem); JMenuItem boldItem = new JMenuItem("Bold"); boldItem.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { Font currentFont = textArea.getFont(); Font newFont = new Font(currentFont.getName(), Font.BOLD, currentFont.getSize()); textArea.setFont(newFont); } }); styleMenu.add(boldItem); JMenuItem italicItem = new JMenuItem("Italic"); italicItem.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { Font currentFont = textArea.getFont(); Font newFont = new Font(currentFont.getName(), Font.ITALIC, currentFont.getSize()); textArea.setFont(newFont); } }); styleMenu.add(italicItem); sizeMenu = new JMenu("Size"); menuBar.add(sizeMenu); for (int i = 8; i <= 24; i += 2) { JMenuItem sizeItem = new JMenuItem(String.valueOf(i)); sizeItem.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { Font currentFont = textArea.getFont(); Font newFont = new Font(currentFont.getName(), currentFont.getStyle(), i); textArea.setFont(newFont); } }); sizeMenu.add(sizeItem); } } public static void main(String[] args) { FontMenuExample frame = new FontMenuExample(); frame.setVisible(true); } } ``` 在这个例子中,我们创建了一个JTextArea和一个JScrollPane,将其添加到窗体中央。然后,我们创建了一个JMenuBar和三个JMenu:Font、Style和Size。Font菜单包含所有可用的字体,Style菜单包含三个样式选项:普通、粗体和斜体。Size菜单包含可用的字体大小选项。 每个菜单项都添加了一个ActionListener,用于根据用户选择更新文本区域的字体样式。在ActionListener中,我们使用Font类创建一个新的字体,将其应用于文本区域。

相关推荐

最新推荐

recommend-type

WPF实现窗体中的悬浮按钮

主要为大家详细介绍了WPF实现窗体中的悬浮按钮,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
recommend-type

在类库或winform项目中打开另一个winform项目窗体的方法

主要介绍了在类库或winform项目中打开另一个winform项目窗体的方法,可以实现Winform项目间窗体的调用,在进行Winform项目开发中非常具有实用价值,需要的朋友可以参考下
recommend-type

C++获得其他程序窗体控件中信息的方法

主要介绍了C++获得其他程序窗体控件中信息的方法,涉及windows控件句柄的获取技巧,需要的朋友可以参考下
recommend-type

java_swing窗体布局

java_swing窗体布局,很实用。上传个大家做个分享。找了好久才找到的。
recommend-type

C#Button窗体常用属性及事件详解

1.常用属性 Name:名称; BackColor:设置控件背景颜色; Enabled:是否可用; FlayStyle:控件样式; Image:设置控件图像;...在真实的C#开发场景中,我们对于按钮的使用,无非就是使用按钮的name属性、
recommend-type

zigbee-cluster-library-specification

最新的zigbee-cluster-library-specification说明文档。
recommend-type

管理建模和仿真的文件

管理Boualem Benatallah引用此版本:布阿利姆·贝纳塔拉。管理建模和仿真。约瑟夫-傅立叶大学-格勒诺布尔第一大学,1996年。法语。NNT:电话:00345357HAL ID:电话:00345357https://theses.hal.science/tel-003453572008年12月9日提交HAL是一个多学科的开放存取档案馆,用于存放和传播科学研究论文,无论它们是否被公开。论文可以来自法国或国外的教学和研究机构,也可以来自公共或私人研究中心。L’archive ouverte pluridisciplinaire
recommend-type

实现实时数据湖架构:Kafka与Hive集成

![实现实时数据湖架构:Kafka与Hive集成](https://img-blog.csdnimg.cn/img_convert/10eb2e6972b3b6086286fc64c0b3ee41.jpeg) # 1. 实时数据湖架构概述** 实时数据湖是一种现代数据管理架构,它允许企业以低延迟的方式收集、存储和处理大量数据。与传统数据仓库不同,实时数据湖不依赖于预先定义的模式,而是采用灵活的架构,可以处理各种数据类型和格式。这种架构为企业提供了以下优势: - **实时洞察:**实时数据湖允许企业访问最新的数据,从而做出更明智的决策。 - **数据民主化:**实时数据湖使各种利益相关者都可
recommend-type

2. 通过python绘制y=e-xsin(2πx)图像

可以使用matplotlib库来绘制这个函数的图像。以下是一段示例代码: ```python import numpy as np import matplotlib.pyplot as plt def func(x): return np.exp(-x) * np.sin(2 * np.pi * x) x = np.linspace(0, 5, 500) y = func(x) plt.plot(x, y) plt.xlabel('x') plt.ylabel('y') plt.title('y = e^{-x} sin(2πx)') plt.show() ``` 运行这段
recommend-type

JSBSim Reference Manual

JSBSim参考手册,其中包含JSBSim简介,JSBSim配置文件xml的编写语法,编程手册以及一些应用实例等。其中有部分内容还没有写完,估计有生之年很难看到完整版了,但是内容还是很有参考价值的。