日期:2014-05-20  浏览次数:20709 次

请问如何从系统中获取字体选择窗口
最近在做一个GUI,程序里需要实现这么一个功能:单击字体选择按钮,会弹出一个字体选择窗口,就像WINDOWS里面的记事本一样,虽然可以自己一个个写,但我希望能从系统中获取这么一个选择窗口,不知道该如何实现。

希望路过的各位高手能出手指点一二。要求:不要只留下代码就拍拍屁股走人,嘿嘿~讲一下思路,最好连一些使用到的主要类的功能也解释一下!

小弟初来乍到,也没什么积分,希望路过的高手不要嫌弃!

小弟不胜感激!!!

------解决方案--------------------
/**
*
*/
package toolpackage;

import java.awt.Font;
import java.awt.GraphicsEnvironment;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;

import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JLabel;
import javax.swing.JList;
import javax.swing.JScrollPane;
import javax.swing.JTextField;
import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener;

import main.Notepad;

/**
* @author Snow
*
*/
public class FontChooser extends JDialog {

/**
*
*/
private static final long serialVersionUID = -1616394874632863203L;

private Notepad notepad;

private String[] fontName = GraphicsEnvironment
.getLocalGraphicsEnvironment().getAvailableFontFamilyNames();

private String[] comName = { "字体 ", "字形 ", "大小 ", "确定 ", "取消 " };

private String[] style = { "常规 ", "粗体 ", "斜体 ", "粗斜体 " };

private String[] size = { "6 ", "7 ", "8 ", "9 ", "10 ", "11 ", "12 ", "14 ", "16 ",
"18 ", "20 ", "22 ", "24 ", "26 ", "28 ", "36 ", "48 ", "72 " };

private Font font = new Font(null, 0, 12);

private JLabel[] label = new JLabel[3];

private JButton[] button = new JButton[2];

private JTextField[] text = new JTextField[4];

private JScrollPane[] scroll = new JScrollPane[3];

private JList[] list = new JList[3];

public FontChooser(Notepad notepad) {
this.notepad = notepad;
this.setTitle( "字体 ");
this.setLayout(null);
initGroupWare();
setSizeAndLocation();
addGroupWare();
this.setResizable(false); // 设置为不能由用户调整大小
}

// 设置组件的大小和位置
private void setSizeAndLocation() {
label[0].setBounds(10, 0, 120, 20);
text[0].setBounds(10, 20, 120, 20);
scroll[0].setBounds(10, 42, 120, 150);
button[0].setBounds(310, 20, 70, 20);
label[1].setBounds(140, 0, 100, 20);
text[1].setBounds(140, 20, 100, 20);
scroll[1].setBounds(140, 42, 100, 150);
button[1].setBounds(310, 50, 70, 20);
label[2].setBounds(250, 0, 50, 20);
text[2].setBounds(250, 20, 50, 20);
scroll[2].setBounds(250, 42, 50, 150);
text[3].setBounds(10, 200, 370, 60);
}

// 添加各组件
private void addGroupWare() {
for (int i = 0; i < label.length; i++) {
this.getContentPane().add(label[i]);
this.getContentPane().add(text[i]);
this.getContentPane().add(scroll[i]);
}
this.getContentPane().add(button[0]);
this.getContentPane().add(button[1]);
this.getContentPane().add(text[3]);
}

// 根据选择创建字体
private Font createFont() {
String f = text[0].getText();
int s = getStyle(text[1].getText());
int sz = Integer.parseInt(text[2].getText());