栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 面试经验 > 面试问答

通过单击JButton显示图像

面试问答 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

通过单击JButton显示图像

就像别人说的一样,总是

JLabel
用来显示图像。这样,很容易在需要时添加/删除它们,而不是绘画。此外,在您的代码中您正在重写
paint(...)
,因为如果所讨论的组件具有一个,
Swing
我们希望重写
paintComponent(...)
各自的方法
JComponent

在这里尝试这段代码,我已经分离了Controller部分,您可能对如何做事有所了解:

import java.awt.BorderLayout;import java.awt.Container;import java.awt.EventQueue;import java.awt.FlowLayout;import java.awt.Graphics;import java.awt.Image;import java.awt.event.ActionEvent;import java.awt.event.ActionListener;import javax.swing.Icon;import javax.swing.JButton;import javax.swing.JLabel;import javax.swing.Jframe;import javax.swing.JPanel;import javax.swing.SwingUtilities;import javax.swing.UIManager;import javax.swing.WindowConstants;public class New2 extends Jframe{    private static String SHOW_ACTION = "show";    private static String HIDE_ACTION = "hide";    public New2(String filename)     {        setTitle("MyWindow");        setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);        setSize(800, 600);        Container container = getContentPane();        container.setLayout(new BorderLayout());        container.add(createControls(), BorderLayout.CENTER);    }    private JPanel createControls()     {        JButton showButton = new JButton("Show");     showButton.setActionCommand(SHOW_ACTION);        JButton hideButton = new JButton("Hide");     hideButton.setActionCommand(HIDE_ACTION);        JLabel imageLabel = new JLabel();        New2Controller n2c = new New2Controller(showButton      , hideButton, imageLabel);        showButton.addActionListener(n2c);       hideButton.addActionListener(n2c);        JPanel panel = new JPanel();        panel.setLayout(new FlowLayout(FlowLayout.CENTER));        panel.add(imageLabel);        panel.add(showButton);        panel.add(hideButton);        return panel;    }        public static void main(String[] args)     {        EventQueue.invokeLater(new Runnable()         { @Override public void run()  {     New2 frame = new New2("/img/image.jpg");     frame.setVisible(true); }        });    }}class New2Controller implements ActionListener{    private JButton showButton;    private JButton hideButton;    private JLabel imageLabel;    private static String SHOW_ACTION = "show";    private static String HIDE_ACTION = "hide";    private Icon infoIcon = UIManager.getIcon("OptionPane.informationIcon");    public New2Controller(JButton show, JButton hide, JLabel label)    {        showButton = show;        hideButton = hide;        imageLabel = label;    }    public void actionPerformed(ActionEvent event)    {        String actionCommand = event.getActionCommand();        if (SHOW_ACTION.equals(actionCommand))         { SwingUtilities.invokeLater(new Runnable() {     public void run()     {          imageLabel.setIcon(infoIcon);     } });        }         else if (HIDE_ACTION.equals(actionCommand))         { imageLabel.setIcon(null);        }    }}

该代码代表您如何使用

ImageIO
和阅读
URL

import java.awt.BorderLayout;import java.awt.Container;import java.awt.EventQueue;import java.awt.FlowLayout;import java.awt.Graphics;import java.awt.Image;import java.awt.event.ActionEvent;import java.awt.event.ActionListener;import javax.swing.ImageIcon;import javax.swing.JButton;import javax.swing.JLabel;import javax.swing.Jframe;import javax.swing.JPanel;import javax.swing.SwingUtilities;import javax.swing.WindowConstants;import javax.imageio.ImageIO;public class New2 extends Jframe{    private static String SHOW_ACTION = "show";    private static String HIDE_ACTION = "hide";    public New2(String filename)     {        setTitle("MyWindow");        setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);        setSize(800, 600);        Container container = getContentPane();        container.setLayout(new BorderLayout());        container.add(createControls(), BorderLayout.CENTER);    }    private JPanel createControls()     {        JButton showButton = new JButton("Show");                showButton.setActionCommand(SHOW_ACTION);        JButton hideButton = new JButton("Hide");                hideButton.setActionCommand(HIDE_ACTION);        JLabel imageLabel = new JLabel();        New2Controller n2c = new New2Controller(showButton     , hideButton, imageLabel);        showButton.addActionListener(n2c);       hideButton.addActionListener(n2c);        JPanel panel = new JPanel();        panel.setLayout(new FlowLayout(FlowLayout.CENTER));        panel.add(imageLabel);        panel.add(showButton);        panel.add(hideButton);        return panel;    }        public static void main(String[] args)     {        EventQueue.invokeLater(new Runnable()         { @Override public void run()  {     New2 frame = new New2("/img/image.jpg");       frame.setVisible(true); }        });    }}class New2Controller implements ActionListener{    private JButton showButton;    private JButton hideButton;    private JLabel imageLabel;    private Image image;    private ImageIcon imageIcon;    private static String SHOW_ACTION = "show";    private static String HIDE_ACTION = "hide";    public New2Controller(JButton show, JButton hide, JLabel label)    {        showButton = show;        hideButton = hide;        imageLabel = label;        try        { image = ImageIO.read(getClass().getResource("/img/caIcon.png"));        }        catch(Exception e)        { e.printStackTrace();        }        imageIcon = new ImageIcon(image);    }    public void actionPerformed(ActionEvent event)    {        String actionCommand = event.getActionCommand();        if (SHOW_ACTION.equals(actionCommand))         { SwingUtilities.invokeLater(new Runnable() {     public void run()     {          imageLabel.setIcon(imageIcon );     } });        }         else if (HIDE_ACTION.equals(actionCommand))         { imageLabel.setIcon(null);        }    }}

此外,当您使用

BorderLayout
从未使用
NORTH
EAST
WEST
SOUTH
为BorderLayout的。他们已被替换
PAGE_START
LINE_START
LINE_END
PAGE_END
分别。

BorderLayout对象具有五个区域。这些区域由BorderLayout常量指定:

  • PAGE_START
  • PAGE_END
  • LINESTART
  • LINE_END
  • 中央

版本说明:在JDK
1.4版之前,各个区域的首选名称有所不同,从指南针的点(例如,顶部区域为BorderLayout.NORTH)到我们示例中使用的常量的更高级版本。
我们的示例使用的常量是首选的,因为它们是标准的,并使程序能够适应具有不同方向的语言。

目录结构:

          Your Project          |          |         classes     src         |     |       img  *.class(or package Folder)

现在使用

getClass().getResource("/img/star.png");



转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/497444.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号