⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 fontdialog.java

📁 类似Windows下的Notepad 并实现其他使用功能
💻 JAVA
字号:
package dazuoye;

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

/**
 * @author wang xiaoling B04041413
 *
 */
@SuppressWarnings("serial")
public class fontDialog extends JDialog implements ActionListener {


	String fontName;  

	int fontStyle;    //主要出口参数

	int fontSize;     //主要出口参数

	int valid;       //主要出口参数

	JLabel sizeLabel, styleLabel, exampleLabel;

	JTextField exampleField;

	JComboBox sizeBox, nameBox;

	ButtonGroup buttonGroup;

	JRadioButton boldRadioButton, italicRadioButton, plainRadioButton;

	JButton defaultButton, okButton, cancelButton;

	JDialog fontDialog;

	JPanel panelNorth, panelSouth, panelEast, panelWest;

	Container container;

	GraphicsEnvironment ge = GraphicsEnvironment.getLocalGraphicsEnvironment();

	public fontDialog() {
		fontDialog = new JDialog();
		container = fontDialog.getContentPane();
		panelNorth = new JPanel();
		panelSouth = new JPanel();
		panelEast = new JPanel();
		panelWest = new JPanel();
		fontName = "新宋体";
		fontStyle = Font.PLAIN;
		fontSize = 20;
		sizeLabel = new JLabel("FontSize:");
		sizeBox = new JComboBox();
		;
		for (int i = 10; i < 26; i++) {
			sizeBox.addItem(String.valueOf(i));
		}
		sizeBox.setSelectedItem("20");
		sizeBox.addActionListener(this);
		nameBox = new JComboBox();
		String strStyle[] = ge.getAvailableFontFamilyNames();
		for (int i = 0; i < strStyle.length; i++) {
			nameBox.addItem(strStyle[i]);
		}
		nameBox.setSelectedItem("新宋体");
		nameBox.addActionListener(this);
		styleLabel = new JLabel("FontName:");
		exampleLabel = new JLabel("预览:");
		exampleField = new JTextField("我的字体", 14);
		exampleField.setEditable(false);
		exampleField.setFont(new Font(fontName, fontStyle, fontSize));
		boldRadioButton = new JRadioButton("BOLD      ");
		boldRadioButton.addActionListener(this);
		italicRadioButton = new JRadioButton("ITALIC     ");
		italicRadioButton.addActionListener(this);
		plainRadioButton = new JRadioButton("PLAIN      ");
		plainRadioButton.addActionListener(this);
		plainRadioButton.setSelected(true);

		buttonGroup = new ButtonGroup();
		buttonGroup.add(boldRadioButton);
		buttonGroup.add(italicRadioButton);
		buttonGroup.add(plainRadioButton);

		defaultButton = new JButton("Default");
		defaultButton.setMnemonic('e');
		defaultButton.addActionListener(this);
		okButton = new JButton("Ok");
		okButton.addActionListener(this);
		cancelButton = new JButton("Cancel");
		cancelButton.addActionListener(this);
		container.setLayout(new BorderLayout());
		panelNorth.add(sizeLabel);
		panelNorth.add(sizeBox);
		panelNorth.add(styleLabel);
		panelNorth.add(nameBox);
		container.add(panelNorth, BorderLayout.NORTH);
		GridLayout gridLayout = new GridLayout(3, 1);
		panelWest.setLayout(gridLayout);
		panelWest.add(boldRadioButton);
		panelWest.add(italicRadioButton);
		panelWest.add(plainRadioButton);
		container.add(panelWest, BorderLayout.WEST);
		panelEast.setLayout(new FlowLayout());
		panelEast.add(exampleLabel);
		panelEast.add(exampleField);
		panelEast.add(defaultButton);
		container.add(panelEast, BorderLayout.EAST);
		panelSouth.add(okButton);
		panelSouth.add(cancelButton);
		container.add(panelSouth, BorderLayout.SOUTH);
		fontDialog.setSize(420, 180);
		fontDialog.setTitle("FontChoose");
		fontDialog.setDefaultCloseOperation(JDialog.DO_NOTHING_ON_CLOSE);
		fontDialog.addWindowListener(new WindowAdapter(){
			public void windowClosing(WindowEvent e){
				fontDialog.dispose();
			}
		});
		fontDialog.setLocationRelativeTo(null);
		fontDialog.setAlwaysOnTop(true);
		fontDialog.setModal(true);
		fontDialog.setVisible(true);

	}

	public void actionPerformed(ActionEvent e) {
		if (e.getSource() == okButton) {
			valid = 1;
			fontDialog.dispose();
		} else if (e.getSource() == cancelButton) {
			valid = 0;
			fontDialog.dispose();
		} else if (e.getSource() == defaultButton) {
			fontName = "新宋体";
			nameBox.setSelectedItem("新宋体");
			fontStyle = Font.PLAIN;
			plainRadioButton.setSelected(true);
			fontSize = 20;
			sizeBox.setSelectedItem("20");
			exampleField.setFont(new Font(fontName, fontStyle, fontSize));
		} else if (e.getSource() == boldRadioButton) {
			fontStyle = Font.BOLD;
			exampleField.setFont(new Font(fontName, fontStyle, fontSize));
		} else if (e.getSource() == italicRadioButton) {
			fontStyle = Font.ITALIC;
			exampleField.setFont(new Font(fontName, fontStyle, fontSize));
		} else if (e.getSource() == plainRadioButton) {
			fontStyle = Font.PLAIN;
			exampleField.setFont(new Font(fontName, fontStyle, fontSize));
		} else if (e.getSource() == sizeBox) {
			fontSize = Integer.parseInt(sizeBox.getSelectedItem().toString());
			exampleField.setFont(new Font(fontName, fontStyle, fontSize));
		} else if (e.getSource() == nameBox) {
			fontName = nameBox.getSelectedItem().toString();
			exampleField.setFont(new Font(fontName, fontStyle, fontSize));
		}
	}
	
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -