使用JDialog,参照Windows记事本编写一个“字体”对话框
时间: 2024-06-10 20:05:44 浏览: 166
好的,我可以帮你回答这个问题。
使用JDialog可以创建一个自定义的对话框,在这个对话框中我们可以添加各种组件以达到我们的需求。
下面是参照Windows记事本编写的一个“字体”对话框的示例代码:
```java
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.border.*;
import javax.swing.event.*;
public class FontDialog extends JDialog {
private JLabel fontLabel, styleLabel, sizeLabel, previewLabel;
private JComboBox fontComboBox, styleComboBox, sizeComboBox;
private JTextArea previewTextArea;
private JButton okButton, cancelButton;
private Font font;
private String[] fontList = GraphicsEnvironment.getLocalGraphicsEnvironment().getAvailableFontFamilyNames();
private String[] styleList = {"常规", "粗体", "斜体", "粗斜体"};
private String[] sizeList = {"8", "9", "10", "11", "12", "14", "16", "18", "20", "22", "24", "26", "28", "36", "48", "72"};
public FontDialog(Frame parent, Font font) {
super(parent, "字体", true);
this.font = font;
JPanel mainPanel = new JPanel(new BorderLayout());
JPanel fontPanel = new JPanel();
JPanel stylePanel = new JPanel();
JPanel sizePanel = new JPanel();
JPanel previewPanel = new JPanel(new BorderLayout());
JPanel buttonPanel = new JPanel(new FlowLayout(FlowLayout.RIGHT));
fontLabel = new JLabel("字体(F):");
styleLabel = new JLabel("字形(Y):");
sizeLabel = new JLabel("大小(S):");
previewLabel = new JLabel("预览:");
fontComboBox = new JComboBox(fontList);
styleComboBox = new JComboBox(styleList);
sizeComboBox = new JComboBox(sizeList);
previewTextArea = new JTextArea("AaBbCcYyZz", 6, 20);
okButton = new JButton("确定");
cancelButton = new JButton("取消");
fontComboBox.setSelectedItem(font.getName());
styleComboBox.setSelectedIndex(font.getStyle());
sizeComboBox.setSelectedItem(String.valueOf(font.getSize()));
fontPanel.add(fontLabel);
fontPanel.add(fontComboBox);
stylePanel.add(styleLabel);
stylePanel.add(styleComboBox);
sizePanel.add(sizeLabel);
sizePanel.add(sizeComboBox);
previewPanel.setBorder(BorderFactory.createCompoundBorder(BorderFactory.createLineBorder(Color.GRAY), BorderFactory.createEmptyBorder(5, 5, 5, 5)));
previewPanel.add(previewLabel, BorderLayout.NORTH);
previewPanel.add(new JScrollPane(previewTextArea), BorderLayout.CENTER);
buttonPanel.add(okButton);
buttonPanel.add(cancelButton);
mainPanel.setBorder(BorderFactory.createEmptyBorder(5, 5, 5, 5));
mainPanel.add(fontPanel, BorderLayout.NORTH);
mainPanel.add(stylePanel, BorderLayout.CENTER);
mainPanel.add(sizePanel, BorderLayout.SOUTH);
mainPanel.add(previewPanel, BorderLayout.WEST);
mainPanel.add(buttonPanel, BorderLayout.SOUTH);
this.setContentPane(mainPanel);
this.pack();
this.setLocationRelativeTo(null);
okButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
font = new Font((String) fontComboBox.getSelectedItem(), styleComboBox.getSelectedIndex(), Integer.parseInt((String) sizeComboBox.getSelectedItem()));
FontDialog.this.dispose();
}
});
cancelButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
FontDialog.this.dispose();
}
});
previewTextArea.setFont(font);
fontComboBox.addItemListener(new ItemListener() {
public void itemStateChanged(ItemEvent e) {
previewTextArea.setFont(font);
previewTextArea.repaint();
}
});
styleComboBox.addItemListener(new ItemListener() {
public void itemStateChanged(ItemEvent e) {
previewTextArea.setFont(font);
previewTextArea.repaint();
}
});
sizeComboBox.addItemListener(new ItemListener() {
public void itemStateChanged(ItemEvent e) {
previewTextArea.setFont(font);
previewTextArea.repaint();
}
});
}
public Font getFont() {
return font;
}
}
```
通过运行以下代码进行测试:
```java
public class TestFontDialog {
public static void main(String[] args) {
JFrame frame = new JFrame("Test FontDialog");
JTextField textField = new JTextField("测试字体对话框");
JButton button = new JButton("字体");
Font font = textField.getFont();
button.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
FontDialog dialog = new FontDialog(frame, font);
dialog.setVisible(true);
textField.setFont(dialog.getFont());
}
});
JPanel panel = new JPanel();
panel.add(textField);
panel.add(button);
frame.setContentPane(panel);
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
}
```
以上示例会在一个JFrame中显示一个JTextField和一个“字体”按钮,点击按钮会弹出一个“字体”对话框,可以选择字体、字形和大小,预览也会更新。
希望这个示例代码能够帮到你,如果有任何疑问,请随时问我。
阅读全文