博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
高产赛母猪
阅读量:4343 次
发布时间:2019-06-07

本文共 5912 字,大约阅读时间需要 19 分钟。

  其实就是GUI练手。

  图片阅读器,超简单版。

  

  要用Graphics类在面板上绘图,这个面板得继承JPanel,然后重写paintComponent(Graphics g)方法,里边就用g来各种draw各种玩弄。

  比如drawImage(img, x, y, observer)第四个参数指的是画图位置,当然用this就可以了。

  observer前面多几个参数的话还可以指定绘图的范围,目测是直接缩放。

  然后还用了下各种Menu,记得,JMenuItem是最小的,是单个菜单选项,JMenu第二小,是单个菜单,比如文件啊编辑啊关于啊。JMenuBar最大,是整个菜单栏。

  菜单定义好了之后使用JFrame的方法,setMenuBar(JMenuBar menu),就搞定了。

 

  不过因为不知道怎么获取菜单栏的高度,加上Win7的窗口好像本来就有点问题,图片画不全,会有一部分看不到哦草我突然想起来忘了设Resizable,懒得重新打包了。

  于是就用了一个非常蠢的方法,对你没有猜错,现在打进电话我就告诉你我给宽度加了16,高度加了59。这个想不到该怎么解决,之后有缘再改吧。

 

  另外还用了下FileFilter类,这个类比较麻烦,得继承它,然后重写accept和getDescription两个方法,第一个接收文件返回布尔值,第二个返回字符串。下边分开说。

  accept方法作用是限制JFileChooser能接受的文件类型,写法是分别判断,想用的比如.jpg文件就返回true,不想显示的比如.txt文件就返回false,另外一定注意,要判断一下file是不是目录,如果是目录的话也要返回true,否则就看不到目录了。

  getDescription方法返回值是下边文件类型框里的文字,比如“所有文件”什么的,主要是给用户看的啦。

 

  发文前我又测试了一下,发现框架这次又太大了不知道怎么回事,反正把蠢方法的宽高都减了10,这回能看了。

1 package imageViewer;  2   3 import java.awt.Color;  4 import java.awt.event.ActionListener;  5 import java.awt.event.ActionEvent;  6 import java.awt.Graphics;  7 import java.awt.Image;  8   9 import javax.swing.ImageIcon; 10 import javax.swing.JFrame; 11 import javax.swing.JPanel; 12 import javax.swing.JMenuItem; 13 import javax.swing.JMenu; 14 import javax.swing.JMenuBar; 15 import javax.swing.JFileChooser; 16 import javax.swing.filechooser.FileFilter; 17  18 import java.io.File; 19  20 @SuppressWarnings("serial") 21 public class MainFrame extends JFrame{ 22     private MyPanel panel; 23      24     public MainFrame() { 25         //初始化菜单栏。 26         JMenuItem openOption = new JMenuItem("打开"); 27         JMenuItem exitOption = new JMenuItem("退出"); 28          29         openOption.addActionListener(new OpenListener(this)); 30         exitOption.addActionListener(new ExitListener()); 31          32         JMenu menu = new JMenu("文件"); 33         menu.add(openOption); 34         menu.add(exitOption); 35          36         JMenuBar menuBar = new JMenuBar(); 37         menuBar.add(menu); 38          39         //初始化绘图面板。 40         panel = new MyPanel(); 41         panel.setBackground(new Color(66, 204, 255)); 42          43         this.add(panel); 44         this.setJMenuBar(menuBar); 45          46         this.setResizable(false); 47     } 48      49     public MyPanel getPanel() {
return this.panel;} 50 } 51 52 /** 自定义绘图面板。*/ 53 class MyPanel extends JPanel { 54 private static final long serialVersionUID = 287940773057884149L; 55 Image img; 56 57 protected void paintComponent(Graphics g) { 58 g.drawImage(img, 0, 0, this); 59 } 60 61 public void setImage(Image img) {
this.img = img;} 62 } 63 64 /**“打开”菜单项的事件监听器。*/ 65 class OpenListener implements ActionListener { 66 MainFrame parent; 67 68 OpenListener(MainFrame parent) {
this.parent = parent;} 69 70 public void actionPerformed(ActionEvent e) { 71 Image img; 72 if ((img = getImage()) != null) { 73 parent.getPanel().setImage(img); 74 parent.getPanel().repaint(); 75 parent.setSize(img.getWidth(parent)+6, img.getHeight(parent)+49); 76 } else { 77 return; 78 } 79 } 80 81 /**提示并获取文件,然后返回它。*/ 82 private File getFile() { 83 JFileChooser fileChooser = new JFileChooser(); 84 fileChooser.setFileSelectionMode(JFileChooser.FILES_ONLY); 85 fileChooser.setMultiSelectionEnabled(false); 86 fileChooser.setCurrentDirectory(new File(".")); 87 fileChooser.setFileFilter(new MyFilter()); 88 fileChooser.setFileHidingEnabled(true); 89 if (fileChooser.showOpenDialog(parent) == JFileChooser.APPROVE_OPTION) 90 return fileChooser.getSelectedFile(); 91 else 92 return null; 93 } 94 95 /**利用getFile方法来读取文件内容并返回作为内容的图片。为了健壮性本来应该判断一下文件类型的,不过反正自己用就算了。*/ 96 private Image getImage() { 97 File file = getFile(); 98 if (file == null) 99 return null;100 else101 return new ImageIcon(file.getPath()).getImage();102 }103 104 /**文件类型过滤器,外边用不着,就写成内部类。*/105 class MyFilter extends FileFilter {106 @Override107 public boolean accept(File f) {108 return (f.isDirectory() ||109 f.getName().endsWith(".jpg") ||110 f.getName().endsWith(".gif") ||111 f.getName().endsWith(".png"));112 }113 114 @Override115 public String getDescription() {116 return ".jpg|.gif|.png";117 }118 }119 }120 121 /**“退出”菜单项的事件监听器。*/122 class ExitListener implements ActionListener {123 public void actionPerformed(ActionEvent e) {124 System.exit(0);125 }126 }
框架和监听器
1 package imageViewer; 2  3 import javax.swing.JFrame; 4 import javax.swing.UIManager; 5 import javax.swing.UnsupportedLookAndFeelException; 6  7 public class Run { 8     public static void main(String[] args) { 9         //设置窗口风格。10         try {11             UIManager.setLookAndFeel("com.sun.java.swing.plaf.windows.WindowsLookAndFeel");12         } catch (UnsupportedLookAndFeelException ule) {13             ule.printStackTrace();14         } catch (IllegalAccessException iae) {15             iae.printStackTrace();16         } catch (InstantiationException ie) {17             ie.printStackTrace();18         } catch (ClassNotFoundException cnfe) {19             cnfe.printStackTrace();20         }21         22         //新建窗口并设置框架属性。23         MainFrame frame = new MainFrame();24         frame.setTitle("简易图片阅读器");25         frame.setSize(400, 400);26         frame.setLocationRelativeTo(null);27         frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);28         frame.setVisible(true);29     }30 }
运行类

 

  对了,为了提醒某个看得起我真的拿我的代码去用了的人,我的java文件头上有package行,得放进相应的文件夹里,如果要单独拿出class文件来用的话把文件里的package XXXX删掉就行了。

转载于:https://www.cnblogs.com/chihane/p/3501189.html

你可能感兴趣的文章
阶段3 2.Spring_04.Spring的常用注解_2 常用IOC注解按照作用分类
查看>>
阶段3 2.Spring_09.JdbcTemplate的基本使用_5 JdbcTemplate在spring的ioc中使用
查看>>
阶段3 3.SpringMVC·_07.SSM整合案例_02.ssm整合之搭建环境
查看>>
小D课堂 - 零基础入门SpringBoot2.X到实战_第1节零基础快速入门SpringBoot2.0_3、快速创建SpringBoot应用之手工创建web应用...
查看>>
小D课堂 - 零基础入门SpringBoot2.X到实战_第1节零基础快速入门SpringBoot2.0_5、SpringBoot2.x的依赖默认Maven版本...
查看>>
阶段3 3.SpringMVC·_07.SSM整合案例_08.ssm整合之Spring整合MyBatis框架
查看>>
小D课堂 - 零基础入门SpringBoot2.X到实战_第2节 SpringBoot接口Http协议开发实战_9、SpringBoot基础HTTP其他提交方法请求实战...
查看>>
小D课堂 - 零基础入门SpringBoot2.X到实战_第2节 SpringBoot接口Http协议开发实战_12、SpringBoot2.x文件上传实战...
查看>>
小D课堂 - 零基础入门SpringBoot2.X到实战_第4节 Springboot2.0单元测试进阶实战和自定义异常处理_19、SpringBoot个性化启动banner设置debug日志...
查看>>
小D课堂 - 零基础入门SpringBoot2.X到实战_第4节 Springboot2.0单元测试进阶实战和自定义异常处理_20、SpringBoot2.x配置全局异常实战...
查看>>
小D课堂 - 零基础入门SpringBoot2.X到实战_第5节 SpringBoot部署war项目到tomcat9和启动原理讲解_23、SpringBoot2.x启动原理概述...
查看>>
小D课堂 - 零基础入门SpringBoot2.X到实战_第4节 Springboot2.0单元测试进阶实战和自定义异常处理_21、SpringBoot2.x配置全局异常返回自定义页面...
查看>>
小D课堂 - 零基础入门SpringBoot2.X到实战_第8节 数据库操作之整合Mybaties和事务讲解_32..SpringBoot2.x持久化数据方式介绍...
查看>>
小D课堂 - 零基础入门SpringBoot2.X到实战_第8节 数据库操作之整合Mybaties和事务讲解_34、SpringBoot整合Mybatis实操和打印SQL语句...
查看>>
小D课堂 - 零基础入门SpringBoot2.X到实战_第8节 数据库操作之整合Mybaties和事务讲解_35、事务介绍和常见的隔离级别,传播行为...
查看>>
小D课堂 - 零基础入门SpringBoot2.X到实战_第9节 SpringBoot2.x整合Redis实战_40、Redis工具类封装讲解和实战...
查看>>
小D课堂 - 零基础入门SpringBoot2.X到实战_第9节 SpringBoot2.x整合Redis实战_37、分布式缓存Redis介绍...
查看>>
小D课堂 - 零基础入门SpringBoot2.X到实战_第10节 SpringBoot整合定时任务和异步任务处理_42、SpringBoot常用定时任务配置实战...
查看>>
小D课堂 - 零基础入门SpringBoot2.X到实战_第9节 SpringBoot2.x整合Redis实战_39、SpringBoot2.x整合redis实战讲解...
查看>>
小D课堂 - 零基础入门SpringBoot2.X到实战_第14节 高级篇幅之SpringBoot多环境配置_59、SpringBoot多环境配置介绍和项目实战...
查看>>