import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;
/**
* Created by IntelliJ IDEA.
*
* @Author :
* @create: 2023/03/29 10:15
*/
public class ArtFont extends JFrame implements ActionListener, ItemListener {
JComboBox fontType;//字型樣式下拉框
JComboBox fontSize;//字型大小下拉框
JComboBox windowStyle;//窗體樣式下拉框
JCheckBox boldBx;// 粗體按鈕
JCheckBox italicBx;// 斜體按鈕
JButton colorBtn;// 顏色按鈕;
String[] fontNames;// 字型名稱;
String[] fontSizes;// 字型大小;
JLabel label;// 輸入提示標籤;
JTextField inputText;// 文字輸入框;
JTextArea txtArea;// 文字顯示區;
JPanel northPanel;// 字型設定;
JPanel centerPanel;// 顯示效果區
JPanel southPanel;//樣式設定
Font font;
int boldStyle, italicStyle, underlineStyle;
int fontSizeStyle;
String fontNameStyle;
Color colorStyle = Color.black;// 設定字型的預設顏色為黑色;
String[] style = { "預設顯示效果", "Windows顯示效果", "Unix顯示效果" };
public ArtFont() {
super("字型設定");
// 設定預設字型
boldStyle = 0;
italicStyle = 0;
underlineStyle = 0;
fontSizeStyle = 10;
fontNameStyle = "宋體";
font = new Font(fontNameStyle, boldStyle + italicStyle, fontSizeStyle);
northPanel = getNorthPanel();
centerPanel = getCenterPanel();
southPanel = getSouthPanel();
// 設定容器;
Container container = getContentPane();
container.setLayout(new BorderLayout());
//將northPanel新增到窗體的北部
container.add(northPanel, BorderLayout.NORTH);
//將centerPanel新增到窗體的中部
container.add(centerPanel, BorderLayout.CENTER);
//將southPanel新增到窗體的南部
container.add(southPanel, BorderLayout.SOUTH);
setSize(500, 300);
//將窗體位於螢幕的中央
this.setLocationRelativeTo(null);
setVisible(true);
}
private JPanel getNorthPanel() {
JPanel panel = new JPanel();
label = new JLabel("輸入");
inputText = new JTextField(10);
boldBx = new JCheckBox("粗體");
italicBx = new JCheckBox("斜體");
colorBtn = new JButton("顏色");
panel.add(label);
panel.add(inputText);
panel.add(boldBx);
panel.add(italicBx);
panel.add(colorBtn);
/***
在下面括號中改名字
JTextArea("計xxxx xxxxxxxx xx")
***/
JTextArea textArea= new JTextArea("計xxxx xxxxxxxx xx");
//註冊事件監視器
inputText.addActionListener(this);
colorBtn.addActionListener(this);
boldBx.addItemListener(this);
italicBx.addItemListener(this);
panel.add(textArea);
return panel;
}
private JPanel getCenterPanel() {
JPanel panel = new JPanel();
txtArea = new JTextArea();
panel.setLayout(new BorderLayout());
panel.add(new JScrollPane(txtArea), BorderLayout.CENTER);
return panel;
}
private JPanel getSouthPanel() {
JPanel panel = new JPanel();
//獲得系統預設字型
GraphicsEnvironment ge = GraphicsEnvironment.getLocalGraphicsEnvironment();
fontNames = ge.getAvailableFontFamilyNames();
fontType = new JComboBox(fontNames);
//設定字型大小
fontSizes = new String[63];
for (int i = 0; i < fontSizes.length; i++) {
fontSizes[i] = Integer.toString(i+10);
}
fontSize = new JComboBox(fontSizes);
windowStyle = new JComboBox(style);
panel.add(fontType);
panel.add(fontSize);
panel.add(windowStyle);
//註冊事件監視器
fontType.addItemListener(this);
fontSize.addItemListener(this);
windowStyle.addItemListener(this);
return panel;
}
public static void main(String args[]) {
ArtFont artFont = new ArtFont();
artFont.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
@Override
public void actionPerformed(ActionEvent e) {
if (e.getSource() == inputText) {
txtArea.setText(inputText.getText());
} else if (e.getSource() == colorBtn) {
colorStyle=JColorChooser.showDialog(this,"請選擇一種顏色",colorStyle);
colorBtn.setForeground(colorStyle);
txtArea.setForeground(colorStyle);
}
}
@Override
public void itemStateChanged(ItemEvent e) {
if (e.getSource() == boldBx) {
if (boldBx.isSelected()){
boldStyle=Font.BOLD;
}else {
boldStyle=Font.PLAIN;
}
} else if (e.getSource() == italicBx) {
if (italicBx.isSelected()){
italicStyle=Font.ITALIC;
}else {
italicStyle=Font.PLAIN;
}
} else if (e.getSource() == fontType) {
fontNameStyle=(String) e.getItem();
} else if (e.getSource() == fontSize) {
fontSizeStyle=Integer.parseInt((String) e.getItem());
} else if (e.getSource() == windowStyle) {
String s = (String) e.getItem();
String className = "";
if (s.equals("Windows顯示效果"))
className = "com.sun.java.swing.plaf.windows.WindowsLookAndFeel";
else if (s.equals("Unix顯示效果"))
className = "com.sun.java.swing.plaf.motif.MotifLookAndFeel";
else if (s.equals("預設顯示效果"))
className = UIManager.getCrossPlatformLookAndFeelClassName();
try {
UIManager.setLookAndFeel(className);
SwingUtilities.updateComponentTreeUI(this);
} catch (Exception de) {
System.out.println("Exception happened!");
}
}
font = new Font(fontNameStyle, boldStyle + italicStyle, fontSizeStyle);
txtArea.setFont(font);
}
}