Java_swing_图形化界面实例应用

关于传统awt应用>>
import java.awt.BorderLayout;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;

import javax.swing.JButton;
import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.filechooser.FileFilter;

public class OpenFile {

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		
		final JFrame f = new JFrame("测试");
		f.setVisible(true);
		/*
		 * 关于布局有几种常见方法
		 * BorderLayout分为 南 北 东 西 中
		 * FlowLayout好比一段文字,组件为文字,一个个附加
		 * GridLayout
		 * */
		f.setLayout(new BorderLayout());
		f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);//关闭
		
		JPanel panel = new JPanel();
		panel.setLayout(new FlowLayout());
		JButton button = new JButton("打开文本");
		panel.add(button);
		JButton button1 = new JButton("保存文本");
		panel.add(button1);
		
		final JTextArea txt = new JTextArea(30, 40);
		final JLabel label = new JLabel();
		
		f.add(panel, BorderLayout.NORTH);
		f.add(new JScrollPane(txt), BorderLayout.CENTER);
		f.add(label, BorderLayout.SOUTH);
		
		f.setBounds(50, 50, 500, 500);
		button.addActionListener(new ActionListener() {
			
			public void actionPerformed(ActionEvent e) {
				JFileChooser file = new JFileChooser(".");	//文件当期目录
				file.addChoosableFileFilter(new MyFilter("txt"));//过滤器
				int choose = file.showOpenDialog(f);
				if(choose == 0){
					File ff = file.getSelectedFile();
					try {
						BufferedReader bufr = new BufferedReader(new FileReader(ff));
						String temp = null;
						while((temp=bufr.readLine()) != null){
							txt.append(temp+"\r\n");
						}
						
					} catch (FileNotFoundException e1) {
						// TODO Auto-generated catch block
						e1.printStackTrace();
					} catch (IOException e1) {
						// TODO Auto-generated catch block
						e1.printStackTrace();
					}
				}
				
			}
		});
	}
}
class MyFilter extends FileFilter{
	String postfix;	//后缀名
	MyFilter(String postfix) {
		this.postfix = postfix;
	}

	@Override
	public boolean accept(File f) {//这个文件是否列出
		if(f.isDirectory())
			return true;
		String str = f.getName();
		int num = str.lastIndexOf('.');
		if(num >0){
			if(str.substring(num+1, str.length()).equals(postfix)){
				return true;
			}
		}
		return false;
	}

	@Override
	public String getDescription() {//描叙
		
		return "*."+postfix+"文件";
	}
	
}

import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.io.File;
import java.util.ArrayList;

import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;

public class Test{
	public static void main(String[] args){
		
		new MyPanel();
	}
}
class MyPanel extends JPanel{
	private static JFrame f = new JFrame("test");
	private static ArrayList<Shape> al = new ArrayList<Shape>();
	MyPanel(){
		f.setVisible(true);
		f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		f.add(this);
		f.setBounds(50, 50, 500, 500);
		
		addMouseListener(new MouseAdapter() {
			Shape s = new Shape();
			public void mousePressed(MouseEvent e) {
				s.setPonit1(e.getX(), e.getY());
			}
			public void mouseReleased(MouseEvent e) {
				s.setPonit2(e.getX(), e.getY());
				al.add(s);
				repaint();
			}
			
		});
	}
	@Override
	public void paint(Graphics g){
		//g.drawImage(new ImageIcon("/2.jpg").getImage(), 20, 20, 440, 1909, this); //画图片
		//g.setFont(new Font("楷体", 30, 30));	//设置字体
		g.setColor(Color.red);					//设置颜色
		//g.drawString("我爱你", 50, 50);		//画字符串
	
		//g.drawLine(shape.getX1(), shape.getY1(), shape.getX2(), shape.getY2());	//直线
		for(int i=0; i<al.size(); i++){
			Shape shape = al.get(i);
			g.drawOval(shape.getX1(), shape.getY1(), shape.getX2()-shape.getX1(), shape.getY2()-shape.getY1());
			//画椭圆
		}
	}
	
}
class Shape{
	private int x1 = 0;
	private int y1 = 0;
	private int x2 = 0;
	private int y2 = 0;
	private ShapeType type = ShapeType.line;
	public void setPonit1(int x1, int y1){
		this.x1 = x1;
		this.y1 = y1;
	}
	public void setPonit2(int x2, int y2){
		this.x2 = x2;
		this.y2 = y2;
	}
	public int getX1() {
		return x1;
	}
	public int getY1() {
		return y1;
	}
	public int getX2() {
		return x2;
	}
	public int getY2() {
		return y2;
	}
}
enum ShapeType{
	line,oval,polygon,rect,string;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值