请问如何从系统中获取字体选择窗口 最近在做一个GUI,程序里需要实现这么一个功能:单击字体选择按钮,会弹出一个字体选择窗口,就像WINDOWS里面的记事本一样,虽然可以自己一个个写,但我希望能从系统中获取这么一个选择窗口,不知道该如何实现。希望路过的各位高手能出手指点一二。要求:不要只留下代码就拍拍屁股走人,嘿嘿~讲一下思路,最好连一些使用到的主要类的功能也解释一下!小弟初来乍到,也没什么积分,希望路过的高手不要嫌弃!小弟不胜感激!!! 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 /** * */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()); return new Font(f, s, sz); } // 确定按钮功能的实现 private void yesButton() { notepad.getTextArea().setFont(this.createFont()); this.setVisible(false); this.dispose(); } // 取消按钮功能的实现 private void cancelButton() { this.setVisible(false); this.dispose(); } // 根据字形名获取字形代码 private int getStyle(String styleName) { int syl = 0; if (styleName.equals("常规")) { syl = 0; } else if (styleName.equals("粗体")) { syl = 1; } else if (styleName.equals("斜体")) { syl = 2; } else if (styleName.equals("粗斜体")) { syl = 3; } return syl; } // 根据字形代码获取字形名 private String getStyle(int syl) { String s = ""; if (syl == 0) { s = "常规"; } else if (syl == 1) { s = "粗体"; } else if (syl == 2) { s = "斜体"; } else if (syl == 3) { s = "粗斜体"; } return s; } // 初始化组件 private void initGroupWare() { initLabel(); initButton(); initTextField(); initList(); initScroll(); } // 初始化list组件 private void initList() { for (int i = 0; i < list.length; i++) { list[i] = new JList(); final JList l = list[i]; final JTextField t = text[i]; l.addListSelectionListener(new ListSelectionListener() { public void valueChanged(ListSelectionEvent e) { t.setText(l.getSelectedValue().toString()); setExampleTextField(); } }); } list[0].setListData(fontName); list[1].setListData(style); list[2].setListData(size); } private void setExampleTextField() { text[3].setFont(this.createFont()); } // 初始化scroll组件 private void initScroll() { for (int i = 0; i < scroll.length; i++) { scroll[i] = new JScrollPane(list[i]); scroll[i].setFont(font); } } // 初始化text组件 private void initTextField() { for (int i = 0; i < text.length; i++) { text[i] = new JTextField(); text[i].setFont(font); } Font f = notepad.getTextArea().getFont(); text[0].setText(f.getFamily()); text[1].setText(getStyle(f.getStyle())); text[2].setText(Integer.toString(f.getSize())); text[3].setText("Notepad SDK 心→落羽‰"); text[3].setHorizontalAlignment(0); text[3].setFont(notepad.getTextArea().getFont()); } // 初始化button组件 private void initButton() { for (int i = 0; i < 2; i++) { button[i] = new JButton(comName[i + 3]); button[i].setFont(font); } button[0].addMouseListener(new MouseAdapter() { public void mousePressed(MouseEvent e) { yesButton(); } }); button[1].addMouseListener(new MouseAdapter() { public void mousePressed(MouseEvent e) { cancelButton(); } }); } // 初始化label组件 private void initLabel() { for (int i = 0; i < 3; i++) { label[i] = new JLabel(comName[i]); label[i].setFont(font); } }}这是我自己写的字体选择框,自己写很麻烦,如果能调用系统的就好了。 /** * */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());return new Font(f, s, sz); } // 确定按钮功能的实现 private void yesButton() {notepad.getTextArea().setFont(this.createFont());this.setVisible(false);this.dispose(); } // 取消按钮功能的实现 private void cancelButton() {this.setVisible(false);this.dispose(); } // 根据字形名获取字形代码 private int getStyle(String styleName) {int syl = 0;if (styleName.equals( "常规 ")) { syl = 0;} else if (styleName.equals( "粗体 ")) { syl = 1;} else if (styleName.equals( "斜体 ")) { syl = 2;} else if (styleName.equals( "粗斜体 ")) { syl = 3;}return syl; } // 根据字形代码获取字形名 private String getStyle(int syl) {String s = " ";if (syl == 0) { s = "常规 ";} else if (syl == 1) { s = "粗体 ";} else if (syl == 2) { s = "斜体 ";} else if (syl == 3) { s = "粗斜体 ";}return s; } // 初始化组件 private void initGroupWare() {initLabel();initButton();initTextField();initList();initScroll(); } // 初始化list组件 private void initList() {for (int i = 0; i < list.length; i++) {list[i] = new JList();final JList l = list[i];final JTextField t = text[i];l.addListSelectionListener(new ListSelectionListener() {public void valueChanged(ListSelectionEvent e) {t.setText(l.getSelectedValue().toString());setExampleTextField();}});}list[0].setListData(fontName);list[1].setListData(style);list[2].setListData(size); } private void setExampleTextField() {text[3].setFont(this.createFont()); } // 初始化scroll组件 private void initScroll() {for (int i = 0; i < scroll.length; i++) {scroll[i] = new JScrollPane(list[i]);scroll[i].setFont(font);} } // 初始化text组件 private void initTextField() {for (int i = 0; i < text.length; i++) {text[i] = new JTextField();text[i].setFont(font);}Font f = notepad.getTextArea().getFont();text[0].setText(f.getFamily());text[1].setText(getStyle(f.getStyle()));text[2].setText(Integer.toString(f.getSize()));text[3].setText( "Notepad SDK 心→落羽‰ ");text[3].setHorizontalAlignment(0);text[3].setFont(notepad.getTextArea().getFont()); } // 初始化button组件 private void initButton() {for (int i = 0; i < 2; i++) {button[i] = new JButton(comName[i + 3]);button[i].setFont(font);}button[0].addMouseListener(new MouseAdapter() {public void mousePressed(MouseEvent e) {yesButton();}});button[1].addMouseListener(new MouseAdapter() {public void mousePressed(MouseEvent e) {cancelButton();}}); } // 初始化label组件 private void initLabel() {for (int i = 0; i < 3; i++) {label[i] = new JLabel(comName[i]);label[i].setFont(font);} }} 最简单的进销存数据库怎么设计 点击后获得鼠标当前坐标并输出在鼠标箭头上方? 关于容器添加组件的层次问题(在线等) 请问怎样通过jdbc向oracle中存储date类型数据 内部类的运用问题!!! 关于Swing的问题? 读取文件中的中文问题!! Thinking in java第1版里1段程序的问题 关于JTree选中问题,在线等 大家谁有完整的win2000下配置tomcat3.2.4作web服务器的文档,谢谢! JAVA如何访问MYSQL的存储过程 命令行执行jar执行文件时,其中使用的log4j出错,但在ECLIPSE 中可以运行
*
*/
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());
return new Font(f, s, sz);
} // 确定按钮功能的实现
private void yesButton() {
notepad.getTextArea().setFont(this.createFont());
this.setVisible(false);
this.dispose();
} // 取消按钮功能的实现
private void cancelButton() {
this.setVisible(false);
this.dispose();
} // 根据字形名获取字形代码
private int getStyle(String styleName) {
int syl = 0;
if (styleName.equals("常规")) {
syl = 0;
} else if (styleName.equals("粗体")) {
syl = 1;
} else if (styleName.equals("斜体")) {
syl = 2;
} else if (styleName.equals("粗斜体")) {
syl = 3;
}
return syl;
} // 根据字形代码获取字形名
private String getStyle(int syl) {
String s = "";
if (syl == 0) {
s = "常规";
} else if (syl == 1) {
s = "粗体";
} else if (syl == 2) {
s = "斜体";
} else if (syl == 3) {
s = "粗斜体";
}
return s;
} // 初始化组件
private void initGroupWare() {
initLabel();
initButton();
initTextField();
initList();
initScroll();
} // 初始化list组件
private void initList() {
for (int i = 0; i < list.length; i++) {
list[i] = new JList();
final JList l = list[i];
final JTextField t = text[i];
l.addListSelectionListener(new ListSelectionListener() { public void valueChanged(ListSelectionEvent e) {
t.setText(l.getSelectedValue().toString());
setExampleTextField();
} });
}
list[0].setListData(fontName);
list[1].setListData(style);
list[2].setListData(size);
} private void setExampleTextField() {
text[3].setFont(this.createFont());
} // 初始化scroll组件
private void initScroll() {
for (int i = 0; i < scroll.length; i++) {
scroll[i] = new JScrollPane(list[i]);
scroll[i].setFont(font);
}
} // 初始化text组件
private void initTextField() {
for (int i = 0; i < text.length; i++) {
text[i] = new JTextField();
text[i].setFont(font);
}
Font f = notepad.getTextArea().getFont();
text[0].setText(f.getFamily());
text[1].setText(getStyle(f.getStyle()));
text[2].setText(Integer.toString(f.getSize()));
text[3].setText("Notepad SDK 心→落羽‰");
text[3].setHorizontalAlignment(0);
text[3].setFont(notepad.getTextArea().getFont());
} // 初始化button组件
private void initButton() {
for (int i = 0; i < 2; i++) {
button[i] = new JButton(comName[i + 3]);
button[i].setFont(font);
}
button[0].addMouseListener(new MouseAdapter() {
public void mousePressed(MouseEvent e) {
yesButton();
}
});
button[1].addMouseListener(new MouseAdapter() { public void mousePressed(MouseEvent e) {
cancelButton();
}
});
} // 初始化label组件
private void initLabel() {
for (int i = 0; i < 3; i++) {
label[i] = new JLabel(comName[i]);
label[i].setFont(font);
}
}
}
这是我自己写的字体选择框,自己写很麻烦,如果能调用系统的就好了。
*
*/
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());
return new Font(f, s, sz);
} // 确定按钮功能的实现
private void yesButton() {
notepad.getTextArea().setFont(this.createFont());
this.setVisible(false);
this.dispose();
} // 取消按钮功能的实现
private void cancelButton() {
this.setVisible(false);
this.dispose();
} // 根据字形名获取字形代码
private int getStyle(String styleName) {
int syl = 0;
if (styleName.equals( "常规 ")) {
syl = 0;
} else if (styleName.equals( "粗体 ")) {
syl = 1;
} else if (styleName.equals( "斜体 ")) {
syl = 2;
} else if (styleName.equals( "粗斜体 ")) {
syl = 3;
}
return syl;
} // 根据字形代码获取字形名
private String getStyle(int syl) {
String s = " ";
if (syl == 0) {
s = "常规 ";
} else if (syl == 1) {
s = "粗体 ";
} else if (syl == 2) {
s = "斜体 ";
} else if (syl == 3) {
s = "粗斜体 ";
}
return s;
} // 初始化组件
private void initGroupWare() {
initLabel();
initButton();
initTextField();
initList();
initScroll();
} // 初始化list组件
private void initList() {
for (int i = 0; i < list.length; i++) {
list[i] = new JList();
final JList l = list[i];
final JTextField t = text[i];
l.addListSelectionListener(new ListSelectionListener() {public void valueChanged(ListSelectionEvent e) {
t.setText(l.getSelectedValue().toString());
setExampleTextField();
}});
}
list[0].setListData(fontName);
list[1].setListData(style);
list[2].setListData(size);
} private void setExampleTextField() {
text[3].setFont(this.createFont());
} // 初始化scroll组件
private void initScroll() {
for (int i = 0; i < scroll.length; i++) {
scroll[i] = new JScrollPane(list[i]);
scroll[i].setFont(font);
}
} // 初始化text组件
private void initTextField() {
for (int i = 0; i < text.length; i++) {
text[i] = new JTextField();
text[i].setFont(font);
}
Font f = notepad.getTextArea().getFont();
text[0].setText(f.getFamily());
text[1].setText(getStyle(f.getStyle()));
text[2].setText(Integer.toString(f.getSize()));
text[3].setText( "Notepad SDK 心→落羽‰ ");
text[3].setHorizontalAlignment(0);
text[3].setFont(notepad.getTextArea().getFont());
} // 初始化button组件
private void initButton() {
for (int i = 0; i < 2; i++) {
button[i] = new JButton(comName[i + 3]);
button[i].setFont(font);
}
button[0].addMouseListener(new MouseAdapter() {
public void mousePressed(MouseEvent e) {
yesButton();
}
});
button[1].addMouseListener(new MouseAdapter() {public void mousePressed(MouseEvent e) {
cancelButton();
}
});
} // 初始化label组件
private void initLabel() {
for (int i = 0; i < 3; i++) {
label[i] = new JLabel(comName[i]);
label[i].setFont(font);
}
}
}