[英]How Can I add a picture to this program
大家好,我想問一下,我有這個程序,它有6個按鈕,每個按鈕都會顯示不同的東西,但是我想知道當我單擊“圖片1”時如何使它顯示圖片,這些圖片應該放在哪里。該程序知道在哪里可以找到它們? 謝謝希望能對您有所幫助:PS我使用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);
}
}
單擊“圖片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
。
看到...
更多細節。
當然,您可以創建自己的圖像組件來顯示圖像,但這是一個合理的主題,我不確定這是否是您想要的。
這些圖片應該放在哪里,以便程序知道在哪里可以找到它們?
這取決於。 您是否希望圖像始終可供程序使用還是要對其進行更改?
如果圖像不太可能經常更改,則可以將它們作為內部資源嵌入到Jar文件中。 如何完成此操作取決於您構建應用程序的方式,但是一般來說,您是在項目源中創建目錄並將圖像放置在其中。 然后,您可以通過Classloader引用圖像。
如果您希望能夠快速更改圖像(而無需重新構建應用程序),則可以將它們存儲在與應用程序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
程序包)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.