日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

java中如何调出字体对话框_java 字体对话框

發布時間:2024/7/19 编程问答 28 豆豆
生活随笔 收集整理的這篇文章主要介紹了 java中如何调出字体对话框_java 字体对话框 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

本人不才!花了很長時間寫了這個代碼。不是很好,比起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,boolean boo){

return showDialog(jframe,boo,null);

}

public static Font showDialog(JFrame jframe,boolean boo,Font font){

JDialog jd = new FontChooserDialog(jframe,boo,font);

jd.setVisible(true);

if(judge){returnFont = fontStyle.getFont();}

jd.dispose();

return returnFont;

}

/**

* This method initializes jPanel

*

* @return javax.swing.JPanel

*/

private JPanel getJPanel() {

if (jPanel == null) {

jPanel = new JPanel();

jPanel.setLayout(null);

jPanel.setFont(new Font("Dialog", Font.PLAIN, 12));

jPanel.add(getJPanel1(), null);

jPanel.add(getJPanel2(), null);

jPanel.add(getOkButton(), null);

jPanel.add(getRegitButton(), null);

jPanel.add(getCancleButton(), null);

}

return jPanel;

}

/**

* This method initializes jScrollPane

*

* @return javax.swing.JScrollPane

*/

private JScrollPane getJScrollPane() {

if (jScrollPane == null) {

fontStyle = new JLabel();

fontStyle.setText("你好!天生我才必有用!Hello World!");

fontStyle.setFont(new Font("\u5b8b\u4f53", Font.PLAIN, 12));

fontStyle.setHorizontalAlignment(SwingConstants.CENTER);

fontStyle.setHorizontalTextPosition(SwingConstants.CENTER);

jScrollPane = new JScrollPane();

jScrollPane.setBorder(BorderFactory.createBevelBorder(BevelBorder.LOWERED));

jScrollPane.setViewportView(fontStyle);

jScrollPane.setBounds(new Rectangle(5, 20, 400, 60));

}

return jScrollPane;

}

/**

* This method initializes jPanel1

*

* @return javax.swing.JPanel

*/

private JPanel getJPanel1() {

if (jPanel1 == null) {

jLabel2 = new JLabel();

jLabel2.setBounds(new Rectangle(285, 5, 120, 15));

jLabel2.setFont(new Font("\u5b8b\u4f53", Font.PLAIN, 12));

jLabel2.setText("大小:");

jLabel1 = new JLabel();

jLabel1.setBounds(new Rectangle(160, 5, 120, 15));

jLabel1.setFont(new Font("\u5b8b\u4f53", Font.PLAIN, 12));

jLabel1.setText("字型:");

jLabel = new JLabel();

jLabel.setBounds(new Rectangle(5, 5, 150, 15));

jLabel.setDisplayedMnemonic(KeyEvent.VK_UNDEFINED);

jLabel.setFont(new Font("\u5b8b\u4f53", Font.PLAIN, 12));

jLabel.setText("字體:");

jPanel1 = new JPanel();

jPanel1.setLayout(null);

jPanel1.setBounds(new Rectangle(5, 5, 410, 175));

jPanel1.setBorder(new SoftBevelBorder(SoftBevelBorder.LOWERED));

jPanel1.add(jLabel, null);

jPanel1.add(jLabel1, null);

jPanel1.add(jLabel2, null);

jPanel1.add(getFontNameText(), null);

jPanel1.add(getFontItalicText(), null);

jPanel1.add(getFontSizeText(), null);

jPanel1.add(getJScrollPane1(), null);

jPanel1.add(getJScrollPane2(), null);

jPanel1.add(getJScrollPane3(), null);

}

return jPanel1;

}

/**

* This method initializes fontNameText

*

* @return javax.swing.JTextField

*/

private JTextField getFontNameText() {

if (fontNameText == null) {

fontNameText = new JTextField();

fontNameText.setBounds(new Rectangle(5, 25, 150, 20));

fontNameText.setFont(new Font("\u5b8b\u4f53", Font.PLAIN, 12));

fontNameText.addKeyListener(new KeyAdapter(){

public void keyTyped(KeyEvent e) {

String oldText = fontNameText.getText();

String newText = "";

if("".equals(fontNameText.getSelectedText()) && null == fontNameText.getSelectedText()){

newText = fontNameText.getText()+e.getKeyChar();

}else{

newText = oldText.substring(0,fontNameText.getSelectionStart())+e.getKeyChar()+oldText.substring(fontNameText.getSelectionEnd());

}

System.out.println("fontName:"+newText);

nameJuge = false;

fontNameList.setSelectedValue(getLateIndex(fontNameList,newText),true);

nameJuge = true;

}

});

}

return fontNameText;

}

/**

* This method initializes fontItalicText

*

* @return javax.swing.JTextField

*/

private JTextField getFontItalicText() {

if (fontItalicText == null) {

fontItalicText = new JTextField();

fontItalicText.setBounds(new Rectangle(160, 25, 120, 20));

fontItalicText.setFont(new Font("\u5b8b\u4f53", Font.PLAIN, 12));

fontItalicText.setEnabled(false);

}

return fontItalicText;

}

/**

* This method initializes fontSizeText

*

* @return javax.swing.JTextField

*/

private JTextField getFontSizeText() {

if (fontSizeText == null) {

fontSizeText = new JTextField();

fontSizeText.setBounds(new Rectangle(285, 25, 120, 20));

fontSizeText.setFont(new Font("\u5b8b\u4f53", Font.PLAIN, 12));

fontSizeText.setColumns(4);

fontSizeText.addKeyListener(new KeyAdapter(){

public void keyTyped(KeyEvent e) {

//System.out.println("Key Code:"+(Character.getNumericValue(e.getKeyChar())==-1));

String oldText = fontSizeText.getText();

String newText = "";

if("".equals(fontSizeText.getSelectedText()) && null == fontSizeText.getSelectedText()){

newText = fontSizeText.getText()+e.getKeyChar();

}else{

newText = oldText.substring(0,fontSizeText.getSelectionStart())+e.getKeyChar()+oldText.substring(fontSizeText.getSelectionEnd());

}

//System.out.println("fontSize:"+newText);

sizeJuge = false;

fontSizeList.setSelectedValue(getLateIndex(fontSizeList,newText),true);

if(newText.matches("(\\d)+")){

fontStyle.setFont(new Font(fontStyle.getFont().getFontName(),fontStyle.getFont().getStyle(),Integer.parseInt(newText)));

}

sizeJuge = true;

}

});

}

return fontSizeText;

}

/**

* This method initializes fontNameList

*

* @return javax.swing.JList

*/

private JList getFontNameList() {

if (fontNameList == null) {

fontNameList = new JList(GraphicsEnvironment.getLocalGraphicsEnvironment()

.getAvailableFontFamilyNames());

fontNameList.setFont(new Font("\u5b8b\u4f53", Font.PLAIN, 12));

fontNameList

.addListSelectionListener(new javax.swing.event.ListSelectionListener() {

public void valueChanged(javax.swing.event.ListSelectionEvent e) {

if(nameJuge){fontNameText.setText(fontNameList.getSelectedValue().toString());}

fontStyle.setFont(new Font(fontNameList.getSelectedValue().toString(),fontStyle.getFont().getStyle(),fontStyle.getFont().getSize()));

}

});

}

return fontNameList;

}

/**

* This method initializes fontItalicList

*

* @return javax.swing.JList

*/

private JList getFontItalicList() {

if (fontItalicList == null) {

fontItalicList = new JList(new String[]{"Plain", "Bold", "Italic","Bold Italic"});

fontItalicList.setFont(new Font("\u5b8b\u4f53", Font.PLAIN, 12));

fontItalicList

.addListSelectionListener(new javax.swing.event.ListSelectionListener() {

public void valueChanged(javax.swing.event.ListSelectionEvent e) {

fontItalicText.setText(fontItalicList.getSelectedValue().toString());

fontStyle.setFont(new Font(fontStyle.getFont().getFontName(),fontItalicList.getSelectedIndex(),fontStyle.getFont().getSize()));

}

});

}

return fontItalicList;

}

/**

* This method initializes fontSizeList

*

* @return javax.swing.JList

*/

private JList getFontSizeList() {

if (fontSizeList == null) {

fontSizeList = new JList(new String[]{"3", "4", "5", "6", "7", "8", "9", "10",

"11", "12", "13", "14", "15", "16", "17", "18", "19", "20", "22",

"24", "27", "30", "34", "39", "45", "51", "60"});

fontSizeList.setFont(new Font("\u5b8b\u4f53", Font.PLAIN, 12));

fontSizeList

.addListSelectionListener(new javax.swing.event.ListSelectionListener() {

public void valueChanged(javax.swing.event.ListSelectionEvent e) {

if(sizeJuge){fontSizeText.setText(fontSizeList.getSelectedValue().toString());}

fontStyle.setFont(new Font(fontStyle.getFont().getFontName(),fontStyle.getFont().getStyle(),Integer.parseInt(fontSizeList.getSelectedValue().toString())));

}

});

}

return fontSizeList;

}

/**

* This method initializes jPanel2

*

* @return javax.swing.JPanel

*/

private JPanel getJPanel2() {

if (jPanel2 == null) {

jPanel2 = new JPanel();

jPanel2.setLayout(null);

jPanel2.setBounds(new Rectangle(3, 180, 414, 90));

jPanel2.setBorder(BorderFactory.createTitledBorder(null, "\u6548\u679c\u9884\u89c8", TitledBorder.DEFAULT_JUSTIFICATION, TitledBorder.DEFAULT_POSITION, new Font("\u5b8b\u4f53", Font.PLAIN, 12), new Color(51, 51, 51)));

jPanel2.add(getJScrollPane(), null);

}

return jPanel2;

}

/**

* This method initializes okButton

*

* @return javax.swing.JButton

*/

private JButton getOkButton() {

if (okButton == null) {

okButton = new JButton();

okButton.setBounds(new Rectangle(215, 275, 60, 20));

okButton.setFont(new Font("\u5b8b\u4f53", Font.PLAIN, 12));

okButton.setText("確定");

okButton.addActionListener(new java.awt.event.ActionListener() {

public void actionPerformed(java.awt.event.ActionEvent e) {

judge = true;

closeWindow();

}

});

}

return okButton;

}

/**

* This method initializes regitButton

*

* @return javax.swing.JButton

*/

private JButton getRegitButton() {

if (regitButton == null) {

regitButton = new JButton();

regitButton.setBounds(new Rectangle(285, 275, 60, 20));

regitButton.setFont(new Font("\u5b8b\u4f53", Font.PLAIN, 12));

regitButton.setText("重置");

regitButton.addActionListener(new java.awt.event.ActionListener() {

public void actionPerformed(java.awt.event.ActionEvent e) {

initializeFont(null);

}

});

}

return regitButton;

}

/**

* This method initializes cancleButton

*

* @return javax.swing.JButton

*/

private JButton getCancleButton() {

if (cancleButton == null) {

cancleButton = new JButton();

cancleButton.setBounds(new Rectangle(355, 275, 60, 20));

cancleButton.setFont(new Font("\u5b8b\u4f53", Font.PLAIN, 12));

cancleButton.setText("取消");

cancleButton.addActionListener(new java.awt.event.ActionListener() {

public void actionPerformed(java.awt.event.ActionEvent e) {

judge = false;

closeWindow();

}

});

}

return cancleButton;

}

/**

* This method initializes jScrollPane1

*

* @return javax.swing.JScrollPane

*/

private JScrollPane getJScrollPane1() {

if (jScrollPane1 == null) {

jScrollPane1 = new JScrollPane();

jScrollPane1.setBounds(new Rectangle(5, 50, 150, 120));

jScrollPane1.setViewportView(getFontNameList());

}

return jScrollPane1;

}

/**

* This method initializes jScrollPane2

*

* @return javax.swing.JScrollPane

*/

private JScrollPane getJScrollPane2() {

if (jScrollPane2 == null) {

jScrollPane2 = new JScrollPane();

jScrollPane2.setBounds(new Rectangle(160, 50, 120, 120));

jScrollPane2.setViewportView(getFontItalicList());

}

return jScrollPane2;

}

/**

* This method initializes jScrollPane3

*

* @return javax.swing.JScrollPane

*/

private JScrollPane getJScrollPane3() {

if (jScrollPane3 == null) {

jScrollPane3 = new JScrollPane();

jScrollPane3.setBounds(new Rectangle(285, 50, 120, 120));

jScrollPane3.setViewportView(getFontSizeList());

}

return jScrollPane3;

}

/**

* 默認的字體初始化方法

* */

private void initializeFont(Font font){

if(font!=null){

defaultFont = font;

fontStyle.setFont(defaultFont);

}

fontStyle.setFont(defaultFont);

fontNameList.setSelectedValue(defaultFont.getFontName(), true);

fontSizeList.setSelectedValue(new Integer(defaultFont.getSize()).toString(), true);

fontItalicList.setSelectedIndex(defaultFont.getStyle());

}

/**

* 判斷里給定的值最近的索引

* */

private Object getLateIndex(JList jlist,String str){

ListModel list = jlist.getModel();

if(str.matches("(\\d)+")){

for(int i = list.getSize()-1;i>=0;i--){

if(Integer.parseInt(list.getElementAt(i).toString())<=Integer.parseInt(str)){

return list.getElementAt(i);

}

}

}else {

for(int i = list.getSize()-2;i>=0;i--){

if(str.compareToIgnoreCase(list.getElementAt(i).toString())==0){

return list.getElementAt(i);

}else if(str.compareToIgnoreCase(list.getElementAt(i).toString())>0){

return list.getElementAt(i+1);

}

}

}

return list.getElementAt(0);

}

/**

* 窗體關閉方法!

* */

private void closeWindow(){

this.setVisible(false);

}

} // @jve:decl-index=0:visual-constraint="91,34"

總結

以上是生活随笔為你收集整理的java中如何调出字体对话框_java 字体对话框的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。