Lesson_19_HomeWork_一个界面

作业内容:

1. 编写以下界面,点击”确定“按钮的时候退出程序!

Lesson_19_HomeWork_一个界面_第1张图片

代码如下:

package Lesson_19;

import java.awt.Font;
import java.awt.Insets;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JList;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.JTextField;
import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener;

public class Home {
	public static void main(String[] args) {
		MyFrame frame=new MyFrame();
		frame.setLocationRelativeTo(null);
		frame.setUndecorated(false);
		frame.setResizable(false);
		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		frame.setVisible(true);
	}
}

class MyFrame extends JFrame{
	public static final int WIDTH=500;
	public static final int HEIGHT=350;
	private JButton btnadd;//加号按钮
	private JButton btnplus;//减号按钮
	private JButton btnarr1;//向上的箭头
	private JButton btnarr2;//向下的箭头
	private JButton btnarr3;//向左的箭头
	private JButton btnarr4;//向右的箭头
	private JLabel lb1;//字符标签
	private JLabel lb2;//
	private JLabel lb3;//
	private JTextArea text;//文本域:选择的字体
	private JButton btnOk;//确定按钮
	private JButton btnNo;//取消按钮
	private JButton btnHelp;//帮助按钮
	private JList fontList1;//字体列表1
	private JList fontList2;//字体列表2
	private JTextField txt;//文本框
	public MyFrame() {
		this.setTitle("编辑字体列表");
		this.setSize(WIDTH,HEIGHT);
		this.setLayout(null);//设置空布局
		
		btnadd=new JButton("+");
		btnadd.setFocusPainted(false);//设置点击按钮后不出现焦点边框
		btnadd.setToolTipText("点击添加字体");
		btnadd.setFont(new Font(null, 0, 24));//将加号调大
		btnadd.setMargin(new Insets(0, 0, 0, 0));//设置文字与按钮边缘的距离为0
		btnadd.setBounds(15, 20, 20, 20);
		this.add(btnadd);
		
		btnplus=new JButton("-");
		btnplus.setFocusPainted(false);
		btnplus.setFont(new Font(null, 0, 24));
		btnplus.setMargin(new Insets(0, 0, 0, 0));//设置文字与按钮边缘的距离为0,否则按钮太小显示不出来
		btnplus.setBounds(45, 20, 20, 20);
		this.add(btnplus);
		
		btnarr1=new JButton("▲");
		btnarr1.setFocusPainted(false);
		btnarr1.setMargin(new Insets(0, 0, 0, 0));
		btnarr1.setBounds(315, 20, 20, 20);
		this.add(btnarr1);
		
		btnarr2=new JButton("▼");
		btnarr2.setFocusPainted(false);
		btnarr2.setMargin(new Insets(0, 0, 0, 0));
		btnarr2.setBounds(337, 20, 20, 20);
		this.add(btnarr2);
		
		btnarr3=new JButton("<<");
		btnarr3.addActionListener(new actionListener1());
		btnarr3.setFocusPainted(false);
		btnarr3.setMargin(new Insets(0, 0, 0, 0));
		btnarr3.setBounds(180, 220, 20, 20);
		this.add(btnarr3);
		
		btnarr4=new JButton(">>");
		btnarr4.addActionListener(new actionListener2());
		btnarr4.setFocusPainted(false);
		btnarr4.setMargin(new Insets(0, 0, 0, 0));
		btnarr4.setBounds(180, 242, 20, 20);
		this.add(btnarr4);
		
		//字符串数组 用于添加到列表中
		String [] font1={"Georgia,Times New Roman,Times,Serif","Verdana,Arial,Helvetca,sans-serif",
		    "Geneva,Arial,Helvetica,sans-serif","黑体","黄金海报小豆字体","大江南北字体","Gavin自创字体"}; 
		fontList1=new JList(font1);//实例化一个列表框,并将font1数组添加进去
		JScrollPane fontScrollPane1=new JScrollPane(fontList1);//实例化一个滚动面板,并将列表框加进去
		fontScrollPane1.setBounds(10, 45, 350, 100);//设置滚动面板的大小及位置
		this.add(fontScrollPane1);
		
		btnOk=new JButton("确定");
		btnOk.addActionListener(new actionListener3());
		btnOk.setFocusPainted(false);
		btnOk.setBounds(390, 10, 80, 30);
		this.add(btnOk);
		
		btnNo=new JButton("取消");
		btnNo.setFocusPainted(false);
		btnNo.setBounds(390, 50, 80, 30);
		this.add(btnNo);
		
		lb1=new JLabel("字体列表:");
		lb1.setBounds(160, 18, 80, 30);
		this.add(lb1);
		
		text=new JTextArea();
		text.setEditable(false);//设置不可编辑
		text.setBounds(10, 190, 160, 115);
		this.add(text);
		
		lb2=new JLabel("选择的字体:");
		lb2.setBounds(20, 160, 100, 30);
		this.add(lb2);
		
		String[] font2={"Arial Unicode MS","Fixedsys","IpaPanNew","System"
							,"Terminal","Marier","Gavin"};//字体列表
		fontList2=new JList(font2);//实例化一个列表框,将字体列表添加进去
		fontList2.addListSelectionListener(new fontListListener());//为每次选择发生更改时要通知的列表添加侦听器
		JScrollPane fontScrollPane2=new JScrollPane(fontList2);
		fontScrollPane2.setBounds(210, 190, 150, 80);
		this.add(fontScrollPane2);
		
		txt=new JTextField();			//实例化一个文本框
		txt.setEditable(false);
		txt.setBounds(210, 275, 150, 30);
		this.add(txt);
		
		lb3=new JLabel("可用字体:");
		lb3.setBounds(220, 160, 100, 30);
		this.add(lb3);
		
		btnHelp=new JButton("帮助");
		btnHelp.setFocusPainted(false);
		btnHelp.setToolTipText("点击获得帮助信息");//设置按钮悬浮提示
		btnHelp.setBounds(390, 270, 80, 30);
		this.add(btnHelp);
	}
	
	class fontListListener implements ListSelectionListener{	//内部类

		public void valueChanged(ListSelectionEvent e) {	//列表监听
			Object selectedValue=fontList2.getSelectedValue();
			txt.setText(selectedValue.toString());
		}
	}
	class actionListener1 implements ActionListener{	//内部类 

		public void actionPerformed(ActionEvent arg0) {	//按钮监听
			text.append(txt.getText()+"\n");
		}
	}
	class actionListener2 implements ActionListener{
		public void actionPerformed(ActionEvent e) {
			text.setText(null);
		}
	}
	
	class actionListener3 implements ActionListener{

		public void actionPerformed(ActionEvent e) {
			System.exit(0);
		}
		
	}
}

执行图片:

Lesson_19_HomeWork_一个界面_第2张图片
 

你可能感兴趣的:(Lesson_19_HomeWork_一个界面)