zoukankan      html  css  js  c++  java
  • 第二次作业

    我这周做的这个字体选择框主要使用的组件分别是标签(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();
    	
    }
    

    }
    程序运行效果图:

    我的照片:

  • 相关阅读:
    Spring MVC 拦截器
    spring中MultiActionController的数据绑定
    Hibernate多对多配置
    hibernate实体类配置文件问题(字段使用默认值)
    HibernateTemplate类的使用 (转)
    javascript小笔记(一)
    spring整合hibernate(2)
    Sina AppEngine 的bug
    找工作
    天下武功唯快不破
  • 原文地址:https://www.cnblogs.com/humeiling/p/5288670.html
Copyright © 2011-2022 走看看