各位大哥给个答案
解决方案 »
- 我的MyEclipse中为何不能识别javax.swing.GroupLayout!!!
- Mysql数据库中jdbc用PreparedStatement问题
- 难题:VC程序转JAVA
- 内部类的问题
- "instanceof"这个关键字是什么意思,有什么作用啊?
- SubReport的问题不简单阿!百思不得七届阿!!!!
- 在JAVA中如何使用数据结构
- ¥¥¥¥¥¥用JBUILDER7作过SWING程序的请进来指教一下,在线结帖,急
- 用JDK怎么才能生成EXE文件啊?
- JBUILDER的简单问题
- 网络何时失效(完美解决追加200分)---socket的另一方关闭或者异常断开程序该如何知道
- JBuilder 2006 里面快速导入包的快捷键
不过也不用自己写.
你到www.chinajavaworld.com里,那里的版主sunking弄了一个OpenSwing,有很多东西
或者到google上搜一下,有很多swing不带的界面包,下一个就可以了
JEditPane里面有SetFont方法的
LZ可以去看看API~
这是我很早之前写的一个控件。
---------------------------------
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowEvent;
import java.awt.event.WindowAdapter;
import javax.swing.*;
import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener;
import java.util.Locale;
import java.util.ResourceBundle;
import java.util.MissingResourceException;
public class JFontChooser{
protected static JDialog fontDialog = null;
protected static JLabel previewArea = null;
protected static Font resultFont = null;
protected static String fontName = null;
protected static String fontFace = null;
protected static int fontSize = 0;
protected static int fontStyle = 0;
protected static JList fNameChoice = null;
protected static JList fSizeChoice = null;
protected static JCheckBox bold = null;
protected static JCheckBox italic = null;
protected static ActionListener listener = new FontAction();
protected static ListSelectionListener listListener = new ListListener();
protected static String[] fontSizes =
{
"5" ,"6" , "7" , "8" , "9" ,
"10","11", "12", "14", "16",
"18","20", "24", "26", "30",
"34","40", "48", "60", "72"
};
protected static ResourceBundle fontResources = null;
static {
try {
fontResources = ResourceBundle.getBundle(
"FontResources", Locale.getDefault());
} catch (MissingResourceException mre) {
System.err.println("FontResources.properties not found");
System.exit(1);
}
}
public JFontChooser(JFrame frame){
showFontChooserDialog(frame);
}
protected static Font showFontChooserDialog(JFrame frame){
fontDialog = new JDialog(
frame, fontResources.getString("Title"), true);
fontDialog.addWindowListener(new WindowAdapter(){
public void windowClosing(WindowEvent e){
doNothing();
}
});
fontDialog.setSize(480, 350);
fontDialog.setResizable(false);
Dimension comSize = fontDialog.getSize();
Dimension scrSize = Toolkit.getDefaultToolkit().getScreenSize();
fontDialog.setLocation((scrSize.width - comSize.width)/2,
(scrSize.height- comSize.height)/2);
JPanel fontArea = createFontPanel();
previewArea = createPreviewLabel();
JPanel buttonsArea = createButtonsPanel();
JPanel bowl = new JPanel();
bowl.setLayout(new BorderLayout());
bowl.setBorder(BorderFactory.createEmptyBorder(5, 10, 5, 10));
bowl.add(BorderLayout.NORTH, fontArea);
bowl.add(BorderLayout.CENTER, previewArea);
bowl.add(BorderLayout.SOUTH, buttonsArea);
Container cont = fontDialog.getContentPane();
cont.add(bowl);
fontDialog.setVisible(true);
return resultFont;
}
protected static JPanel createFontPanel(){
JPanel fontPanel = new JPanel();
fontPanel.setLayout(new BoxLayout(fontPanel, BoxLayout.X_AXIS));
fNameChoice = createFacesList();
JScrollPane scrPaneName = new JScrollPane(fNameChoice);
scrPaneName.setBorder(BorderFactory.createTitledBorder(
fontResources.getString("FontTitle")));
fSizeChoice = createSizesList();
JScrollPane scrPaneSize = new JScrollPane(fSizeChoice);
scrPaneSize.setBorder(BorderFactory.createTitledBorder(
fontResources.getString("SizeTitle")));
JPanel attribsPanel = createAttribsPanel();
fontPanel.add(scrPaneName);
fontPanel.add(Box.createHorizontalStrut(15));
fontPanel.add(scrPaneSize);
fontPanel.add(Box.createHorizontalStrut(15));
fontPanel.add(attribsPanel);
return fontPanel;
}
protected static JList createFacesList(){
String[] fonts = GraphicsEnvironment.
getLocalGraphicsEnvironment().getAvailableFontFamilyNames();
ListCellRenderer fontRenderer = new FontListCellRenderer();
JList facesList = new JList(fonts);
facesList.setCellRenderer(fontRenderer);
facesList.setSelectedIndex(0);
facesList.addListSelectionListener(listListener);
facesList.setBorder(BorderFactory.createLoweredBevelBorder());
facesList.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
return facesList;
}
JList sizeList = new JList(fontSizes);
sizeList.setSelectedIndex(7);
sizeList.setPrototypeCellValue("MMMMMM");
sizeList.addListSelectionListener(listListener);
sizeList.setBorder(BorderFactory.createLoweredBevelBorder());
sizeList.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
return sizeList;
}
protected static JPanel createAttribsPanel(){
JPanel boldItalicPanel = new JPanel();
boldItalicPanel.setLayout(new GridLayout(0,1));
boldItalicPanel.setBorder(BorderFactory.createTitledBorder(
fontResources.getString("AttribsTitle")));
bold = new JCheckBox(
fontResources.getString("BoldLabel"), false);
italic = new JCheckBox(
fontResources.getString("ItalicLabel"), false);
bold.addActionListener(listener);
italic.addActionListener(listener);
boldItalicPanel.add(bold);
boldItalicPanel.add(italic);
return boldItalicPanel;
}
protected static JLabel createPreviewLabel(){
JLabel previewLabel = new JLabel(
fontResources.getString("PreviewText")
+ " " + fontFace, JLabel.CENTER);
previewLabel.setBorder(BorderFactory.createTitledBorder(
fontResources.getString("PreviewTitle")));
previewLabel.setSize(460, 70);
return previewLabel;
}
protected static JPanel createButtonsPanel(){
JPanel buttonsPanel = new JPanel();
JButton applyButton = new JButton(
fontResources.getString("ApplyLabel"));
applyButton.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
previewFont();
fontDialog.setVisible(false);
fontDialog.dispose();
}
});
JButton cancelButton = new JButton(
fontResources.getString("CancelLabel"));
cancelButton.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
doNothing();
}
});
buttonsPanel.add(applyButton);
buttonsPanel.add(Box.createHorizontalStrut(5));
buttonsPanel.add(cancelButton);
buttonsPanel.setBorder(
BorderFactory.createCompoundBorder(
BorderFactory.createEmptyBorder(2, 1, 0, 1),
BorderFactory.createTitledBorder("")
)
);
return buttonsPanel;
}
protected static class FontListCellRenderer extends JLabel
implements ListCellRenderer{
public FontListCellRenderer(){
setOpaque(true);
}
public Component getListCellRendererComponent(
JList list,
Object value,
int index,
boolean isSelected,
boolean cellHasFocus){
String fontN = value.toString();
setText(fontN);
setFont(new Font(fontN, 0, 13));
if (isSelected){
setForeground(list.getSelectionForeground());
setBackground(list.getSelectionBackground());
}else{
setForeground(list.getForeground());
setBackground(list.getBackground());
}
if(cellHasFocus){
setBorder(BorderFactory.createLineBorder(Color.LIGHT_GRAY, 1));
}else{
setBorder(BorderFactory.createEmptyBorder(2, 2, 2, 2));
}
return this;
}
}
protected static class FontAction implements ActionListener{
public void actionPerformed(ActionEvent event){
previewFont();
}
}
protected static class ListListener implements ListSelectionListener{
public void valueChanged(ListSelectionEvent e){
previewFont();
}
}
protected static void previewFont(){
fontFace = (String)fNameChoice.getSelectedValue();
fontName = (String)fSizeChoice.getSelectedValue();
fontSize = Integer.parseInt(fontName);
fontStyle = ( bold.isSelected() ? Font.BOLD : 0)
+ ( italic.isSelected()? Font.ITALIC : 0);
resultFont = new Font(fontFace, fontStyle, fontSize);
previewArea.setText(
fontResources.getString("PreviewText") + " " + fontFace);
previewArea.setFont(resultFont);
fontDialog.validate();
}
protected static void doNothing(){
resultFont = null;
fontFace = null;
fontSize = 0;
fontDialog.setVisible(false);
fontDialog.dispose();
}
public String getSelectedName(){
return fontFace;
}
public int getSelectedSize(){
return fontSize;
}
public int getSelectedStyle(){
return fontStyle;
}
public Font getSelectedFont(){
return resultFont;
}
}