I want to use Java to make a txt editing software like in Windows. It involves the font setting tab. I searched online for a long time but couldn't find it. I was angry and wrote one myself. I will post it here to share it. Next time I encounter such a problem, I don't have to write the code myself!
package experiment;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;/** * FontFormat setting dialog*/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 choose; private JRadioButton[] language = new JRadioButton[2]; private ButtonGroup languageg; private String Slanguage[] = { new String("Li Tao"), new String("ABC") }; private static JFrame frame; public Font font, newFont;// Font class private Color color;// Color class Color newColor; private JFileChooser fileChoose = new JFileChooser();// File selection class instance private JDialog colorDlg;// Color dialog private JColorChooser colorChoose = new JColorChooser();// Color selection class instance private GraphicsEnvironment environment; // This class also obtains system fonts; private String[] fontNameSet;// Font 'logical name' set// String array of font' style' set private String[] fontStyleSet = { "General", "tilt", "bold", "tilt bold" }; // Constant array of font' style' set private Integer[] fontCon = { Font.PLAIN, Font.ITALIC, Font.BOLD, Font.BOLD | Font.ITALIC }; // Font 'size' set 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[]) {// Main function FontFormat a = new FontFormat(); a.setVisible(true); } public FontFormat() {// The parameterless constructor super(frame, "Li Tao―Font Setting Window", true); frame = new JFrame(); initGUI(); } public FontFormat(JFrame frame) {// The parameter-containing constructor super(frame, "Li Tao―Font Setting Window", true); this.frame = frame;// There must be a public Font object in the parent window// setAlwaysOnTop(true); initGUI(); } private void initGUI() {// Interface initialization try { getContentPane().setLayout(null); environment = GraphicsEnvironment.getLocalGraphicsEnvironment();// GraphicsEnvironment is an abstract class that cannot be instantiated, and can only use the static method in it to get an instance fontNameSet = environment.getAvailableFontFamilyNames();// Get system font addMenu();// Add menu initFont();// Initialize font// pack(); setSize(380, 337); setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE); setWindowPos();// Center the window screen setResizable(false);// Size immutable} catch (Exception e) { e.printStackTrace(); } } private void initFont() {// Initialize the font// Set the default font format to the font format opposite the parent window font 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;// Save the original font format 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());// Change text styleTx.setText(fontStyleSet[idxStyle]); sizeTx.setText("" + frame.getFont().getSize()); nameLt.setSelectedValue(frame.getFont().getName(), true);// Change list to display styleLt.setSelectedIndex(idxStyle); sizeLt.setSelectedValue("" + frame.getFont().getSize(), true); font = new Font(fontNameSet[0], fontCon[0], 12);// Save the current format newFont = font;// Save the original font format presTx.setFont(font);// Set to the current mode in preview} } private void addMenu() {// Add to menu// 4 lables----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- nameLb = new JLabel(); getContentPane().add(nameLb); nameLb.setText("Font:"); nameLb.setBounds(10, 14, 120, 26); nameLb.setFont(new java.awt.Font("SimSun", 1, 14)); styleLb = new JLabel(); getContentPane().add(styleLb); styleLb.setText("Font:"); styleLb.setBounds(151, 14, 120, 23); styleLb.setFont(new java.awt.Font("SimSun", 1, 14)); sizeLb = new JLabel(); getContentPane().add(sizeLb); sizeLb.setText("Size:"); sizeLb.setBounds(275, 14, 79, 24); sizeLb.setFont(new java.awt.Font("SimSun", 1, 14)); presLb = new JLabel(); getContentPane().add(presLb); presLb.setText("Preview:"); presLb.setBounds(151, 150, 120, 80); presLb.setFont(new java.awt.Font("SimSun", 1, 14)); // 4个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个下拉条--+监听----------------------------------------------------------------------------- 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); } }); }// ------------------------------------------------------------------------------------- // 中英选项(--------------------------------------------------------------------------------- languageg = new ButtonGroup(); language[0] = new JRadioButton("中"); getContentPane().add(language[0]); language[0].setSelected(false);// Initialize display 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("English"); 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 buttons + listen -------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- { approveActionPerformed(evt); } }); // Cancel button cancel = new JButton(); getContentPane().add(cancel); cancel.setText("Cancel"); 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); } }); // Color selection button choose = new JButton(); getContentPane().add(chose); choose.setText("color"); choose.setBounds(287, 265, 67, 20); choose.setFont(new java.awt.Font("KaiTi_GB2312", 1, 12)); choose.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent evt) { choseActionPerformed(evt); } });// --------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- (screenSize.height - frameSize.height) / 2); } private void nameLtMouseClicked(MouseEvent evt) {// Mouse click event nameTx.setText(nameLt.getSelectedValue().toString()); font = new Font(nameTx.getText(), font.getStyle(), font.getSize()); presTx.setFont(font); } private void styleLtMouseClicked(MouseEvent evt) {// Mouse click event 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) {// mouse click event sizeTx.setText(sizeLt.getSelectedValue().toString()); font = new Font(font.getName(), font.getStyle(), Integer.parseInt(sizeTx.getText())); presTx.setFont(font); } private void approveActionPerformed(ActionEvent evt) {// Determine the trigger event of the button 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 object of the parent window newFont = font;// Update the original save format newColor = color;// Update the color this.dispose(); } private void cancelActionPerformed(ActionEvent evt) {// Cancel button trigger event this.dispose(); } private void chooseActionPerformed(ActionEvent evt) {// Color selection trigger event 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 {// Rewrite the color button click the listening class overlay interface ActionListener public void actionPerformed(ActionEvent e) { Color c = colorChoose.getColor(); color = c; presTx.setForeground(c); presTx.repaint(); } }} The effects are as follows:
I hope this article will be helpful to you. This is all for you to introduce the contents of Java imitation windows font settings tab. I hope everyone will continue to follow our website! If you want to learn Java, you can continue to follow this website.