大家好,我想问一下,我有这个程序,它有 6 个按钮,每个按钮都会显示不同的内容,但我想知道当我单击“图片 1”时如何让它显示图片以及这些图片应该在哪里以便程序知道在哪里可以找到它们? 谢谢希望您能帮忙: 附注我使用netbeans
import java.awt.Toolkit;
import java.awt.Dimension;
import java.awt.Container;
import java.awt.FlowLayout;
import javax.swing.JButton;
import javax.swing.JFrame;
public class Gui {
static JFrame aWindow = new JFrame("This Is Me:");
public static void main(String[] args) {
int windowWidth = 600;
int windowHeight = 500;
aWindow.setBounds(500,500, windowWidth, windowHeight);
aWindow.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
FlowLayout flow = new FlowLayout();
Container content = aWindow.getContentPane();
content.setLayout(flow);
for(int i = 1; i <= 5; i++) {
content.add(new JButton("Picture " + i));}
String path = "__2 copy.jpg";
aWindow.setVisible(true);
}
}
最佳答案
how can i make it show a picture when i click "Picture 1"?
您需要首先将 ActionListener
附加到按钮。然后,您需要在 actionPerformed
方法中加载图像。
加载图像的最佳方法是使用ImageIO
API。如何实现这一点取决于图像的存储方式。
如果图像存储在应用程序 Jar 中(捆绑资源,请参见下文),您将使用类似...
ImageIO.read(getClass().getResource("/path/to/image.jpg");
如果它们存储在外部...
ImageIO.read(new File("relative/path/to/image.jpg");
加载图像后,您可以使用 ImageIcon
包装由 ImageIO
加载的图像并将其应用到 JLabel
。
看...
了解更多详情。
当然,您可以创建自己的图像组件来显示图像,但这是一个相当高级的主题,我不确定这是否是您想要的。
where should those pictures be located so the program knows where to find them?
这要看情况。您希望图像始终可供程序使用还是要更改它们?
如果图像不太可能经常更改,您可以将它们作为内部资源嵌入到 Jar 文件中。如何完成此操作取决于您构建应用程序的方式,但一般而言,您在项目源中创建一个目录并将图像放置在其中。然后您可以通过类加载器引用图像。
如果您希望能够快速更改图像(并且不需要重新构建应用程序),那么我会将它们存储在与应用程序 Jar 相同的目录中。这将允许您从相对位置将它们引用为File
(即new File("Image.jpg");
)
已更新示例
import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.EventQueue;
import java.awt.FlowLayout;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.image.BufferedImage;
import java.io.File;
import javax.imageio.ImageIO;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;
public class TestImages {
public static void main(String[] args) {
new TestImages();
}
public TestImages() {
EventQueue.invokeLater(new Runnable() {
@Override
public void run() {
try {
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
} catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
}
JFrame frame = new JFrame("Testing");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setLayout(new BorderLayout());
frame.add(new TestPane());
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
});
}
public class TestPane extends JPanel {
private JLabel lblPic;
public TestPane() {
setLayout(new BorderLayout());
JPanel buttons = new JPanel(new FlowLayout(FlowLayout.CENTER));
JButton btnFile = new JButton("Load from file");
JButton btnResource = new JButton("Load from resource");
buttons.add(btnFile);
buttons.add(btnResource);
btnFile.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
try {
BufferedImage image = ImageIO.read(new File("Pony01.png"));
lblPic.setIcon(new ImageIcon(image));
} catch (Exception exp) {
JOptionPane.showMessageDialog(TestPane.this, "Failed to load image", "Fail", JOptionPane.ERROR_MESSAGE);
exp.printStackTrace();
}
}
});
btnResource.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
try {
BufferedImage image = ImageIO.read(getClass().getResource("/Pony02.png"));
lblPic.setIcon(new ImageIcon(image));
} catch (Exception exp) {
JOptionPane.showMessageDialog(TestPane.this, "Failed to load image", "Fail", JOptionPane.ERROR_MESSAGE);
exp.printStackTrace();
}
}
});
lblPic = new JLabel();
lblPic.setVerticalAlignment(JLabel.CENTER);
lblPic.setHorizontalAlignment(JLabel.CENTER);
add(lblPic);
add(buttons, BorderLayout.NORTH);
}
@Override
public Dimension getPreferredSize() {
return new Dimension(300, 300);
}
@Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
Graphics2D g2d = (Graphics2D) g.create();
g2d.dispose();
}
}
}
显然,您需要提供自己的图像。嵌入资源应位于源代码的顶级文件夹中(通常称为 default
包)
关于java - 如何向该程序添加图片,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15939188/