使用JDialog,参照Windows记事本编写一个“字体”对话框
时间: 2023-06-07 07:12:09 浏览: 97
好的,我会尽可能详细地回答你的问题。首先,JDialog是Java中的一个类,用于创建一个对话框窗口,它可以包含用户输入的表单、文本框、按钮等组件。在这个字体对话框的例子中,我们会使用JDialog来打造一个带有各种字体、字号、字形等选项的自定义字体面板。下面是代码实现:
```java
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.border.*;
public class FontDialog extends JDialog implements ActionListener {
private static final long serialVersionUID = 1L;
private JList<String> fontList;
private JList<String> sizeList;
private JCheckBox boldCheck;
private JCheckBox italicCheck;
private JTextField previewText;
private JButton okButton;
private JButton cancelButton;
private String selectedFontName;
private int selectedFontSize;
private boolean isBold;
private boolean isItalic;
public FontDialog(JFrame parentFrame) {
super(parentFrame, "Select Font", true);
setSize(400, 400);
setResizable(false);
setLocationRelativeTo(parentFrame);
selectedFontName = "";
selectedFontSize = 0;
isBold = false;
isItalic = false;
JPanel mainPanel = new JPanel(new BorderLayout(10, 10));
mainPanel.setBorder(new EmptyBorder(10, 10, 10, 10));
JPanel fontPanel = new JPanel(new BorderLayout(10, 10));
fontPanel.setPreferredSize(new Dimension(200, 0));
fontList = new JList<>(GraphicsEnvironment.getLocalGraphicsEnvironment().getAvailableFontFamilyNames());
fontList.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
JScrollPane fontScroll = new JScrollPane(fontList);
fontPanel.add(fontScroll, BorderLayout.CENTER);
JPanel sizePanel = new JPanel(new BorderLayout(10, 10));
sizePanel.setPreferredSize(new Dimension(70, 0));
String[] sizes = {"8", "10", "12", "14", "16", "18", "20", "24", "28", "32", "36", "48", "72"};
sizeList = new JList<>(sizes);
sizeList.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
JScrollPane sizeScroll = new JScrollPane(sizeList);
sizePanel.add(sizeScroll, BorderLayout.CENTER);
JPanel stylePanel = new JPanel(new GridLayout(2, 1, 5, 5));
boldCheck = new JCheckBox("Bold");
italicCheck = new JCheckBox("Italic");
stylePanel.add(boldCheck);
stylePanel.add(italicCheck);
JPanel settingsPanel = new JPanel(new BorderLayout(10, 10));
settingsPanel.setBorder(new TitledBorder("Settings"));
settingsPanel.add(fontPanel, BorderLayout.CENTER);
settingsPanel.add(sizePanel, BorderLayout.EAST);
settingsPanel.add(stylePanel, BorderLayout.SOUTH);
previewText = new JTextField("The quick brown fox jumps over the lazy dog.");
previewText.setEditable(false);
previewText.setBorder(new CompoundBorder(BorderFactory.createLineBorder(Color.BLACK), new EmptyBorder(5, 5, 5, 5)));
previewText.setBackground(Color.WHITE);
JPanel buttonsPanel = new JPanel(new GridLayout(1, 2, 10, 10));
okButton = new JButton("OK");
okButton.addActionListener(this);
cancelButton = new JButton("Cancel");
cancelButton.addActionListener(this);
buttonsPanel.add(okButton);
buttonsPanel.add(cancelButton);
mainPanel.add(settingsPanel, BorderLayout.CENTER);
mainPanel.add(previewText, BorderLayout.SOUTH);
mainPanel.add(buttonsPanel, BorderLayout.SOUTH);
setContentPane(mainPanel);
setVisible(true);
}
public void actionPerformed(ActionEvent e) {
if (e.getSource() == cancelButton) {
dispose();
} else if (e.getSource() == okButton) {
selectedFontName = fontList.getSelectedValue();
selectedFontSize = Integer.parseInt(sizeList.getSelectedValue());
isBold = boldCheck.isSelected();
isItalic = italicCheck.isSelected();
dispose();
}
}
public String getSelectedFontName() {
return selectedFontName;
}
public int getSelectedFontSize() {
return selectedFontSize;
}
public boolean isBold() {
return isBold;
}
public boolean isItalic() {
return isItalic;
}
}
```
这个类继承了JDialog类,并实现了ActionListener接口,用于处理窗口中的按钮点击事件。在构造函数中,我们先设置一些初始值,然后创建了一个JPanel作为主面板,并设置了其布局和边框。接着创建了三个子面板分别放置字体、字号、字形等选项,并通过JScrollPane将它们包装成可滚动的列表。然后我们创建了一个JTextField作为字体预览区域,并设置了它的边框和背景色。
最后,我们创建了两个按钮,分别为OK和Cancel,并通过GridLayout布局将它们放置在一个JPanel中。我们将OK按钮的ActionListener设置为当前类,用于处理用户点击OK按钮的事件。当用户点击OK按钮时,我们通过getter函数获取用户所选择的字体、字号、字形等选项,并保存到成员变量中。然后调用dispose()方法将窗口关闭。当用户点击Cancel按钮时,我们只需要将窗口关闭即可,因此在ActionListener中我们只需要判断事件来源即可。
你可以在你的应用程序中实例化FontDialog类,并在需要的时候弹出字体对话框。比如,你可以在菜单中添加一个“选择字体”选项,点击后弹出字体对话框,并将用户选择的字体信息保存下来。另外,你还可以将用户选择的字体信息应用到你的其他组件上,比如文本框、标签等。
阅读全文