你要加上:
getClass().getResource(...)
例如:import java.awt.*;
import java.awt.event.*;
import java.util.Vector;
import javax.swing.*;public class JListDemo extends Frame {
Panel panelShow = new Panel();
JList dataList;
BorderLayout bordL = new BorderLayout();
JTextField textField; public JListDemo() {
try {
textField=new JTextField("first");
String[] data = {"one", "two", "free", "four"};
JList dataList = new JList(data);
panelShow.setLayout(bordL);
dataList.addMouseListener(new java.awt.event.MouseAdapter() {
public void mouseClicked(MouseEvent e) {
textField.setText("change");
}
});
dataList.setVisibleRowCount(12); // Add list to a scrollpane
JScrollPane scrollPane = new JScrollPane(dataList);
panelShow.add(scrollPane);
String str="c:/csdn.gif";
JButton butt=new JButton(createImageIcon("csdn.gif", "Splash.accessible_description"));
// Button1 = new JButton(new ImageIcon("tray1.gif"));
butt.addKeyListener(new java.awt.event.KeyAdapter(){
public void keyPressed(KeyEvent e)
{
if(e.getKeyCode()==KeyEvent.VK_ENTER)
textField.setText("wwwwww");
}
});
panelShow.add(textField, BorderLayout.SOUTH);
panelShow.add(butt, BorderLayout.EAST);
this.add(panelShow, BorderLayout.CENTER);
}
catch(Exception e) {
e.printStackTrace();
}
}
public static void main(String[] args) {
JListDemo JListDemo = new JListDemo(); JListDemo.addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {System.exit(0);}
});
JListDemo.setBounds (120,120,300,300);
JListDemo.show ();
}
public ImageIcon createImageIcon(String filename, String description) {
String path = "/images/" + filename;
return new ImageIcon(getClass().getResource(path));
}}
getClass().getResource(...)
例如:import java.awt.*;
import java.awt.event.*;
import java.util.Vector;
import javax.swing.*;public class JListDemo extends Frame {
Panel panelShow = new Panel();
JList dataList;
BorderLayout bordL = new BorderLayout();
JTextField textField; public JListDemo() {
try {
textField=new JTextField("first");
String[] data = {"one", "two", "free", "four"};
JList dataList = new JList(data);
panelShow.setLayout(bordL);
dataList.addMouseListener(new java.awt.event.MouseAdapter() {
public void mouseClicked(MouseEvent e) {
textField.setText("change");
}
});
dataList.setVisibleRowCount(12); // Add list to a scrollpane
JScrollPane scrollPane = new JScrollPane(dataList);
panelShow.add(scrollPane);
String str="c:/csdn.gif";
JButton butt=new JButton(createImageIcon("csdn.gif", "Splash.accessible_description"));
// Button1 = new JButton(new ImageIcon("tray1.gif"));
butt.addKeyListener(new java.awt.event.KeyAdapter(){
public void keyPressed(KeyEvent e)
{
if(e.getKeyCode()==KeyEvent.VK_ENTER)
textField.setText("wwwwww");
}
});
panelShow.add(textField, BorderLayout.SOUTH);
panelShow.add(butt, BorderLayout.EAST);
this.add(panelShow, BorderLayout.CENTER);
}
catch(Exception e) {
e.printStackTrace();
}
}
public static void main(String[] args) {
JListDemo JListDemo = new JListDemo(); JListDemo.addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {System.exit(0);}
});
JListDemo.setBounds (120,120,300,300);
JListDemo.show ();
}
public ImageIcon createImageIcon(String filename, String description) {
String path = "/images/" + filename;
return new ImageIcon(getClass().getResource(path));
}}
解决方案 »
免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货