Rae*_*hid 6 java swing jlabel jcombobox imageicon
每当从JComboBox中选择一个项目时,我都试图从图像文件夹中将图标设置为JLabel.JComboBox中的项目名称和文件夹中图像的名称相同.因此,无论何时从JComboBox中选择一个项目,都应将具有相同名称的相应图像设置为JLabel的图标.我想做这样的事情.
private void cmb_movieselectPopupMenuWillBecomeInvisible(javax.swing.event.PopupMenuEvent evt){
updateLabel(cmb_moviename.getSelectedItem().toString());
}
protected void updateLabel(String name) {
ImageIcon icon = createImageIcon("C:\\Users\\xerof_000\\Pictures\\tmspictures\\" + name + ".jpg");
if(icon != null){
Image img = icon.getImage();
Image newimg = img.getScaledInstance(lbl_pic.getWidth(), lbl_pic.getHeight(), java.awt.Image.SCALE_SMOOTH);
icon = new ImageIcon(newimg);
lbl_pic.setIcon(icon);
lbl_pic.setText(null);
}
else{
lbl_pic.setText("Image not found");
lbl_pic.setIcon(null);
}
}
protected static ImageIcon createImageIcon(String path) {
URL imgURL;
imgURL = NowShowing.class.getResource(path);
if (imgURL != null) {
return new ImageIcon(imgURL);
} else {
return null;
}
}
Run Code Online (Sandbox Code Playgroud)
我认为问题出现在"C:\ Users\xerof_000\Pictures\tmspictures \"中我尝试使用"C:/ Users/xerof_000/Pictures/tmspictures /",但即使这样也行不通.无论我做什么,它只在JLabel上显示"Image not found".
nIc*_*cOw 12
这是我的目录结构:
packageexample
|
-------------------------------------------
| | |
build(folder) src(folder) manifest.txt
| |
swing(package) ComboExample.java
|
imagetest(subpackage)
|
ComboExample.class + related .class files
Run Code Online (Sandbox Code Playgroud)
这是ComboExample.java文件的内容:
package swing.imagetest;
import java.awt.*;
import java.awt.event.*;
import java.net.*;
import javax.swing.*;
public class ComboExample {
private String[] data = new String[]{
"geek0",
"geek1",
"geek2",
"geek3",
"geek4"
};
private String MESSAGE = "No Image to display yet...";
private JLabel imageLabel;
private JComboBox cBox;
private ActionListener comboActions =
new ActionListener() {
@Override
public void actionPerformed(ActionEvent ae) {
JComboBox combo = (JComboBox) ae.getSource();
ImageIcon image = new ImageIcon(
getClass().getResource(
"/" + combo.getSelectedItem() + ".gif"));
if (image != null) {
imageLabel.setIcon(image);
imageLabel.setText("");
} else {
imageLabel.setText(MESSAGE);
}
}
};
private void displayGUI() {
JFrame frame = new JFrame("Combo Example");
frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
JPanel contentPane = new JPanel();
imageLabel = new JLabel(MESSAGE, JLabel.CENTER);
cBox = new JComboBox(data);
cBox.addActionListener(comboActions);
contentPane.add(imageLabel);
contentPane.add(cBox);
frame.setContentPane(contentPane);
frame.pack();
frame.setLocationByPlatform(true);
frame.setVisible(true);
}
public static void main(String... args) {
EventQueue.invokeLater(new Runnable() {
@Override
public void run() {
new ComboExample().displayGUI();
}
});
}
}
Run Code Online (Sandbox Code Playgroud)
现在编译:
为了编译,我做了这个:
Gagandeep Bali@LAPTOP ~/c/Mine/JAVA/J2SE/src/packageexample
$ javac -d build src/*.java
Run Code Online (Sandbox Code Playgroud)
清单文件的内容:

JAR文件创建:
Gagandeep Bali@LAPTOP ~/c/Mine/JAVA/J2SE/src/packageexample
$ cd build
Gagandeep Bali@LAPTOP ~/c/Mine/JAVA/J2SE/src/packageexample/build
$ jar -cfm imagecombo.jar ../manifest.txt *
Run Code Online (Sandbox Code Playgroud)
现在把它JAR File带到任何有这些图像的位置(
,
,
,
和
),并运行JAR File,并看到结果:-)
| 归档时间: |
|
| 查看次数: |
57683 次 |
| 最近记录: |