点击JButton显示图像
问题描述:
正如标题所示,我试图创建一个有几个按钮的程序,每个按钮都会在点击时显示一张图片。 但是,我想知道如果不使用图形类如here并且没有将容器设为全局,这是否可能。 但是我试过这个,我的程序似乎没有将图像添加到我的面板。点击JButton显示图像
下面是代码:
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);
}
}
答
安德鲁在他的评论暗示。像下面那样重构你的代码。
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时执行此操作。没有文字或图像的标签对用户是不可见的。点击按钮后,为现有标签设置图标。 –
'new ImageIcon(“picture1.jpg”);'应用程序资源在部署时将成为嵌入式资源,所以现在就开始访问它们是明智的做法。 [tag:embedded-resource]必须通过URL而不是文件访问。请参阅[信息。页面为嵌入式资源](http://stackoverflow.com/tags/embedded-resource/info)如何形成的URL。 –
当“构建GUI时”,你的意思是在代码的开头?另外,我刚刚进行了测试,当我注意到第一次单击按钮时,图像不显示,但是当按下按钮然后最大化/最小化窗口时,图像显示出来。我不知道这与我的问题有什么关系。 – blaze077