Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/308.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 单击JButton以显示图像_Java_Swing_Jbutton_Actionlistener - Fatal编程技术网

Java 单击JButton以显示图像

Java 单击JButton以显示图像,java,swing,jbutton,actionlistener,Java,Swing,Jbutton,Actionlistener,正如标题所示,我正在尝试创建一个有几个按钮的程序,每个按钮在单击时都会显示一张图片。 但是,我想知道,如果不使用如图所示的graphic类,也不使容器全球化,这是否是可能的。 但是,我的程序似乎没有将图像添加到我的面板中 代码如下: import javax.swing.*; import java.awt.*; import java.awt.event.*; public class PhotoAlbum extends JFrame implements ActionListener{

正如标题所示,我正在尝试创建一个有几个按钮的程序,每个按钮在单击时都会显示一张图片。 但是,我想知道,如果不使用如图所示的graphic类,也不使容器全球化,这是否是可能的。 但是,我的程序似乎没有将图像添加到我的面板中

代码如下:

import javax.swing.*;

import java.awt.*;
import java.awt.event.*;

public class PhotoAlbum extends JFrame implements ActionListener{

private JPanel imagePanel;
private JPanel labelPanel;
public PhotoAlbum(){
    super();
    Container contentPane = getContentPane();
    setSize(1800, 1000);
    setDefaultCloseOperation(EXIT_ON_CLOSE);

    setTitle("Button Demo"); //Theme here
    contentPane.setBackground(Color.blue);
    contentPane.setLayout(new BorderLayout());

    createButtons(contentPane);
    instruction(contentPane);
    createImageLabel(contentPane);
}

public void instruction(Container contentPane){
    JPanel instruction = new JPanel();
    instruction.setLayout(new FlowLayout());
    instruction.setBackground(Color.yellow);
    Font fontType1 = new Font("Comic Sans MS", Font.BOLD, 40);
    JLabel instruction1 = new JLabel("Click on the button to view a"
            + " photo.");
    instruction1.setForeground(Color.BLUE);
    instruction1.setFont(fontType1);
    instruction.add(instruction1);
    contentPane.add(instruction, BorderLayout.SOUTH);
}

public void createButtons(Container contentPane){
    labelPanel = new JPanel();
    labelPanel.setBackground(Color.pink);
    labelPanel.setLayout(new GridLayout(9,1));

    String[] imageLabel = new String[9];
    imageLabel[0] = "Image 1";
    imageLabel[1] = "Image 2";
    imageLabel[2] = "Image 3";
    imageLabel[3] = "Image 4";
    imageLabel[4] = "Image 5";
    imageLabel[5] = "Image 6";
    imageLabel[6] = "Image 7";
    imageLabel[7] = "Image 8";
    imageLabel[8] = "Exit";

    Color[] color = new Color[9];
    color[0] = Color.cyan;
    color[1] = new Color(242, 121, 234);
    color[2] = Color.red;
    color[3] = Color.green;
    color[4] = Color.blue;
    color[5] = new Color(1, 255, 248);
    color[6] = Color.magenta;
    color[7] = new Color(205, 255, 1);
    color[8] = new Color(205, 255, 1);

    Font fontType = new Font("Times New Roman", Font.BOLD, 30);


    JButton[] button = new JButton[9];
    for (int i=0; i<button.length; i++)
    {
        button[i] = new JButton(imageLabel[i]);
        button[i].addActionListener(this);
        button[i].setBackground(color[i]);
        button[i].setFont(fontType);
        labelPanel.add(button[i]);
    }

    contentPane.add(labelPanel, BorderLayout.WEST);
}

public void createImageLabel(Container contentPane){
    imagePanel = new JPanel();
    imagePanel.setBackground(Color.magenta);
    contentPane.add(imagePanel, BorderLayout.CENTER);
}

public void actionPerformed(ActionEvent event){
    String actionCommand = event.getActionCommand();
    if(actionCommand.equals("Image 1")) {
        JLabel addImage = new JLabel();
        ImageIcon image = new ImageIcon("picture1.jpg");
        addImage.setIcon(image);
        imagePanel.add(addImage);
        imagePanel.setBackground(Color.yellow);
    }

    else if(actionCommand.equals("Exit"))
        System.exit(0);
    else System.out.println("Error in button interface.");

}

public static void main(String[] args)
{
    PhotoAlbum buttonGui= new PhotoAlbum();
    buttonGui.setVisible(true);
}


}
import javax.swing.*;
导入java.awt.*;
导入java.awt.event.*;
公共类PhotoAlbum扩展JFrame实现ActionListener{
私人JPanel imagePanel;
私人JPanel labelPanel;
公共相册(){
超级();
容器contentPane=getContentPane();
设置大小(18001000);
setDefaultCloseOperation(关闭时退出);
setTitle(“按钮演示”);//此处为主题
contentPane.setBackground(颜色:蓝色);
setLayout(新的BorderLayout());
创建按钮(contentPane);
说明(内容窗格);
createImageLabel(contentPane);
}
公共无效指令(容器内容窗格){
JPanel指令=新的JPanel();
指令.setLayout(新的FlowLayout());
说明.立根背景(颜色.黄色);
Font fontType1=新字体(“Comic Sans MS”,Font.BOLD,40);
JLabel指令1=新JLabel(“单击按钮查看标签”
+"照片";;
说明1.设置前景(颜色为蓝色);
说明1.设置字体(fontType1);
说明.添加(说明1);
contentPane.add(说明,BorderLayout.SOUTH);
}
公共void createButtons(容器内容窗格){
labelPanel=新的JPanel();
标签面板。背景(颜色。粉红色);
labelPanel.setLayout(新网格布局(9,1));
String[]imageLabel=新字符串[9];
imageLabel[0]=“图像1”;
imageLabel[1]=“图像2”;
imageLabel[2]=“图像3”;
imageLabel[3]=“图像4”;
imageLabel[4]=“图像5”;
imageLabel[5]=“图像6”;
imageLabel[6]=“图像7”;
imageLabel[7]=“图像8”;
imageLabel[8]=“退出”;
颜色[]颜色=新颜色[9];
颜色[0]=color.cyan;
颜色[1]=新颜色(242、121、234);
颜色[2]=颜色为红色;
颜色[3]=颜色为绿色;
颜色[4]=颜色为蓝色;
颜色[5]=新颜色(1255248);
颜色[6]=颜色为洋红色;
颜色[7]=新颜色(205,255,1);
颜色[8]=新颜色(205,255,1);
Font fontType=新字体(“Times new Roman”,Font.BOLD,30);
JButton[]按钮=新JButton[9];

对于(inti=0;i,正如Andrew在他的评论中所建议的那样

if(actionCommand.equals("Image 1")) {
    JLabel addImage = new JLabel();
    URL url = getClass().getResource("picture1.jpg");
    if (url != null) {
        ImageIcon image = new ImageIcon(url);
        addImage.setIcon(image);
        imagePanel.add(addImage);
        imagePanel.setBackground(Color.yellow);
        this.revalidate();
    }
}
picture1.jpg和PhotoAlbum.java在同一个地方


否则,您的代码工作正常。尽管大图像无法正确显示。

正如Andrew在其评论中所建议的那样。请按如下方式重构代码

if(actionCommand.equals("Image 1")) {
    JLabel addImage = new JLabel();
    URL url = getClass().getResource("picture1.jpg");
    if (url != null) {
        ImageIcon image = new ImageIcon(url);
        addImage.setIcon(image);
        imagePanel.add(addImage);
        imagePanel.setBackground(Color.yellow);
        this.revalidate();
    }
}
picture1.jpg和PhotoAlbum.java在同一个地方


否则,您的代码工作正常。尽管大图像无法正确显示。

在构造GUI时,比在“执行的操作”方法中创建和添加标签更好(更容易)的方法是这样做。没有文本或图像的标签对用户不可见。单击按钮时,设置现有标签的图标。
新建图像图标(“picture1.jpg”);
应用程序资源在部署时将成为嵌入式资源,因此现在就开始像访问它们一样访问它们是明智的。必须通过URL而不是文件访问。有关如何形成URL的信息,请参阅。通过“何时构建GUI”,你的意思是在代码的开头吗?另外,我刚刚测试了一下,当我注意到最初单击按钮时,图像没有显示。但是,当我按下按钮,然后最大化/最小化窗口时,图像显示出来。我不知道这是否与我的问题有关。这是构造函数
public PhotoAlbum(){
在它和结束构造函数的
}
之间的某个位置执行它。更好(更容易)在action Executed方法中创建和添加标签的方法是在构建GUI时创建和添加标签。没有文本或图像的标签对用户不可见。单击按钮时,为现有标签设置图标。
new ImageIcon(“picture1.jpg”)
应用程序资源在部署时将成为嵌入式资源,因此现在就开始像访问它们一样访问它们是明智的。必须通过URL而不是文件来访问。有关如何形成URL的信息,请参阅。通过“何时构建GUI”,你的意思是在代码的开头吗?另外,我刚刚测试了一下,当我注意到最初单击按钮时,图像没有显示。但是,当我按下按钮,然后最大化/最小化窗口时,图像显示出来。我不知道这是否与我的问题有关。这是构造函数
public PhotoAlbum(){
在它和结束构造函数的
}
之间的某个位置执行它。这应该在
if(actionCommand.equals(“Image 1”)
}之间的
?我尝试过使用它,但是它会将背景更改为黄色,但不会添加图像。@blaze077能否设置一个断点并检查url是否为空。您的代码正在为我工作并显示图像。我不熟悉断点。我知道如何添加断点,但不知道如何检查url是否为空图片必须在我的Java项目中,是吗?@blaze077图片必须在我的Java项目中,这是可以的,但是路径应该是正确的。这应该在
if(actionCommand.equals(“Image 1”)
之间的
{
}
?我尝试过使用它,但是它会将背景更改为黄色,但不会添加图像。@blaze077能否设置一个断点并检查url是否为空。您的代码正在为我工作并显示图像。我不熟悉断点。我知道如何添加断点,但不知道如何检查url是否为空图片必须在我的Java项目中,ye