使用JDialog,参照Windows记事本编写一个“字体”对话框
时间: 2024-05-07 16:16:48 浏览: 94
字体对话框
好的,我可以为您提供一些关于JDialog的参考代码来帮助您实现这个“字体”对话框。请注意,以下代码仅供参考,您需要按照您的具体需求进行修改。
首先,您需要创建一个JDialog子类,例如:
```
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.*;
public class FontDialog extends JDialog {
// 字体选择器和预览文本框
private JLabel previewLabel;
private JComboBox<String> fontNameSelector;
private JComboBox<Integer> fontSizeSelector;
private JCheckBox boldCheckbox;
private JCheckBox italicCheckbox;
// 确定和取消按钮
private JButton okButton;
private JButton cancelButton;
// 对话框返回值
private String selectedFontName;
private int selectedFontSize;
private boolean selectedBold;
private boolean selectedItalic;
public FontDialog(Frame parentFrame, String title) {
super(parentFrame, title);
// 初始化字体选择器和预览文本框
previewLabel = new JLabel("Preview Text", JLabel.CENTER);
previewLabel.setFont(new Font("sans-serif", Font.PLAIN, 14));
fontNameSelector = new JComboBox<>(GraphicsEnvironment.getLocalGraphicsEnvironment().getAvailableFontFamilyNames());
fontSizeSelector = new JComboBox<>(new Integer[] { 8, 10, 12, 14, 16, 18, 20, 22, 24 });
boldCheckbox = new JCheckBox("Bold");
italicCheckbox = new JCheckBox("Italic");
// 初始化确定和取消按钮,并设置事件监听器
okButton = new JButton("OK");
cancelButton = new JButton("Cancel");
okButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
// 在“确定”按钮被点击时,将当前字体选择器的值保存为对话框的返回值
selectedFontName = (String) fontNameSelector.getSelectedItem();
selectedFontSize = (Integer) fontSizeSelector.getSelectedItem();
selectedBold = boldCheckbox.isSelected();
selectedItalic = italicCheckbox.isSelected();
setVisible(false);
}
});
cancelButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
// 在“取消”按钮被点击时,将对话框返回值设置为null,并隐藏对话框
selectedFontName = null;
selectedFontSize = 0;
selectedBold = false;
selectedItalic = false;
setVisible(false);
}
});
// 将组件添加到对话框中,并设置布局
JPanel previewPanel = new JPanel(new BorderLayout());
previewPanel.add(previewLabel, BorderLayout.CENTER);
previewPanel.setBorder(BorderFactory.createEmptyBorder(10, 10, 10, 10));
JPanel inputPanel = new JPanel(new GridLayout(4, 1, 5, 5));
inputPanel.setBorder(BorderFactory.createEmptyBorder(10, 10, 10, 10));
inputPanel.add(fontNameSelector);
inputPanel.add(fontSizeSelector);
inputPanel.add(boldCheckbox);
inputPanel.add(italicCheckbox);
JPanel buttonPanel = new JPanel(new FlowLayout(FlowLayout.RIGHT, 10, 10));
buttonPanel.setBorder(BorderFactory.createEmptyBorder(0, 10, 10, 10));
buttonPanel.add(okButton);
buttonPanel.add(cancelButton);
getContentPane().setLayout(new BorderLayout());
getContentPane().add(previewPanel, BorderLayout.CENTER);
getContentPane().add(inputPanel, BorderLayout.NORTH);
getContentPane().add(buttonPanel, BorderLayout.SOUTH);
pack();
setLocationRelativeTo(parentFrame);
}
public Font showDialog(Font defaultFont) {
// 设置字体选择器的默认值
fontNameSelector.setSelectedItem(defaultFont.getFamily());
fontSizeSelector.setSelectedItem(defaultFont.getSize());
boldCheckbox.setSelected(defaultFont.isBold());
italicCheckbox.setSelected(defaultFont.isItalic());
// 显示对话框,并等待对话框关闭
setVisible(true);
// 根据对话框返回值构造新的字体对象,并返回给调用者
if (selectedFontName != null) {
int style = (selectedBold ? Font.BOLD : 0) | (selectedItalic ? Font.ITALIC : 0);
return new Font(selectedFontName, style, selectedFontSize);
} else {
return null;
}
}
}
```
然后,您可以在应用程序中使用该对话框,例如:
```
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.*;
public class App {
public static void main(String[] args) {
JFrame frame = new JFrame("FontDialog Test");
JTextArea textArea = new JTextArea(10, 30);
textArea.setFont(new Font("sans-serif", Font.PLAIN, 14));
JButton fontButton = new JButton("Font...");
fontButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
// 创建一个新的FontDialog,并使用当前文本区域的字体作为默认值
FontDialog dialog = new FontDialog(frame, "Font Dialog");
Font defaultFont = textArea.getFont();
Font selectedFont = dialog.showDialog(defaultFont);
if (selectedFont != null) {
textArea.setFont(selectedFont);
}
}
});
JPanel panel = new JPanel(new BorderLayout());
panel.add(new JScrollPane(textArea), BorderLayout.CENTER);
panel.add(fontButton, BorderLayout.SOUTH);
frame.getContentPane().add(panel);
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
}
```
希望这个例子能够帮助您完成您的项目!
阅读全文