我有个问题。我不知道如何通过单击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
我们希望重写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;
/**
* @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");
正如标题所示,我正在尝试创建一个有几个按钮的程序,每个按钮在单击时都会显示一张图片。但是,我想知道,如果不使用此处所示的graphic类,也不使容器全球化,这是否可能。我尝试了这个,但是,我的程序似乎没有将图像添加到我的面板中。 代码如下:
问题内容: 我有一个代码可以显示员工图表。 数据(名称,电话,照片等)存储在SQLServer中,并通过JSP显示。可以正常显示数据,但图像.jpg(存储在IMAGE = BLOB列中)除外。 顺便说一句,我已经显示了图像(请参见下面的代码),但是我不知道如何将其放在.css定义的区域中(也请参见以下代码),因为图像是通过resultSet被加载到浏览器的整个页面中。 有谁知道我如何“构图”图像?
我试图创建一个小的GUI,它有2个JButtons和2个JPanels,每个JPanels上都有一些绘图动画。默认情况下,它必须显示第一个JPanel,通过单击第二个JButton我想看到第二个JPanel。所以:我创建了JFrame、Panel1和Panel2,在这里我绘制了我的动画,创建了Button1和Button2,并向它们添加了ActionListeners。我还有一个MainPanel
我的JButton ActionListener有问题。我在另一个类中定义了一个doTheCleaning()方法,当调用该方法时,会对GUI进行一系列更改。 然后,在另一个类中,我实例化了包含doTheCleaning()方法的类,并用jbutton的actionperformed()方法编写了ActionListener,如下所示: 我知道如何执行其他操作,比如addActionListene
问题内容: 我在NetBeans中设计了两个JFrame。 当我单击“规则”按钮(即放在JFrame1上)时,它将打开第二个JFrame(但JFrame2在JFrame1的窗口上打开,这是我不想要的)。在第二个JFrame中,有一个“关闭”按钮。但是,当我单击此按钮时,我希望打开JFrame1并且它也能正常工作,但是JFrame2实际上没有关闭,并且JFrame1出现在JFrame2上方。 简而言
如何在相关产品和商店(目录)页面中点击产品缩略图到单个产品?它有一个添加到购物车包装被删除,现在它只是徘徊。 http://www.woodcabinetsite.com/product-category/kitchen-cabinets/ 此外,每个产品都以图库格式显示其缩略图(它们都堆叠在一起)——我想弄清楚如何让这些缩略图以旋转木马格式显示,因为每个产品都有5-10个图库图像。 http:/