JAVA Swing

之前就想做这个,但是当初不知道如何做,而且百度了一堆发现没有实现的方法,

现在看了看,准备百度的,但是没有度出来,于是自己做了,

界面有的是多余的,因为做这个是为了其他的项目,所以当时设计的时候界面有的没有用.

代码如下

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.util.*;
import java.io.*;
import java.awt.image.*;
import javax.imageio.ImageIO;
public class MainDemo implements ActionListener {
	JFrame frame=null;
	JLabel label=new JLabel();//用于显示图片
	JLabel labelRGB=new JLabel("");
	BufferedImage bi=null;//用于存储临时打开的图片流
	public void init(){
		JButton  openFile=new JButton("打开文件");
		openFile.addActionListener(this);
		

		frame=new JFrame();
		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		frame.setSize(1100,1000);
		frame.setLayout(new BorderLayout());
		//操作数据按钮
		JPanel OpearJP=new JPanel(new FlowLayout());
		frame.add(OpearJP,BorderLayout.NORTH);
		OpearJP.setBorder(BorderFactory.createTitledBorder("操作"));
		OpearJP.setPreferredSize(new Dimension(1100,300));
		JButton jb1=new JButton("按钮1");
		OpearJP.add(jb1);
		//用于显示的面板
		JPanel ShowJP=new JPanel(new BorderLayout());
		ShowJP.setBorder(BorderFactory.createTitledBorder("显示"));
		ShowJP.setPreferredSize(new Dimension(1100,700));
		frame.add(ShowJP,BorderLayout.CENTER);
		//用于显示操作rgb的数据
		JPanel NorthJP=new JPanel(new FlowLayout(FlowLayout.LEFT));
		ShowJP.add(NorthJP,BorderLayout.NORTH);
		NorthJP.setPreferredSize(new Dimension(1100,100));
		NorthJP.add(openFile);//打开一个文件
		NorthJP.add(new JLabel("当前的RGB值为:"));
		NorthJP.add(labelRGB);
		//用于获取RGB图片
		JPanel WestJP=new JPanel(new BorderLayout());
		ShowJP.add(WestJP,BorderLayout.WEST);
		WestJP.setPreferredSize(new Dimension(550,550));
		WestJP.add(label,BorderLayout.CENTER);
		//获取指定位置的RGB值
		label.addMouseListener(new MouseListener(){
			public void mousePressed(MouseEvent e){
				if(e.getButton()==e.BUTTON1){//判断是否为左击
					//获取RGB值
					int[] rgb=getRGB(bi,e.getX(),e.getY());
					labelRGB.setText("当前位置X="+e.getX()+"Y="+e.getY()+"对应的RGB为:"+rgb[0]+"."+rgb[1]+"."+rgb[2]);
				}
			}
			public void mouseClicked(MouseEvent e){
			}
			public void mouseReleased(MouseEvent e){
			}
			public void mouseEntered(MouseEvent e){
			}
			public void mouseExited(MouseEvent e){
			}
		});
		//和显示处理后的图片
		JPanel EastJP=new JPanel(new BorderLayout());
		ShowJP.add(EastJP,BorderLayout.EAST);
		EastJP.setPreferredSize(new Dimension(550,550));
	
		frame.setVisible(true);
	}
	//实现按钮监听事件
	public void actionPerformed(ActionEvent e){
		String ButtonName=e.getActionCommand();
		if(ButtonName.equals("打开文件")){
			System.out.println("打开文件:");
			openFile();//打开文件
		}

	}
	//打开文件夹操作
	public void openFile(){
		JFileChooser chooser=new JFileChooser();
		chooser.showOpenDialog(frame);//在某个容器上打开一个文件选择器
		File f = chooser.getSelectedFile();
		//String FilePath=f.getAbsolutePath();
		//文件是否存在或者是否选择
		if(f == null) {
			return;
		}
		try {
			bi = ImageIO.read(f);
			/*获取文件是否为图片,如果能够正常的获取到一张图片的宽高属性,
			那肯定这是一张图片,因为非图片文件是获取不到它的宽高属性的*/
			if(bi == null || bi.getHeight() <=0 || bi.getWidth() <=0){
				label.setText("您选择的不是一张图片,请从新选择!");
				return;
			} else {
				String path = f.getPath();
				System.out.println(path);
				ImageIcon image = new ImageIcon(path);
				label.setIcon(image);			//设置JLabel的显示图片
				//frame.pack();//使此窗口的大小适合其子组件的首选大小和布局。 
				//frame.validate();	//验证此容器及其所有子组件。 
			}
		} catch (IOException e) {
			//e.printStackTrace();
			return;
		}
	}
	//获取当前位置的RGB操作
	public int[] getRGB(BufferedImage bi,int x,int y){
		int[] rgb=new int[3];
		int pixel=bi.getRGB(x,y);
		rgb[0]=(pixel&0xff0000)>>16;
		rgb[1]=(pixel&0xff00)>>8;
		rgb[2]=(pixel&0xff);
		return rgb;
	}
	public static void main(String[] args){
		(new MainDemo()).init();
	}
}

效果如图

Logo

CSDN联合极客时间,共同打造面向开发者的精品内容学习社区,助力成长!

更多推荐