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

📄 fontdialog.java

📁 自己用java做的一个仿windows的记事本 希望各位前辈多加指点
💻 JAVA
字号:
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Container;
import java.awt.Font;
import java.awt.GraphicsEnvironment;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.BorderFactory;
import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JLabel;
import javax.swing.JList;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextField;
import javax.swing.UIManager;
import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener;

public class Fontdialog extends JDialog implements ListSelectionListener,ActionListener{
	
	Container contentPane = this.getContentPane();
	JLabel word = new JLabel("字体(F):");
	JLabel wordx = new JLabel("字形(Y):");
	JLabel bs = new JLabel("大小(s):");
	
	JTextField f1 = new JTextField();
	JTextField f2 = new JTextField();
	JTextField f3 = new JTextField();
	JTextField fsl = new JTextField("魔帅出品");
	
	JList l1 = new JList();
	JList l2 = new JList();
	JList l3 = new JList();
	
	JScrollPane p1 = new JScrollPane(l1);
	JScrollPane p2 = new JScrollPane(l2);
	JScrollPane p3 = new JScrollPane(l3);
	
	JPanel sl = new JPanel();
	
	JButton Enter = new JButton("确定");
	JButton quxiao = new JButton("取消");
	
	Font font1 = new Font("Fixedsys",Font.PLAIN,12);
	Font font2 = new Font("Fixedsys",Font.PLAIN,12);
	
	String selName = f1.getText(), selFontx = f2.getText(), selBs;
	
	Fontdialog(JiShiBen ben, String string, boolean b){
		super(ben,string,b);
		try{
			UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
		}catch(Exception e){}
		init();
		this.setSize(440,325);
		this.setResizable(false);
		this.setLayout(null);
		this.setLocationRelativeTo(null);
	}

	void init(){
		l1.addListSelectionListener(this);
		l2.addListSelectionListener(this);
		l3.addListSelectionListener(this);
		contentPane.add(word);
		contentPane.add(wordx);
		contentPane.add(bs);
		contentPane.add(f1);
		contentPane.add(f2);
		contentPane.add(f3);
		contentPane.add(p1);
		contentPane.add(p2);
		contentPane.add(p3);
		contentPane.add(Enter);
		contentPane.add(quxiao);
		contentPane.add(sl);
		contentPane.setBackground(Color.WHITE);
		
		word.setLocation(10,5);
		word.setSize(50,20);
		
		wordx.setLocation(165,5);
		wordx.setSize(50,20);
		
		bs.setLocation(280,5);
		bs.setSize(50,20);
		
		f1.setLocation(10,25);
		f1.setSize(150,20);
		f1.setText("Fixedsys");
		GraphicsEnvironment g1 = GraphicsEnvironment.getLocalGraphicsEnvironment();
		String [] fontName = g1.getAvailableFontFamilyNames();//获取windows的字体
		l1.setListData(fontName);
		
		f2.setLocation(165,25);
		f2.setSize(110,20);
		f2.setText("常规");
		String []fontx = {"常规","斜体","粗体","粗斜体"};
		l2.setListData(fontx);
		
		f3.setLocation(280,25);
		f3.setSize(50,20);
		f3.setText("12");
		String [] fontbs = {"8","9","10","11","12","14","16","18","20","22","24","26","28","36","48","72"};
		l3.setListData(fontbs);
		
		p1.setHorizontalScrollBar(null);//去掉下面的滚动条
		p1.setLocation(10,45);
		p1.setSize(150,90);
		
		p2.setHorizontalScrollBar(null);
		p2.setLocation(165,45);
		p2.setSize(110,90);
		
		p3.setHorizontalScrollBar(null);
		p3.setLocation(280,45);
		p3.setSize(50,90);
		
		Enter.setLocation(345,25);
		Enter.setSize(70,20);
		quxiao.setLocation(345,50);
		quxiao.setSize(70,20);
		
		sl.setLocation(165,155);
		sl.setSize(165,80);
		sl.setLayout(new BorderLayout());
		sl.add(fsl,BorderLayout.CENTER);
		sl.setBorder(BorderFactory.createTitledBorder("示例"));
		fsl.setSize(100,20);
		fsl.setHorizontalAlignment(JTextField.CENTER);
		
		Enter.addActionListener(this);
		quxiao.addActionListener(this);
	}

	public void valueChanged(ListSelectionEvent e) {
		if(e.getSource() == l1){
			selName = (String)l1.getSelectedValue();
			f1.setText(selName);
			this.My_Font();
		}else if(e.getSource() == l2){		
			selFontx = (String)l2.getSelectedValue();
			f2.setText(selFontx);
			this.My_Font();
		}else if(e.getSource() == l3){		
			selBs = (String)l3.getSelectedValue();
			f3.setText(selBs);
			this.My_Font();
		}
		
		fsl.setFont(font1);
	}
	public void My_Font(){
		if(selFontx == "常规"){
			font1 = new Font(selName,Font.PLAIN,Integer.parseInt(f3.getText()));
		}else if(selFontx=="斜体"){
			font1 = new Font(selName,Font.ITALIC,Integer.parseInt(f3.getText()));
		}else if(selFontx=="粗体"){
			font1 = new Font(selName,Font.BOLD,Integer.parseInt(f3.getText()));
		}else if(selFontx=="粗斜体"){
			font1 = new Font(selName,Font.ITALIC+Font.BOLD,Integer.parseInt(f3.getText()));
		}
	}

	public void actionPerformed(ActionEvent e) {
		if(e.getSource()=="确定"){
			font2 = font1;
			this.setDefaultCloseOperation(HIDE_ON_CLOSE);
		}else{
			this.setDefaultCloseOperation(HIDE_ON_CLOSE);
		}
	}
}

⌨️ 快捷键说明

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