java button图片_java – 单击JButton显示图像

该博客介绍了如何在Java Swing中使用JButton显示和隐藏图像。通过创建一个控制器类,实现ActionListener,当点击"Show"按钮时显示ImageIcon图像,点击"Hide"按钮时隐藏图像。示例代码展示了如何加载图像资源并将其与JLabel关联,以实现在JFrame中显示和隐藏图像。
摘要由CSDN通过智能技术生成

同样地说,其他人总是使用JLabel来显示图像.这样,很容易在需要时添加/删除它们,而不是绘画.此外,在你的代码中你覆盖了paint(…),对于Swing,我们更喜欢覆盖相应JComponent的paintComponent(…)方法,如果所述组件有一个.

在这里尝试这个代码,我已经分离了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;

/**

* @author

*

*/

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;

}

/**

* @param args

*/

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;

/**

?* @author

?*

?*/

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;

? ? }

? ? /**

? ? ?* @param args

? ? ?*/

? ? 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”);

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值