[英]Show image by click JButton
我有個問題。 我不知道如何通過單擊JButton顯示圖像。
我有一個可以顯示和隱藏圖像的類:
/**
*
*/
package com.samples;
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.JFrame;
import javax.swing.JPanel;
import javax.swing.WindowConstants;
/**
* @author
*
*/
public class New2 extends JFrame implements ActionListener {
private static String SHOW_ACTION = "show";
private static String HIDE_ACTION = "hide";
private Image image = null;
private boolean showImage = false;
public New2(String filename) {
setTitle("MyWindow");
setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
setSize(800, 600);
this.image = new ImageIcon("..//src/img/Ster.png").getImage();
Container container = getContentPane();
container.setLayout(new BorderLayout());
container.add(createControls(), BorderLayout.SOUTH);
}
private JPanel createControls() {
JButton showButton = new JButton("Show");
showButton.addActionListener(this);
showButton.setActionCommand(SHOW_ACTION);
JButton hideButton = new JButton("Hide");
hideButton.addActionListener(this);
hideButton.setActionCommand(HIDE_ACTION);
JPanel panel = new JPanel();
panel.setLayout(new FlowLayout(FlowLayout.CENTER));
panel.add(showButton);
panel.add(hideButton);
return panel;
}
@Override
public void paint(Graphics g) {
super.paint(g);
if (showImage) {
g.drawImage(image, 100, 200, image.getWidth(null), image.getHeight(null), null);
}
}
@Override
public void actionPerformed(ActionEvent event) {
String actionCommand = event.getActionCommand();
if (SHOW_ACTION.equals(actionCommand)) {
showImage = true;
} else if (HIDE_ACTION.equals(actionCommand)) {
showImage = false;
}
repaint();
}
/**
* @param args
*/
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
@Override
public void run() {
New2 frame = new New2("resources/image.jpg");
frame.setVisible(true);
}
});
}
}
我正在使用MVC,所以我想在我的控制器映射中使用JButton的代碼,但是我不知道該怎么做。
package View;
import java.awt.Color;
import java.awt.Container;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.Image;
import java.awt.event.ActionListener;
import javax.swing.BorderFactory;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JPanel;
import Controller.HomeController;
import Controller.KeeperController;
public class Selectie extends JFrame{
private JLabel label, label1, label2;
private JButton keeper;
private JPanel panel;
private Container window = getContentPane();
private KeeperController controller;
public Selectie()
{
initGUI();
}
public void initGUI()
{
setLayout(null);
setTitle();
setSize(800,600);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
label = new JLabel();
label.setBounds(0, 0, 266, 800);
label.setBackground(Color.RED);
label.setOpaque(true);
window.add(label);
label1 = new JLabel();
label1.setBounds(266, 0, 266, 800);
label1.setBackground(Color.BLACK);
label1.setOpaque(true);
window.add(label1);
label2 = new JLabel();
label2.setBounds(532, 0, 266, 800);
label2.setBackground(Color.RED);
label2.setOpaque(true);
window.add(label2);
keeper = new JButton("1. "+""+" Kenneth Vermeer");
keeper.setBounds(60, 500, 200, 25);
keeper.setFocusable(false);
keeper.setBorderPainted(false);
keeper.setContentAreaFilled(false);
keeper.setFont(new Font("Arial",Font.PLAIN,17));
label.add(keeper);
}
}
單擊按鈕保持器時需要顯示圖像。
就像其他人一樣,總是使用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常量指定:
版本說明:在JDK 1.4版之前,各個區域的首選名稱有所不同,從指南針的點(例如,頂部區域為BorderLayout.NORTH)到我們示例中使用的常量的更高級版本。 我們的示例使用的常量是首選的,因為它們是標准的,並使程序能夠適應具有不同方向的語言。
目錄結構:
Your Project
| |
classes src
| |
img *.class(or package Folder)
現在使用getClass().getResource("/img/star.png");
1)使用JLabel#setIcon()代替將圖像繪制為JFrame的背景
2)將方法創建到class Selectie
private void setIconToLabel (Icon icon){
myDesiredLabel.setIcon(icon);
}
3)不要為使用CardLayout的另一個圖像創建新的JFrame
您可以簡單地添加一個JLable在其上顯示圖像。 之后,您可以根據條件設置Visible JLabel。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.