java實(shí)現(xiàn)仿windows 字體設(shè)置選項(xiàng)卡實(shí)例
想用java做一個(gè)像windows里一樣的txt編輯軟件,涉及到字體設(shè)置選項(xiàng)卡,在網(wǎng)上找了很久都沒找到,就生氣啦自己寫一個(gè),現(xiàn)在貼這里分享一下,下次再遇到這樣的問題就不用自己親自打代碼啦!
package 實(shí)驗(yàn);
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import javax.swing.*;
import javax.swing.border.BevelBorder;
/**
* 字體格式設(shè)置對(duì)話框
*/
public class FontFormat extends JDialog {
private JLabel nameLb;
private JLabel styleLb;
private JLabel sizeLb;
private JLabel presLb;
private JTextField nameTx;
private JTextField styleTx;
private JTextField sizeTx;
private JTextField presTx;
private JList nameLt;
private JList styleLt;
private JList sizeLt;
private JScrollPane jScrollPane1;
private JScrollPane jScrollPane2;
private JScrollPane jScrollPane3;
private JButton approve;
private JButton cancel;
private JButton chose;
private JRadioButton[] language = new JRadioButton[2];
private ButtonGroup languageg;
private String Slanguage[] = { new String("李濤"), new String("ABC") };
private static JFrame frame;
public Font font, newFont;// 字體類
private Color color;// 顏色類
Color newColor;
private JFileChooser fileChoose = new JFileChooser();// 文件選擇類實(shí)例
private JDialog colorDlg;// 顏色對(duì)話框
private JColorChooser colorChoose = new JColorChooser();// 顏色選擇類實(shí)例
private GraphicsEnvironment environment; // 該類中又獲取系統(tǒng)字體的方法;
private String[] fontNameSet;// 字體‘邏輯名'集
// 字體‘樣式'集的字符串?dāng)?shù)組
private String[] fontStyleSet = { "常規(guī)", "傾斜", "加粗", "傾斜 加粗" };
// 字體‘樣式'集的常量數(shù)組
private Integer[] fontCon = { Font.PLAIN, Font.ITALIC, Font.BOLD,
Font.BOLD | Font.ITALIC };
// 字體‘大小'集
private String[] fontSizeSet = { "6", "7", "8", "9", "10", "11", "12",
"14", "16", "18", "20", "22", "24", "26", "28", "36", "48", "72" };
public static void main(String args[]) {// 主函數(shù)
FontFormat a = new FontFormat();
a.setVisible(true);
}
public FontFormat() {// 無參構(gòu)造函數(shù)
super(frame, "李濤—字體設(shè)置窗口", true);
frame = new JFrame();
initGUI();
}
public FontFormat(JFrame frame) {// 含參構(gòu)造函數(shù)
super(frame, "李濤—字體設(shè)置窗口", true);
this.frame = frame;// 父窗口中必須有一個(gè)public的Font對(duì)象
// setAlwaysOnTop(true);
initGUI();
}
private void initGUI() {// 字體格式選擇器的界面初始化
try {
getContentPane().setLayout(null);
environment = GraphicsEnvironment.getLocalGraphicsEnvironment();// GraphicsEnvironment是一個(gè)抽象類,不能實(shí)例化,只能用其中的靜態(tài)方法獲取一個(gè)實(shí)例
fontNameSet = environment.getAvailableFontFamilyNames();// 獲取系統(tǒng)字體
addMenu();// 加入菜單
initFont();// 初始化字體
// pack();
setSize(380, 337);
setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
setWindowPos();// 使窗口屏幕居中
setResizable(false);// 大小不可變
} catch (Exception e) {
e.printStackTrace();
}
}
private void initFont() {// 初始化字體
// 設(shè)置默認(rèn)字體格式為父窗口font對(duì)向的字體格式
if (frame.getFont() == null) {
nameTx.setText(fontNameSet[0]);
styleTx.setText(fontStyleSet[0]);
sizeTx.setText("12");
nameLt.setSelectedValue(fontNameSet[0], true);
styleLt.setSelectedIndex(0);
sizeLt.setSelectedValue("12", true);
font = new Font(fontNameSet[0], fontCon[0], 12);
newFont = font;// 保存原來的字體格式
presTx.setFont(font);
// JOptionPane.showMessageDialog(null, "ccac");
} else {
int idxStyle = 0;
for (int i = 0; i < fontCon.length; i++) {
if (fontCon[i] == frame.getFont().getStyle())
idxStyle = i;
}
nameTx.setText(frame.getFont().getName());// 改text
styleTx.setText(fontStyleSet[idxStyle]);
sizeTx.setText("" + frame.getFont().getSize());
nameLt.setSelectedValue(frame.getFont().getName(), true);// 改list顯示
styleLt.setSelectedIndex(idxStyle);
sizeLt.setSelectedValue("" + frame.getFont().getSize(), true);
font = new Font(fontNameSet[0], fontCon[0], 12);// 保存當(dāng)前格式
newFont = font;// 保存原來的字體格式
presTx.setFont(font);// 預(yù)覽中設(shè)為當(dāng)前模式
}
}
private void addMenu() {// 加入菜單
// 4個(gè)lable---------------------------------------------------------------------------------
nameLb = new JLabel();
getContentPane().add(nameLb);
nameLb.setText("字體:");
nameLb.setBounds(10, 14, 120, 26);
nameLb.setFont(new java.awt.Font("SimSun", 1, 14));
styleLb = new JLabel();
getContentPane().add(styleLb);
styleLb.setText("字型:");
styleLb.setBounds(151, 14, 120, 23);
styleLb.setFont(new java.awt.Font("SimSun", 1, 14));
sizeLb = new JLabel();
getContentPane().add(sizeLb);
sizeLb.setText("大?。?);
sizeLb.setBounds(275, 14, 79, 24);
sizeLb.setFont(new java.awt.Font("SimSun", 1, 14));
presLb = new JLabel();
getContentPane().add(presLb);
presLb.setText("預(yù)覽:");
presLb.setBounds(151, 150, 120, 80);
presLb.setFont(new java.awt.Font("SimSun", 1, 14));
// 4個(gè)textfield---------------------------------------------------------------------------------
nameTx = new JTextField();
nameTx.setEditable(false);
getContentPane().add(nameTx);
nameTx.setBounds(10, 42, 120, 22);
styleTx = new JTextField();
styleTx.setEditable(false);
getContentPane().add(styleTx);
styleTx.setBounds(151, 42, 100, 21);
sizeTx = new JTextField();
sizeTx.setEditable(false);
getContentPane().add(sizeTx);
sizeTx.setBounds(275, 42, 79, 22);
presTx = new JTextField();
presTx.setEditable(false);
getContentPane().add(presTx);
presTx.setBounds(151, 200, 203, 61);
presTx.setText(Slanguage[1]);
// 3個(gè)下拉條--+監(jiān)聽-----------------------------------------------------------------------------
jScrollPane1 = new JScrollPane();
getContentPane().add(jScrollPane1);
jScrollPane1.setBounds(10, 74, 120, 210);
{
ListModel fontNameModel = new DefaultComboBoxModel(fontNameSet);
nameLt = new JList();
jScrollPane1.setViewportView(nameLt);
nameLt.setModel(fontNameModel);
nameLt.setBounds(274, 193, 90, 86);
nameLt.setBorder(BorderFactory
.createEtchedBorder(BevelBorder.LOWERED));
nameLt.addMouseListener(new MouseAdapter() {
public void mouseClicked(MouseEvent evt) {
nameLtMouseClicked(evt);
}
});
}
jScrollPane2 = new JScrollPane();
getContentPane().add(jScrollPane2);
jScrollPane2.setBounds(151, 74, 100, 103);
{
ListModel fontStyleModel = new DefaultComboBoxModel(fontStyleSet);
styleLt = new JList();
jScrollPane2.setViewportView(styleLt);
styleLt.setModel(fontStyleModel);
styleLt.setBounds(310, 215, 70, 102);
styleLt.setBorder(BorderFactory
.createEtchedBorder(BevelBorder.LOWERED));
styleLt.addMouseListener(new MouseAdapter() {
public void mouseClicked(MouseEvent evt) {
styleLtMouseClicked(evt);
}
});
}
jScrollPane3 = new JScrollPane();
getContentPane().add(jScrollPane3);
jScrollPane3.setBounds(275, 75, 79, 100);
{
ListModel fontSizeModel = new DefaultComboBoxModel(fontSizeSet);
sizeLt = new JList();
jScrollPane3.setViewportView(sizeLt);
sizeLt.setModel(fontSizeModel);
sizeLt.setBounds(300, 218, 54, 102);
sizeLt.setBorder(BorderFactory
.createEtchedBorder(BevelBorder.LOWERED));
sizeLt.addMouseListener(new MouseAdapter() {
public void mouseClicked(MouseEvent evt) {
sizeLtMouseClicked(evt);
}
});
}// -------------------------------------------------------------------------------------
// 中英選項(xiàng)(---------------------------------------------------------------------------------
languageg = new ButtonGroup();
language[0] = new JRadioButton("中");
getContentPane().add(language[0]);
language[0].setSelected(false);// 初始化顯示
language[0].setBounds(271, 179, 40, 20);
language[0].setFont(new java.awt.Font("SimSun", 1, 12));
languageg.add(language[0]);
language[0].addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent evt) {
presTx.setText(Slanguage[0]);
}
});
language[1] = new JRadioButton("英");
getContentPane().add(language[1]);
language[1].setSelected(true);
language[1].setBounds(321, 179, 40, 20);
language[1].setFont(new java.awt.Font("SimSun", 1, 12));
languageg.add(language[1]);
language[1].addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent evt) {
presTx.setText(Slanguage[1]);
}
});
// 3個(gè)按鈕+監(jiān)聽---------------------------------------------------------------------------------
// 確定按鈕
approve = new JButton();
getContentPane().add(approve);
approve.setText("確定");
approve.setBounds(151, 265, 67, 20);
approve.setFont(new java.awt.Font("KaiTi_GB2312", 1, 12));
approve.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent evt) {
approveActionPerformed(evt);
}
});
// 取消按鈕
cancel = new JButton();
getContentPane().add(cancel);
cancel.setText("取消");
cancel.setBounds(219, 265, 67, 20);
cancel.setFont(new java.awt.Font("KaiTi_GB2312", 1, 12));
cancel.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent evt) {
cancelActionPerformed(evt);
}
});
// 顏色選擇按鈕
chose = new JButton();
getContentPane().add(chose);
chose.setText("顏色");
chose.setBounds(287, 265, 67, 20);
chose.setFont(new java.awt.Font("KaiTi_GB2312", 1, 12));
chose.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent evt) {
choseActionPerformed(evt);
}
});// -------------------------------------------------------------------------
}
private void setWindowPos() {// 窗口居中
Toolkit kit = Toolkit.getDefaultToolkit();// 抽象類,通過靜態(tài)方法獲取實(shí)例
Dimension frameSize = new Dimension(), screenSize = kit.getScreenSize(); // 獲取屏幕的大小
getSize(frameSize); // 獲取窗口大小
setLocation((screenSize.width - frameSize.width) / 2,
(screenSize.height - frameSize.height) / 2);
}
private void nameLtMouseClicked(MouseEvent evt) {// 字體邏輯名列表的鼠標(biāo)單擊事件
nameTx.setText(nameLt.getSelectedValue().toString());
font = new Font(nameTx.getText(), font.getStyle(), font.getSize());
presTx.setFont(font);
}
private void styleLtMouseClicked(MouseEvent evt) {// 字體樣式列表的鼠標(biāo)單擊事件
String temp = styleLt.getSelectedValue().toString();
styleTx.setText(temp);
int index = 0;
while (index < 4 && !fontStyleSet[index].equals(temp)) {
index++;
}
font = new Font(font.getName(), fontCon[index], font.getSize());
presTx.setFont(font);
}
private void sizeLtMouseClicked(MouseEvent evt) {// 字體大小列表的鼠標(biāo)點(diǎn)擊事件
sizeTx.setText(sizeLt.getSelectedValue().toString());
font = new Font(font.getName(), font.getStyle(),
Integer.parseInt(sizeTx.getText()));
presTx.setFont(font);
}
private void approveActionPerformed(ActionEvent evt) {// 確定按鈕的觸發(fā)事件
String name = nameTx.getText();
int style = fontCon[styleLt.getSelectedIndex()];
int size = Integer.parseInt(sizeTx.getText());
font = new Font(name, style, size);
frame.setFont(font); // 父窗口的Font對(duì)象
newFont = font;// 更新原來保存格式
newColor = color;// 更新顏色
this.dispose();
}
private void cancelActionPerformed(ActionEvent evt) {// 取消按鈕的觸發(fā)事件
this.dispose();
}
private void choseActionPerformed(ActionEvent evt) {// 顏色選擇觸發(fā)事件
if (colorDlg == null) {
colorDlg = JColorChooser.createDialog(FontFormat.this,
"Select Text Color", true, colorChoose,
new ColorOKListener(), null);
}
colorChoose.setColor(color = presTx.getForeground());
colorDlg.setVisible(true);
}
class ColorOKListener implements ActionListener {// 重寫顏色按鈕點(diǎn)擊監(jiān)聽類覆蓋接口ActionListener
public void actionPerformed(ActionEvent e) {
Color c = colorChoose.getColor();
color = c;
presTx.setForeground(c);
presTx.repaint();
}
}
}
效果如下:

希望本文所述對(duì)你有所幫助,java仿windows 字體設(shè)置選項(xiàng)卡內(nèi)容就給大家介紹到這里了。希望大家繼續(xù)關(guān)注我們的網(wǎng)站!想要學(xué)習(xí)java可以繼續(xù)關(guān)注本站。
相關(guān)文章
SpringBoot項(xiàng)目中@Test不出現(xiàn)可點(diǎn)擊運(yùn)行的按鈕問題
這篇文章主要介紹了SpringBoot項(xiàng)目中@Test不出現(xiàn)可點(diǎn)擊運(yùn)行的按鈕問題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-01-01
如何將復(fù)雜SQL轉(zhuǎn)換成Java對(duì)象的實(shí)例講解
轉(zhuǎn)換復(fù)雜SQL到Java代碼,我們需要確定數(shù)據(jù)庫連接方式和工具,使用JDBC的API來連接數(shù)據(jù)庫、執(zhí)行SQL語句,復(fù)雜SQL語句可以被拆分為多個(gè)步驟,每個(gè)步驟執(zhí)行一個(gè)特定的操作,通過將SQL語句拆分為多個(gè)步驟,我們可以更好地理解復(fù)雜SQL的邏輯,并且更容易將其轉(zhuǎn)換為Java代碼2024-05-05
Spring Boot集成spring-boot-devtools開發(fā)時(shí)實(shí)現(xiàn)熱部署的方式
這篇文章主要介紹了Spring Boot集成spring-boot-devtools開發(fā)時(shí)實(shí)現(xiàn)熱部署的方式,文中還給大家提到了spring boot 實(shí)現(xiàn)熱部署的方式及集成注意事項(xiàng),感興趣的朋友跟隨腳本之家小編一起學(xué)習(xí)吧2018-05-05
數(shù)據(jù)庫連接池c3p0配置_動(dòng)力節(jié)點(diǎn)Java學(xué)院整理
這篇文章主要為大家詳細(xì)介紹了數(shù)據(jù)庫連接池c3p0配置的相關(guān)資料,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-08-08
Java IO復(fù)用_動(dòng)力節(jié)點(diǎn)Java學(xué)院整理
這篇文章主要介紹了Java IO復(fù)用的相關(guān)知識(shí),非常不錯(cuò),具有參考借鑒價(jià)值,需要的的朋友參考下吧2017-05-05
Spring cloud gateway設(shè)置context-path服務(wù)路由404排查過程
這篇文章主要介紹了Spring cloud gateway設(shè)置context-path服務(wù)路由404排查過程,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2024-08-08
Kafka日志清理實(shí)現(xiàn)詳細(xì)過程講解
這篇文章主要為大家介紹了Kafka日志清理實(shí)現(xiàn)詳細(xì)過程講解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-05-05

