使用JDialog,参照Windows记事本编写一个“字体”对话框
时间: 2024-05-11 19:15:54 浏览: 80
好的,我可以通过以下代码实现:
```
import javax.swing.*;
import javax.swing.event.*;
import java.awt.*;
import java.awt.event.*;
public class FontDialog extends JDialog implements ActionListener, ListSelectionListener {
private static final long serialVersionUID = 1L;
private JTextField tfFont;
private JTextField tfSize;
private JList<String> listFontNames;
private JList<Integer> listSizes;
private JButton btnOK;
private JButton btnCancel;
private Font selectedFont;
public FontDialog(JFrame parentFrame) {
super(parentFrame, "Font", true);
setResizable(false);
setSize(400, 300);
setLocationRelativeTo(parentFrame);
JPanel panelMain = new JPanel();
panelMain.setLayout(new BorderLayout(10, 10));
JPanel panelFont = new JPanel(new GridLayout(2, 2, 10, 10));
JPanel panelSize = new JPanel();
tfFont = new JTextField("Arial", 20);
tfSize = new JTextField("12", 5);
GraphicsEnvironment ge = GraphicsEnvironment.getLocalGraphicsEnvironment();
String[] fontNames = ge.getAvailableFontFamilyNames();
listFontNames = new JList<String>(fontNames);
listFontNames.setSelectedValue("Arial", true);
listFontNames.addListSelectionListener(this);
Integer[] sizes = { 8, 9, 10, 11, 12, 14, 16, 18, 20, 22, 24, 26, 28, 36, 48, 72 };
listSizes = new JList<Integer>(sizes);
listSizes.setSelectedValue(12, true);
listSizes.addListSelectionListener(this);
JScrollPane scrollFontNames = new JScrollPane(listFontNames);
JScrollPane scrollSizes = new JScrollPane(listSizes);
scrollFontNames.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);
scrollSizes.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);
panelFont.add(new JLabel("Font: "));
panelFont.add(tfFont);
panelFont.add(new JLabel("Size: "));
panelFont.add(tfSize);
panelSize.setLayout(new BorderLayout());
panelSize.add(new JLabel("Size: "), BorderLayout.NORTH);
panelSize.add(scrollSizes, BorderLayout.CENTER);
panelMain.add(panelFont, BorderLayout.NORTH);
panelMain.add(scrollFontNames, BorderLayout.CENTER);
panelMain.add(panelSize, BorderLayout.EAST);
JPanel panelButtons = new JPanel();
btnOK = new JButton("OK");
btnCancel = new JButton("Cancel");
btnOK.addActionListener(this);
btnCancel.addActionListener(this);
panelButtons.add(btnOK);
panelButtons.add(btnCancel);
panelMain.add(panelButtons, BorderLayout.SOUTH);
setContentPane(panelMain);
}
public Font showDialog() {
setVisible(true);
return selectedFont;
}
public void actionPerformed(ActionEvent e) {
Object source = e.getSource();
if (source == btnOK) {
String fontName = tfFont.getText();
int size = Integer.parseInt(tfSize.getText());
selectedFont = new Font(fontName, Font.PLAIN, size);
dispose();
} else if (source == btnCancel) {
selectedFont = null;
dispose();
}
}
public void valueChanged(ListSelectionEvent e) {
Object source = e.getSource();
if (!e.getValueIsAdjusting()) {
if (source == listFontNames) {
String fontName = listFontNames.getSelectedValue();
tfFont.setText(fontName);
tfFont.setFont(new Font(fontName, Font.PLAIN, 12));
} else if (source == listSizes) {
Integer size = listSizes.getSelectedValue();
tfSize.setText(size.toString());
tfSize.setFont(new Font(tfSize.getFont().getFamily(), Font.PLAIN, size));
}
}
}
}
```
然后在主程序中调用该对话框即可:
```
import javax.swing.*;
public class Main {
public static void main(String[] args) {
JFrame frame = new JFrame("Sample");
frame.setSize(400, 300);
frame.setLocationRelativeTo(null);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
FontDialog fontDialog = new FontDialog(frame);
Font font = fontDialog.showDialog();
if (font != null) {
JOptionPane.showMessageDialog(frame, "Selected font: " + font.getName() + " " + font.getSize());
// Do something with selected font
}
}
}
```
这样就可以弹出一个“字体”对话框,用户可以选择想要的字体和字号。
阅读全文