第二次作业

本文介绍了一个字体选择器的设计与实现过程,使用Java Swing组件如JLabel、JTextField、JList等构建界面,并展示了如何获取系统字体列表。

我这周做的这个字体选择框主要使用的组件分别是标签(JLabel)、文本框(JTextField)、列表框(JList),中间容器主要使用了面板(JPanel)和滚动面板(JScrollPane)
标签(JLabel):private JLabel nameLbl,styleLbl,sizeLbl;
styleLbl = new JLabel("字形");
文本框(JTextField):private JTextField nameText,styleText,sizeText;
styleText = new JTextField("正常");
列表框(JList):private JList nameList,styleList,sizeList;
styleList =new JList(style);
滚动面板(JScrollPane): private JScrollPane nameSPane,styleSPane,sizeSPane;
styleSPane = new JScrollPane(styleList);
通过查阅API文档和相关资料学习了获取系统所安装的字体名称,以便显示在第一个类表框中,代码如下
GraphicsEnvironment eq = GraphicsEnvironment.getLocalGraphicsEnvironment();
String[] availableFonts = eq.getAvailableFontFamilyNames();
代码如下:
import java.awt.*;

import java.awt.event.*;

import javax.swing.;
import javax.swing.event.
;

public class fontDialog extends JDialog implements ActionListener,ListSelectionListener{
public static final int Cancle = 0;
public static final int OK = 1;
public static final String [] style = {"正常","斜体","粗体","粗斜体"};
public static final String [] size = {"8","9","12","14","22","24","72"};
private Font userFont = null;
private int userSelect = Cancle;
private JFrame parent = null;
private Container con;
private JScrollPane nameSPane,styleSPane,sizeSPane;
private JPanel panel[];
private JLabel nameLbl,styleLbl,sizeLbl;
private JTextField nameText,styleText,sizeText;
private JList nameList,styleList,sizeList;
private JButton OKbtn,cancleBtn;
private fontDialog myFontDialog=null;
private JTextArea text;

public fontDialog(){
    this(null);
}

public fontDialog(JFrame owner) {
    // TODO Auto-generated constructor stub
    super(owner,true);
    parent = owner;
    setTitle("字体");
    con = getContentPane();
    //设置中间容器的布局,从上到下排列
    BoxLayout box  = new BoxLayout(con,BoxLayout.Y_AXIS);
    con.setLayout(box);
    
    panel = new JPanel[4];
    for(int i = 0;i<3;i++){
        panel[i] = new JPanel();
        panel[i].setLayout(new GridLayout(1,3));
        panel[i].setBackground(Color.GREEN);
    }
    panel[3] = new JPanel();
    panel[3].setLayout(new FlowLayout());
    //创建第一行的3个标签放在第一个panel中
    nameLbl = new JLabel("字体");
    styleLbl = new JLabel("字形");
    sizeLbl = new JLabel("大小");
    panel[0].add(nameLbl);
    panel[0].add(styleLbl);
    panel[0].add(sizeLbl);
    //创建第二行的3个文本框放在第二个panel中
    nameText = new JTextField("宋体");
    nameText.setColumns(5);
    //设置只读
    nameText.setEditable(false);
    nameText.setBackground(Color.YELLOW);
    styleText = new JTextField("正常");
    styleText.setColumns(5);
    styleText.setEditable(false);
    styleText.setBackground(Color.YELLOW);
    text = new JTextArea();
    sizeText = new JTextField("12");
    sizeText.setColumns(5);
    sizeText.setEditable(false);
    sizeText.setBackground(Color.YELLOW);
    panel[1].add(nameText);
    panel[1].add(styleText);
    panel[1].add(sizeText);
    
    //获取系统所安装的字体名称,以便显示在第一个类表框中
    GraphicsEnvironment eq = GraphicsEnvironment.getLocalGraphicsEnvironment();
    String[] availableFonts = eq.getAvailableFontFamilyNames();
    nameList =new JList(availableFonts);
    nameList.addListSelectionListener(this);//事件监听器
    nameSPane = new JScrollPane(nameList);
    nameList.setBackground(Color.magenta);
    styleList =new JList(style);
    styleList.addListSelectionListener(this);//事件监听器
    styleSPane = new JScrollPane(styleList);
    styleList.setBackground(Color.magenta);
    
    sizeList =new JList(size);
    sizeList.addListSelectionListener(this);//事件监听器
    sizeSPane = new JScrollPane(sizeList);
    sizeList.setBackground(Color.magenta);
    
    panel[2].add(nameSPane);
    panel[2].add(styleSPane);
    panel[2].add(sizeSPane);
    
    OKbtn = new JButton("确定");
    OKbtn.addActionListener(this);
    cancleBtn = new JButton("取消");
    cancleBtn.addActionListener(this);
    panel[3].add(OKbtn);
    panel[3].add(cancleBtn);
    for(int i=0;i<4;i++){
        con.add(panel[i]);
    }
    showFontDialog();
    //parent.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}

@Override
public void actionPerformed(ActionEvent e) {
    // TODO Auto-generated method stub
    String mse = "";
    int styleIndex = Font.PLAIN,fontSize;
    if(e.getSource() == OKbtn){
        if(styleText.getText().equals("正常"))
            styleIndex = Font.PLAIN;
        
        if(styleText.getText().equals("斜体"))
            styleIndex = Font.ITALIC;
        if(styleText.getText().equals("粗斜体"))
            styleIndex = Font.BOLD;
        
        fontSize = Integer.parseInt(sizeText.getText());
        userFont = new Font(nameText.getText(),styleIndex,fontSize);
        userSelect = OK;
        mse = mse+" "+nameText.getText()+"  "+sizeText.getText()+"  "+styleText.getText();
        setVisible(false);
        JOptionPane.showMessageDialog(parent, "你选择了"+mse);

    }
    else{
        userSelect = Cancle;
        setVisible(false);
    }
}

@Override
public void valueChanged(ListSelectionEvent e) {
    // TODO Auto-generated method stub
    if(e.getSource() ==nameList)
        nameText.setText((String)nameList.getSelectedValue());
    if(e.getSource() ==styleList)
        styleText.setText((String)styleList.getSelectedValue());
    if(e.getSource() ==sizeList)
        sizeText.setText((String)sizeList.getSelectedValue());
    
}
public int showFontDialog(){
    setSize(300,300);
    int x,y;
    if(parent !=null){
        x=parent.getX()+30;
        y=parent.getY()+30;
    }else{
        x = 150;
        y = 100;
    }
    setLocation(new Point(x,y));
    setVisible(true);
    return userSelect;
}
public Font getFont(){
    return userFont;
}
 void doChangeFont(){
    if(myFontDialog ==null)
        myFontDialog = new fontDialog();
    if(((fontDialog) myFontDialog).showFontDialog() == fontDialog.OK)
        text.setFont(myFontDialog.getFont());
    
}


public static void main(String[] args) {
    new fontDialog();
    
}

}
程序运行效果图:
903293-20160317223005162-442534981.png903293-20160317223034521-1737982321.png
903293-20160317223058881-1843422207.png

我的照片:903293-20160317223714599-346717451.jpg

转载于:https://www.cnblogs.com/humeiling/p/5288670.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值