使用JDialog,参照Windows记事本编写一个“字体”对话框,并且能够运行
时间: 2023-06-07 15:12:08 浏览: 146
好的,让我来回答你的问题。首先,我明白你想要使用JDialog参照Windows记事本编写一个“字体”对话框。实现这个功能,你可以按照以下步骤进行:
1. 创建一个JDialog的子类,并设置dialog的标题和大小。
2. 在dialog中使用JComboBox、JList和JLabel等Swing组件来构建“字体”、“字形”和“字号”三个选项。
3. 使用Java提供的Font类和GraphicsEnvironment类来获取系统中所有可用的字体,并将其添加到JComboBox中。
4. 实现一个ActionListener接口的内部类,用于监听用户选择“确认”或“取消”按钮的点击事件,并且在用户点击确认按钮后设置所选的字体、字形和字号。
5. 在主窗口中实现一个ActionListener接口的内部类,用于在用户点击字体设置的按钮时创建并显示JDialog对话框。
下面是一个简单的实现示例,希望能对你有所帮助。
```java
import java.awt.Font;
import java.awt.GraphicsEnvironment;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JDialog;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JList;
import javax.swing.JPanel;
import javax.swing.ListSelectionModel;
public class FontDialog extends JDialog {
private JComboBox<String> fontComboBox;
private JList<String> styleList;
private JComboBox<Integer> sizeComboBox;
private JLabel fontLabel;
private Font selectedFont;
public FontDialog(JFrame parent) {
super(parent, "字体", true);
setSize(300, 200);
JPanel panel = new JPanel();
panel.setLayout(null);
JLabel fontNameLabel = new JLabel("字体:");
fontNameLabel.setBounds(10, 10, 80, 20);
panel.add(fontNameLabel);
JLabel fontStyleLabel = new JLabel("字形:");
fontStyleLabel.setBounds(10, 40, 80, 20);
panel.add(fontStyleLabel);
JLabel fontSizeLabel = new JLabel("字号:");
fontSizeLabel.setBounds(10, 70, 80, 20);
panel.add(fontSizeLabel);
GraphicsEnvironment ge = GraphicsEnvironment.getLocalGraphicsEnvironment();
String[] fontNames = ge.getAvailableFontFamilyNames();
fontComboBox = new JComboBox<String>(fontNames);
fontComboBox.setBounds(100, 10, 180, 20);
panel.add(fontComboBox);
String[] styles = {"常规", "粗体", "斜体", "粗斜体"};
styleList = new JList<String>(styles);
styleList.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
styleList.setSelectedIndex(0);
styleList.setVisibleRowCount(4);
styleList.setBounds(100, 40, 80, 70);
panel.add(styleList);
Integer[] sizes = {8, 9, 10, 11, 12, 14, 16, 18, 20, 22, 24, 26, 28, 36, 48, 72};
sizeComboBox = new JComboBox<Integer>(sizes);
sizeComboBox.setSelectedIndex(4);
sizeComboBox.setBounds(100, 70, 80, 20);
panel.add(sizeComboBox);
JButton okButton = new JButton("确认");
okButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
int style = Font.PLAIN;
switch (styleList.getSelectedIndex()) {
case 1:
style = Font.BOLD;
break;
case 2:
style = Font.ITALIC;
break;
case 3:
style = Font.BOLD | Font.ITALIC;
break;
}
selectedFont = new Font(fontComboBox.getSelectedItem().toString(), style,
(int) sizeComboBox.getSelectedItem());
dispose();
}
});
okButton.setBounds(200, 40, 80, 20);
panel.add(okButton);
JButton cancelButton = new JButton("取消");
cancelButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
selectedFont = null;
dispose();
}
});
cancelButton.setBounds(200, 70, 80, 20);
panel.add(cancelButton);
fontLabel = new JLabel("Sample Text");
fontLabel.setBounds(10, 110, 260, 30);
panel.add(fontLabel);
add(panel);
setLocationRelativeTo(parent);
}
public Font showDialog() {
setVisible(true);
return selectedFont;
}
public static void main(String[] args) {
JFrame frame = new JFrame("主窗口");
frame.setSize(400, 400);
JButton fontButton = new JButton("字体");
fontButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
FontDialog dialog = new FontDialog(frame);
Font font = dialog.showDialog();
if (font != null) {
frame.getContentPane().setFont(font);
}
}
});
fontButton.setBounds(10, 10, 80, 20);
frame.add(fontButton);
frame.setVisible(true);
}
}
```
希望这个例子能对你有所启发,如果还有其他问题,欢迎继续提问。
阅读全文