设置字体


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);
}
}
}
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值