设置字体

import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.BoxLayout;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JList;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextField;
import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener;
public class World extends JFrame implements ListSelectionListener{
	JLabel JL1 = new JLabel("字体:");
	JLabel JL2 = new JLabel("字型:");
	JLabel JL3 = new JLabel("大小:");
	JLabel JL4 = new JLabel("示例");
	JTextField Jta1,Jta2,Jta3;
	JList front,word,size;
	String []wordJta = {"常规","粗体","斜体","粗斜体"};
	String []sizeJta = {"8","9","10","11","12","13","14","15","20","30","40","50"};
	JPanel jPanel1,jPanel2,jPanel3,jPanel4,jPanel5;
	JScrollPane Jsp1,Jsp2,Jsp3;
	JButton Ok,cancel;
	
	public void luanchJFrame() {
		Jta1 = new JTextField();
		Jta1.setEditable(false);
		Jta2 = new JTextField();
		Jta2.setEditable(false);
		Jta3 = new JTextField();
		Jta3.setEditable(false);
		GraphicsEnvironment ge = GraphicsEnvironment.getLocalGraphicsEnvironment();
		String []locale = ge.getAvailableFontFamilyNames();
		front = new JList(locale);
		front.addListSelectionListener(this);
		word = new JList(wordJta);
		word.addListSelectionListener(this);
		size = new JList(sizeJta);
		size.addListSelectionListener(this);
		jPanel1 = new JPanel(new GridLayout(1,3));
		jPanel1.add(JL1);
		jPanel1.add(JL2);
		jPanel1.add(JL3);
		jPanel2 = new JPanel(new GridLayout(1,3));
		jPanel2.add(Jta1);
		jPanel2.add(Jta2);
		jPanel2.add(Jta3);
		jPanel3 = new JPanel(new GridLayout(1,3));
		Jsp1 = new JScrollPane(front);
		jPanel3.add(Jsp1);
		Jsp2 = new JScrollPane(word);
		jPanel3.add(Jsp2);
		Jsp3 = new JScrollPane(size);
		jPanel3.add(Jsp3);
		jPanel4 = new JPanel();
	    BoxLayout box = new BoxLayout(jPanel4,BoxLayout.Y_AXIS);
	    jPanel4.setLayout(box);
	    jPanel4.add(jPanel1);
	    jPanel4.add(jPanel2);
	    jPanel4.add(jPanel3);
	    jPanel5 = new JPanel();
	    Ok = new JButton("确定");
	    Ok.addActionListener(new BListener());
	    cancel = new JButton("取消");
	    cancel.addActionListener(new BListener());
	    jPanel5.add(Ok);
	    jPanel5.add(cancel);
	    this.getContentPane().add(jPanel5,BorderLayout.SOUTH);
	    this.getContentPane().add(jPanel4,BorderLayout.CENTER);
	    this.getContentPane().add(JL4,BorderLayout.EAST);
		this.setLocation(300,300);
		this.setSize(300,300);
		this.setVisible(true);
		this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		
		
	}
	
	public static void main(String[] args) {
		new World().luanchJFrame();

	}

	@Override
	public void valueChanged(ListSelectionEvent e) {
		if(e.getSource().equals(front))
			Jta1.setText((String)front.getSelectedValue());
		if(e.getSource().equals(word))
			Jta2.setText((String)word.getSelectedValue());
		if(e.getSource().equals(size))
			Jta3.setText((String)size.getSelectedValue());
	}
	class BListener implements ActionListener {
		public void actionPerformed(ActionEvent e) {
			if(e.getSource().equals(cancel))
				System.exit(0);
			if(e.getSource().equals(Ok)){
				String str1 = Jta1.getText();
				String str2 = Jta2.getText();
				String str3 = Jta3.getText();
				int intsize = Integer.valueOf(str3);
				Font fontword;
				int i = Font.PLAIN;
				if(str2.equals("常规"))
					i = Font.PLAIN;
				if(str2.equals("粗体"))
					i = Font.BOLD;
				if(str2.equals("斜体"))
					i = Font.ITALIC;
				if(str2.equals("粗斜体"))
					i = Font.BOLD|Font.ITALIC;
				fontword = new Font(str1,i,intsize);
				JL4.setFont(fontword);
			} 
		}
	}
}

你可能感兴趣的:(swing)