用菜单实现,打开一个图片文件,并把该图片在窗体中显示出来。
package GUI;
import javax.swing.*;
import javax.swing.filechooser.FileFilter;
import javax.swing.filechooser.FileNameExtensionFilter;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;
import java.io.IOException;
import java.net.URI;
public class MenuTest extends JFrame {
public JPanel jp;
public JMenuBar jMenuBar;
public JMenu menu_File, menu_Edit, menu_View, menu_About;
public JMenuItem menuItem_Open, menuItem_Exit1, menuItem_Exit2,
menuItem_Exit3, menuItem_Exit4, menuItem_New,
menuItem_Cut, menuItem_Copy, menuItem_MyBlog;
public JLabel jLabel_img;
public MenuTest(){
this.setTitle("菜单");
this.setSize(500, 500);
this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
this.setLocationRelativeTo(null);
this.setResizable(false);
jp = new JPanel(new FlowLayout());
jMenuBar = new JMenuBar();
menu_File = new JMenu("文件");
menu_Edit = new JMenu("编辑");
menu_View = new JMenu("视图");
menu_About = new JMenu("关于");
jMenuBar.add(menu_File);
jMenuBar.add(menu_Edit);
jMenuBar.add(menu_View);
jMenuBar.add(menu_About);
//-=-=-=-=-=-=-文件-=-=-=-=-=-=-
menuItem_New = new JMenuItem("新建");
menuItem_Open = new JMenuItem("打开");
menuItem_Exit1 = new JMenuItem("退出");
menu_File.add(menuItem_New);
menu_File.add(menuItem_Open);
menu_File.addSeparator();
menu_File.add(menuItem_Exit1);
//-=-=-=-=-=-=-编辑-=-=-=-=-=-=-
menuItem_Copy = new JMenuItem("复制");
menuItem_Cut = new JMenuItem("裁剪");
menuItem_Exit2 = new JMenuItem("退出");
menu_Edit.add(menuItem_Copy);
menu_Edit.add(menuItem_Cut);
menu_Edit.addSeparator();
menu_Edit.add(menuItem_Exit2);
//-=-=-=-=-=-=-视图-=-=-=-=-=-=-
menuItem_Exit3 = new JMenuItem("退出");
menu_View.add(menuItem_Exit3);
//-=-=-=-=-=-=-关于-=-=-=-=-=-=-
menuItem_MyBlog = new JMenuItem("银月のBlog");
menuItem_Exit4 = new JMenuItem("退出");
menu_About.add(menuItem_MyBlog);
menu_About.addSeparator();
menu_About.add(menuItem_Exit4);
ActL actL = new ActL();
menuItem_Open.addActionListener(actL);
menuItem_Exit1.addActionListener(actL);
menuItem_Exit2.addActionListener(actL);
menuItem_Exit3.addActionListener(actL);
menuItem_Exit4.addActionListener(actL);
menuItem_MyBlog.addActionListener(actL);
jLabel_img = new JLabel("假装我是一张图片");
jp.add(jLabel_img);
this.setJMenuBar(jMenuBar);
this.setContentPane(jp);
this.setVisible(true);
}
class ActL implements ActionListener{
@Override
public void actionPerformed(ActionEvent actionEvent) {
String m = actionEvent.getActionCommand();
if(m.equals("退出")) {
System.exit(0);
} else if(m.equals("打开")) {
int i = fileChooser.showOpenDialog(getContentPane());
//↓判断用户单击的是否为“打开”按钮
if (i == JFileChooser.APPROVE_OPTION) {
File selectedFile = fileChooser.getSelectedFile();
Image img = Toolkit.getDefaultToolkit().getImage(selectedFile.getAbsolutePath());
//↓按尺寸缩放并将Image转换为ImageIcon
ImageIcon imgIco = new ImageIcon(img.getScaledInstance(500,400,img.SCALE_SMOOTH));
jLabel_img.setIcon(imgIco);
jLabel_img.setText(null);
}
} else if(m.equals("银月のBlog")) {
URI uri=java.net.URI.create("https://i.loli.link");
Desktop dp=java.awt.Desktop.getDesktop();
//↓获取系统默认浏览器打开链接
if(dp.isSupported(java.awt.Desktop.Action.BROWSE)){
try {
dp.browse(uri);
} catch (IOException e) {
e.printStackTrace();
}
}
} else {
System.out.println(m);
}
}
JFileChooser fileChooser;
{
fileChooser = new JFileChooser();
FileFilter filter = new FileNameExtensionFilter
("图像文件(JPG/PNG/GIF)", "JPG", "JPEG", "GIF", "PNG");
fileChooser.setFileFilter(filter);
}
}
public static void main (String[] args){
new MenuTest();
}
}