Font sansbold14 = new Font("宋体",Font.BOLD,20); JButton button1 = new JButton("Test"); button1.setFont(sansbold14);
Font sansbold14 = new Font("宋体",Font.BOLD,20); JButton button1 = new JButton("Test"); button1.setFont(sansbold14); 把上面的字体该一下试试!
我试过了!还是不行,难道在WIN2000系统上就没有好的方案了吗?
import javax.swing.*; import java.awt.*; public class JB{ public static void main(String args[]){ JFrame jf=new JFrame("wan"); Font sansbold14 = new Font("宋体",Font.BOLD,20); JButton button1 = new JButton("Test"); button1.setFont(sansbold14); jf.getContentPane().add(button1,BorderLayout.CENTER); jf.pack(); jf.setVisible(true); } }我要分
给你一个例子,用这一句: GraphicsEnvironment ge = GraphicsEnvironment.getLocalGraphicsEnvironment(); fonts = ge.getAllFonts();获得系统中所有字体,如果还没有你满意的字体那就没办法了 import javax.swing.*; import javax.swing.event.*; import java.awt.*; import java.awt.event.*;// Window frame class for showing selected font sample class FontSample extends JFrame { Font font; // Currently shown font String text; // Sample text to display // Constructor public FontSample() { super(); font = null; text = "Abcdefg 1234567890 !@#$%^&*()"; setDefaultCloseOperation(JFrame.HIDE_ON_CLOSE); setSize(425, 120); } // Called before showing window public void changeFont(Font f) { font = f.deriveFont(24.0f); // Resize to 24 pts setTitle(font.getFontName()); // Title = font name if (isShowing()) repaint(); // Repaint if already visible } // Paint sample text using current font in window public void paint(Graphics g) { Rectangle r = getBounds(null); g.setColor(Color.white); // Erase background to white g.fillRect(0, 0, r.width, r.height); if (font != null) { g.setFont(font); g.setColor(Color.black); g.drawString(text, 10, r.height / 2); } } }// Main program class public class FontDemo extends JFrame { final protected Font[] fonts; // Array of fonts final protected FontSample fontSample; // Sample window// Constructor public FontDemo() { super(); // Select local system look and feel try { UIManager.setLookAndFeel( UIManager.getSystemLookAndFeelClassName()); } catch (Exception e) { } // End program when window closes addWindowListener(new WindowAdapter() { public void windowClosing(WindowEvent e) { System.exit(0); } }); // Create child sample font window fontSample = new FontSample(); // Loading fonts may take a while; tell user System.out.print("Loading font names..."); // Get available fonts in 1pt sizes GraphicsEnvironment ge = GraphicsEnvironment.getLocalGraphicsEnvironment(); fonts = ge.getAllFonts(); // Create a JComboBox object for listing font names JComboBox fontBox = new JComboBox(); for (int i = 0; i < fonts.length; i++) fontBox.addItem(fonts[i].getFontName()); fontBox.setEditable(false); // Respond to item selection fontBox.addActionListener( new ActionListener() { public void actionPerformed(ActionEvent e) { JComboBox box = (JComboBox)e.getSource(); int fontIndex = box.getSelectedIndex(); fontSample.changeFont(fonts[fontIndex]); fontSample.show(); } } );
System.out.println("\nSelect font for a sample"); Container content = getContentPane(); content.setLayout(new FlowLayout()); content.add(new JLabel("Available fonts")); content.add(fontBox); } public static void main(String[] args) { FontDemo app = new FontDemo(); app.setTitle("Font Demonstration"); app.setSize(320, 240); app.show(); } }
楼主,我做过一个小小的FontChooser,也许对你有帮助,不过真的事没发现过好看的字体哦。 需要在JBuilder下运行,因为用到了XYLayout /** * <p>Title: JavaMe</p> * <p>Description: </p> * <p>Copyright: Copyright (c) 2003</p> * <p>Company: ISMT, HKUST</p> * @author group4 * @version 1.0 */ import java.awt.*; import javax.swing.*; import javax.swing.border.*; import java.awt.event.*; import javax.swing.event.*; import javax.swing.text.*;import com.borland.jbcl.layout.*; import MainApplication.*; public class FontChooser implements ActionListener, ListSelectionListener { private JDialog dialog; private JPanel panel = new JPanel(); private XYLayout xYLayout = new XYLayout(); private JTextField nameField = new JTextField(); private JTextField styleField = new JTextField(); private JTextField sizeField = new JTextField(); private JList nameList = new JList(); private JPanel namePanel = new JPanel(); private Border border; private TitledBorder titledBorder1, titledBorder2, titledBorder3; private JPanel stylePanel = new JPanel(); private JPanel sizePanel = new JPanel(); private JList styleList = new JList(); private JList sizeList = new JList(); private JButton okButton = new JButton(); private JButton cancelButton = new JButton(); //font attributes private Font [] fonts; private String [] fontNames; private String [] fontStyles = {"Plain", "Bold", "Italic", "Bold & Italic"}; private String [] fontSizes = {"8", "9", "10", "11", "12", "13", "14", "16", "18", "20", "22", "24", "28", "32", "36", "48", "60", "72"}; private String name, styleString, sizeString; private int style = Font.PLAIN, size = 12; private Font defaultFont = new Font("Times New Roman", Font.PLAIN, 12); //approve status public static final int OK_OPTION = 1; public static final int CANCEL_OPTION = 2; public static final int ERROR_OPTION = 3; private int option; private Frame frame; private String title; private boolean modal; //constructor with arguments public FontChooser(Frame frame, String title, boolean modal) { this.frame = frame; this.title = title; this.modal = modal; } //constructor with arguments public FontChooser(Frame frame, String title, boolean modal, Font defaultFont) { this.defaultFont = defaultFont; this.frame = frame; this.title = title; this.modal = modal; } //constructor without arguments public FontChooser() { this(null, "Font Chooser", true); } //initialize method private void initDialog() { panel.setLayout(xYLayout); //buttons okButton.setText("OK"); cancelButton.setText("Cancel"); okButton.addActionListener(this); cancelButton.addActionListener(this); xYLayout.setHeight(245); xYLayout.setWidth(340); //customize titledBorders titledBorder1 = new TitledBorder(BorderFactory.createMatteBorder(1,1,1,1,Color.black),"Font Name:"); titledBorder2 = new TitledBorder(BorderFactory.createMatteBorder(1,1,1,1,Color.black),"Font Style:"); titledBorder3 = new TitledBorder(BorderFactory.createMatteBorder(1,1,1,1,Color.black),"Font Size:"); //customize the three panels namePanel.setBorder(titledBorder1); namePanel.setLayout(new BorderLayout()); stylePanel.setBorder(titledBorder2); stylePanel.setLayout(new BorderLayout()); sizePanel.setBorder(titledBorder3); sizePanel.setLayout(new BorderLayout()); //text fields nameField.setEditable(false); styleField.setEditable(false); sizeField.setEditable(false); stylePanel.add(styleField, BorderLayout.NORTH); stylePanel.add(new JScrollPane(styleList), BorderLayout.CENTER); panel.add(okButton, new XYConstraints(176, 195, 71, -1)); panel.add(sizePanel, new XYConstraints(263, 16, 66, 163)); namePanel.add(nameField, BorderLayout.NORTH); namePanel.add(new JScrollPane(nameList), BorderLayout.CENTER); sizePanel.add(sizeField, BorderLayout.NORTH); sizePanel.add(new JScrollPane(sizeList), BorderLayout.CENTER); panel.add(namePanel, new XYConstraints(15, 17, 148, 163)); panel.add(cancelButton, new XYConstraints(253, 195, -1, -1)); panel.add(stylePanel, new XYConstraints(169, 17, 90, 162)); border = new MatteBorder(null);
namePanel.setVisible(true); stylePanel.setVisible(true); sizePanel.setVisible(true); dialog.getContentPane().add(panel, BorderLayout.CENTER); dialog.setSize(342, 250); dialog.setResizable(false); //Center the window Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize(); Dimension frameSize = dialog.getSize(); if (frameSize.height > screenSize.height) { frameSize.height = screenSize.height; } if (frameSize.width > screenSize.width) { frameSize.width = screenSize.width; } dialog.setLocation((screenSize.width - frameSize.width) / 2, (screenSize.height - frameSize.height) / 2); } //initialize method private void initializeList() { //get the fonts of the local platforms GraphicsEnvironment ge = GraphicsEnvironment.getLocalGraphicsEnvironment(); fonts = ge.getAllFonts(); fontNames = new String[fonts.length]; for(int i=0; i<fonts.length; i++) { fontNames[i] = fonts[i].getName(); } //create the list using the above attributes nameList = new JList(fontNames); styleList = new JList(fontStyles); sizeList = new JList(fontSizes); //set the default value //name list nameList.setSelectedValue(defaultFont.getFontName(), true); nameList.ensureIndexIsVisible(nameList.getSelectedIndex()); nameField.setText(defaultFont.getFontName()); //style list if(defaultFont.getStyle() == Font.PLAIN) { styleField.setText("Plain"); styleList.setSelectedValue("Plain", true); } if(defaultFont.getStyle() == Font.BOLD) { styleList.setSelectedValue("Bold", true); styleField.setText("Bold"); } if(defaultFont.getStyle() == Font.ITALIC) { styleList.setSelectedValue("Italic", true); styleField.setText("Italic"); } if(defaultFont.getStyle() == Font.BOLD+Font.ITALIC) { styleList.setSelectedValue("Bold & Italic", true); styleField.setText("Bold & Italic"); } styleList.ensureIndexIsVisible(styleList.getSelectedIndex()); //size list sizeList.setSelectedValue(String.valueOf(defaultFont.getSize()), true); sizeList.ensureIndexIsVisible(sizeList.getSelectedIndex()); sizeField.setText(String.valueOf(defaultFont.getSize())); nameList.addListSelectionListener(this); styleList.addListSelectionListener(this); sizeList.addListSelectionListener(this); } //create the font chooser dialog public JDialog createDialog() { dialog = new JDialog(frame, title, modal); //we must first initialize the list because otherwise it will not be correctly shown initializeList(); initDialog(); dialog.pack(); return dialog; } //a method to get the font chooser shown and return the option the user chooses public int showFontChooser() { dialog = createDialog(); //dealing with the case when the dialog is shut down by click close button in the right-up corner. dialog.addWindowListener(new WindowAdapter() { public void windowClosed(WindowEvent e) { option = CANCEL_OPTION; } }); option = ERROR_OPTION; dialog.show(); dialog.dispose(); dialog = null; return option; } //get selected font public Font getSelectedFont() { return new Font(name, style, size); } //action listener for the buttons public void actionPerformed(ActionEvent e) { //ok button clicked if(e.getSource() == okButton) { option = OK_OPTION; name = (String)(nameList.getSelectedValue()); styleString = (String)(styleList.getSelectedValue()); sizeString = (String)(sizeList.getSelectedValue()); if(styleString == "Plain") { style = Font.PLAIN; } else if(styleString == "Bold") { style = Font.BOLD; } else if(styleString == "Italic") { style = Font.ITALIC; } else if(styleString == "Bold & Italic") { style = Font.BOLD+Font.ITALIC; } size = Integer.parseInt(sizeString); dialog.dispose(); } //cancel button clicked if(e.getSource() == cancelButton) { option = CANCEL_OPTION; dialog.dispose(); } } //list selection listener public void valueChanged(ListSelectionEvent e) { //name list if(e.getSource() == nameList) { name = (String)(nameList.getSelectedValue()); nameField.setText(name); } //style list if(e.getSource() == styleList) { styleString = (String)(styleList.getSelectedValue()); styleField.setText(styleString); } //size list if(e.getSource() == sizeList) { sizeString = (String)(sizeList.getSelectedValue()); sizeField.setText(sizeString); } }}
JButton button1 = new JButton("Test");
button1.setFont(sansbold14);
JButton button1 = new JButton("Test");
button1.setFont(sansbold14);
把上面的字体该一下试试!
import java.awt.*;
public class JB{
public static void main(String args[]){
JFrame jf=new JFrame("wan");
Font sansbold14 = new Font("宋体",Font.BOLD,20);
JButton button1 = new JButton("Test");
button1.setFont(sansbold14);
jf.getContentPane().add(button1,BorderLayout.CENTER);
jf.pack();
jf.setVisible(true);
}
}我要分
GraphicsEnvironment ge =
GraphicsEnvironment.getLocalGraphicsEnvironment();
fonts = ge.getAllFonts();获得系统中所有字体,如果还没有你满意的字体那就没办法了
import javax.swing.*;
import javax.swing.event.*;
import java.awt.*;
import java.awt.event.*;// Window frame class for showing selected font sample
class FontSample extends JFrame {
Font font; // Currently shown font
String text; // Sample text to display // Constructor
public FontSample() {
super();
font = null;
text = "Abcdefg 1234567890 !@#$%^&*()";
setDefaultCloseOperation(JFrame.HIDE_ON_CLOSE);
setSize(425, 120);
} // Called before showing window
public void changeFont(Font f) {
font = f.deriveFont(24.0f); // Resize to 24 pts
setTitle(font.getFontName()); // Title = font name
if (isShowing()) repaint(); // Repaint if already visible
} // Paint sample text using current font in window
public void paint(Graphics g) {
Rectangle r = getBounds(null);
g.setColor(Color.white); // Erase background to white
g.fillRect(0, 0, r.width, r.height);
if (font != null) {
g.setFont(font);
g.setColor(Color.black);
g.drawString(text, 10, r.height / 2);
}
}
}// Main program class
public class FontDemo extends JFrame {
final protected Font[] fonts; // Array of fonts
final protected FontSample fontSample; // Sample window// Constructor
public FontDemo() {
super();
// Select local system look and feel
try {
UIManager.setLookAndFeel(
UIManager.getSystemLookAndFeelClassName());
} catch (Exception e) { }
// End program when window closes
addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {
System.exit(0);
}
}); // Create child sample font window
fontSample = new FontSample(); // Loading fonts may take a while; tell user
System.out.print("Loading font names..."); // Get available fonts in 1pt sizes
GraphicsEnvironment ge =
GraphicsEnvironment.getLocalGraphicsEnvironment();
fonts = ge.getAllFonts(); // Create a JComboBox object for listing font names
JComboBox fontBox = new JComboBox();
for (int i = 0; i < fonts.length; i++)
fontBox.addItem(fonts[i].getFontName());
fontBox.setEditable(false); // Respond to item selection
fontBox.addActionListener(
new ActionListener() {
public void actionPerformed(ActionEvent e) {
JComboBox box = (JComboBox)e.getSource();
int fontIndex = box.getSelectedIndex();
fontSample.changeFont(fonts[fontIndex]);
fontSample.show();
}
}
);
System.out.println("\nSelect font for a sample");
Container content = getContentPane();
content.setLayout(new FlowLayout());
content.add(new JLabel("Available fonts"));
content.add(fontBox);
} public static void main(String[] args) {
FontDemo app = new FontDemo();
app.setTitle("Font Demonstration");
app.setSize(320, 240);
app.show();
}
}
需要在JBuilder下运行,因为用到了XYLayout
/**
* <p>Title: JavaMe</p>
* <p>Description: </p>
* <p>Copyright: Copyright (c) 2003</p>
* <p>Company: ISMT, HKUST</p>
* @author group4
* @version 1.0
*/
import java.awt.*;
import javax.swing.*;
import javax.swing.border.*;
import java.awt.event.*;
import javax.swing.event.*;
import javax.swing.text.*;import com.borland.jbcl.layout.*;
import MainApplication.*;
public class FontChooser implements ActionListener, ListSelectionListener
{
private JDialog dialog;
private JPanel panel = new JPanel();
private XYLayout xYLayout = new XYLayout();
private JTextField nameField = new JTextField();
private JTextField styleField = new JTextField();
private JTextField sizeField = new JTextField();
private JList nameList = new JList();
private JPanel namePanel = new JPanel();
private Border border;
private TitledBorder titledBorder1, titledBorder2, titledBorder3;
private JPanel stylePanel = new JPanel();
private JPanel sizePanel = new JPanel();
private JList styleList = new JList();
private JList sizeList = new JList();
private JButton okButton = new JButton();
private JButton cancelButton = new JButton(); //font attributes
private Font [] fonts;
private String [] fontNames;
private String [] fontStyles = {"Plain", "Bold", "Italic", "Bold & Italic"};
private String [] fontSizes = {"8", "9", "10", "11", "12", "13", "14", "16", "18",
"20", "22", "24", "28", "32", "36", "48", "60", "72"};
private String name, styleString, sizeString;
private int style = Font.PLAIN, size = 12; private Font defaultFont = new Font("Times New Roman", Font.PLAIN, 12); //approve status
public static final int OK_OPTION = 1;
public static final int CANCEL_OPTION = 2;
public static final int ERROR_OPTION = 3;
private int option; private Frame frame;
private String title;
private boolean modal; //constructor with arguments
public FontChooser(Frame frame, String title, boolean modal)
{
this.frame = frame;
this.title = title;
this.modal = modal;
}
//constructor with arguments
public FontChooser(Frame frame, String title, boolean modal, Font defaultFont)
{
this.defaultFont = defaultFont;
this.frame = frame;
this.title = title;
this.modal = modal;
}
//constructor without arguments
public FontChooser()
{
this(null, "Font Chooser", true);
}
//initialize method
private void initDialog()
{
panel.setLayout(xYLayout);
//buttons
okButton.setText("OK");
cancelButton.setText("Cancel");
okButton.addActionListener(this);
cancelButton.addActionListener(this); xYLayout.setHeight(245);
xYLayout.setWidth(340); //customize titledBorders
titledBorder1 = new TitledBorder(BorderFactory.createMatteBorder(1,1,1,1,Color.black),"Font Name:");
titledBorder2 = new TitledBorder(BorderFactory.createMatteBorder(1,1,1,1,Color.black),"Font Style:");
titledBorder3 = new TitledBorder(BorderFactory.createMatteBorder(1,1,1,1,Color.black),"Font Size:"); //customize the three panels
namePanel.setBorder(titledBorder1);
namePanel.setLayout(new BorderLayout());
stylePanel.setBorder(titledBorder2);
stylePanel.setLayout(new BorderLayout());
sizePanel.setBorder(titledBorder3);
sizePanel.setLayout(new BorderLayout()); //text fields
nameField.setEditable(false);
styleField.setEditable(false);
sizeField.setEditable(false); stylePanel.add(styleField, BorderLayout.NORTH);
stylePanel.add(new JScrollPane(styleList), BorderLayout.CENTER);
panel.add(okButton, new XYConstraints(176, 195, 71, -1));
panel.add(sizePanel, new XYConstraints(263, 16, 66, 163));
namePanel.add(nameField, BorderLayout.NORTH);
namePanel.add(new JScrollPane(nameList), BorderLayout.CENTER);
sizePanel.add(sizeField, BorderLayout.NORTH);
sizePanel.add(new JScrollPane(sizeList), BorderLayout.CENTER);
panel.add(namePanel, new XYConstraints(15, 17, 148, 163));
panel.add(cancelButton, new XYConstraints(253, 195, -1, -1));
panel.add(stylePanel, new XYConstraints(169, 17, 90, 162));
border = new MatteBorder(null);
stylePanel.setVisible(true);
sizePanel.setVisible(true); dialog.getContentPane().add(panel, BorderLayout.CENTER);
dialog.setSize(342, 250);
dialog.setResizable(false);
//Center the window
Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
Dimension frameSize = dialog.getSize();
if (frameSize.height > screenSize.height)
{
frameSize.height = screenSize.height;
}
if (frameSize.width > screenSize.width)
{
frameSize.width = screenSize.width;
}
dialog.setLocation((screenSize.width - frameSize.width) / 2, (screenSize.height - frameSize.height) / 2); }
//initialize method
private void initializeList()
{
//get the fonts of the local platforms
GraphicsEnvironment ge = GraphicsEnvironment.getLocalGraphicsEnvironment();
fonts = ge.getAllFonts();
fontNames = new String[fonts.length];
for(int i=0; i<fonts.length; i++)
{
fontNames[i] = fonts[i].getName();
}
//create the list using the above attributes
nameList = new JList(fontNames);
styleList = new JList(fontStyles);
sizeList = new JList(fontSizes); //set the default value
//name list
nameList.setSelectedValue(defaultFont.getFontName(), true);
nameList.ensureIndexIsVisible(nameList.getSelectedIndex());
nameField.setText(defaultFont.getFontName());
//style list
if(defaultFont.getStyle() == Font.PLAIN)
{
styleField.setText("Plain");
styleList.setSelectedValue("Plain", true);
}
if(defaultFont.getStyle() == Font.BOLD)
{
styleList.setSelectedValue("Bold", true);
styleField.setText("Bold");
}
if(defaultFont.getStyle() == Font.ITALIC)
{
styleList.setSelectedValue("Italic", true);
styleField.setText("Italic");
}
if(defaultFont.getStyle() == Font.BOLD+Font.ITALIC)
{
styleList.setSelectedValue("Bold & Italic", true);
styleField.setText("Bold & Italic");
}
styleList.ensureIndexIsVisible(styleList.getSelectedIndex()); //size list
sizeList.setSelectedValue(String.valueOf(defaultFont.getSize()), true);
sizeList.ensureIndexIsVisible(sizeList.getSelectedIndex());
sizeField.setText(String.valueOf(defaultFont.getSize())); nameList.addListSelectionListener(this);
styleList.addListSelectionListener(this);
sizeList.addListSelectionListener(this);
}
//create the font chooser dialog
public JDialog createDialog()
{
dialog = new JDialog(frame, title, modal); //we must first initialize the list because otherwise it will not be correctly shown
initializeList();
initDialog();
dialog.pack(); return dialog;
} //a method to get the font chooser shown and return the option the user chooses
public int showFontChooser()
{
dialog = createDialog(); //dealing with the case when the dialog is shut down by click close button in the right-up corner.
dialog.addWindowListener(new WindowAdapter()
{
public void windowClosed(WindowEvent e)
{
option = CANCEL_OPTION;
}
}); option = ERROR_OPTION; dialog.show();
dialog.dispose();
dialog = null; return option;
}
//get selected font
public Font getSelectedFont()
{
return new Font(name, style, size);
}
//action listener for the buttons
public void actionPerformed(ActionEvent e)
{
//ok button clicked
if(e.getSource() == okButton)
{
option = OK_OPTION; name = (String)(nameList.getSelectedValue());
styleString = (String)(styleList.getSelectedValue());
sizeString = (String)(sizeList.getSelectedValue());
if(styleString == "Plain")
{
style = Font.PLAIN;
}
else if(styleString == "Bold")
{
style = Font.BOLD;
}
else if(styleString == "Italic")
{
style = Font.ITALIC;
}
else if(styleString == "Bold & Italic")
{
style = Font.BOLD+Font.ITALIC;
} size = Integer.parseInt(sizeString); dialog.dispose();
} //cancel button clicked
if(e.getSource() == cancelButton)
{
option = CANCEL_OPTION;
dialog.dispose();
}
}
//list selection listener
public void valueChanged(ListSelectionEvent e)
{
//name list
if(e.getSource() == nameList)
{
name = (String)(nameList.getSelectedValue());
nameField.setText(name);
}
//style list
if(e.getSource() == styleList)
{
styleString = (String)(styleList.getSelectedValue());
styleField.setText(styleString);
}
//size list
if(e.getSource() == sizeList)
{
sizeString = (String)(sizeList.getSelectedValue());
sizeField.setText(sizeString);
}
}}