本人不才!花了很长时间写了这个代码。不是很好,比起Windows自带的字体对话框差一点。
不过已经够用了。等将来有时间了,在进行完善。
如果有什么好的建议,可以提。本人再补。
预览效果如下图:
package styleDispose;
import java.awt.GraphicsEnvironment;
import javax.swing.JDialog;
import javax.swing.JFrame;
import javax.swing.ListModel;
import java.awt.Font;
import javax.swing.JPanel;
import java.awt.Rectangle;
import javax.swing.JScrollPane;
import javax.swing.BorderFactory;
import javax.swing.border.TitledBorder;
import java.awt.Color;
import javax.swing.JList;
import javax.swing.JLabel;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import javax.swing.JTextField;
import javax.swing.border.BevelBorder;
import javax.swing.border.SoftBevelBorder;
import javax.swing.JButton;
import javax.swing.SwingConstants;
public class FontChooserDialog extends JDialog {
/**
*
*/
private static final long serialVersionUID = 1L;
/**
* 界面设计需要
* */
private JPanel jPanel = null;
private JScrollPane jScrollPane = null;
private JPanel jPanel1 = null;
private JLabel jLabel = null;
private JLabel jLabel1 = null;
private JLabel jLabel2 = null;
private JTextField fontNameText = null;
private JTextField fontItalicText = null;
private JTextField fontSizeText = null;
private JList fontNameList = null;
private JList fontItalicList = null;
private JList fontSizeList = null;
private JPanel jPanel2 = null;
private JButton okButton = null;
private JButton regitButton = null;
private JButton cancleButton = null;
private JScrollPane jScrollPane1 = null;
private JScrollPane jScrollPane2 = null;
private JScrollPane jScrollPane3 = null;
private static JLabel fontStyle = null;
/**
* 字体默认变量
* */
private Font defaultFont = new Font("\u5b8b\u4f53", Font.PLAIN, 12);
/**
* 返回字体变量
* */
private static Font returnFont = null;
/**
* Boolean 变量,判断是否正常返回,是否用户选择了字体
* */
private static boolean judge = false;
/**
* 以防止事件重复调用或不必要的更改,定义两个boolean变量分别
* 为:fontNameList和fontSizeList判断
* 等于true则循环调用,false则不
* */
private boolean nameJuge = true;
private boolean sizeJuge = true;
public FontChooserDialog(){
this(null);
}
public FontChooserDialog(JFrame jframe){
this(jframe,true);
}
public FontChooserDialog(JFrame jframe,boolean boo){
this(jframe,boo,null);
}
public FontChooserDialog(JFrame jframe,boolean boo,Font font){
super(jframe,boo);
initialize();
initializeFont(font);
this.setLocationRelativeTo(jframe);
}
/**
* This method initializes this
*
*/
private void initialize() {
this.setContentPane(getJPanel());
this.setFont(new Font("\u5b8b\u4f53", Font.PLAIN, 12));
this.setBounds(new Rectangle(0, 0, 430, 335));
this.setTitle("字体选择对话框");
this.addWindowListener(new WindowAdapter(){
public void windowClosing(WindowEvent e) {
judge = false;
closeWindow();
}
});
}
public static Font showDialog(JFrame jframe,