错在哪?为什么显示不出图像来

freezhATsis 2009-03-26 08:29:34


//工具栏

JButton toolNewFile = new JButton(new ImageIcon("images/toolNewFile.jpg"));

JButton toolOpenFile = new JButton(new ImageIcon("images/toolOpenFile.jpg"));

JButton toolSaveFile = new JButton(new ImageIcon("images/toolSaveFile.jpg"));

JButton toolCopy = new JButton(new ImageIcon("images/toolCopy.jpg"));

JButton toolCut = new JButton(new ImageIcon("images/toolCut.jpg"));

JButton toolPaste = new JButton(new ImageIcon("images/toolPaste.jpg"));

JButton toolSelectAll = new JButton(new ImageIcon("images/toolSelectAll.jpg"));


图片在文件夹images中,文件夹与源文件同目录下!

...全文
153 12 打赏 收藏 转发到动态 举报
写回复
用AI写文章
12 条回复
切换为时间正序
请发表友善的回复…
发表回复
zswzkl 2011-06-22
  • 打赏
  • 举报
回复
很简但 把完整路径写上就行
hoojo 2009-03-26
  • 打赏
  • 举报
回复

JLabel w4 = new JLabel("");

w4.setBounds(0,35,325,220);
w4.setBackground(Color.white);
img2=new ImageIcon("q.gif");
w4.setIcon(img2);
试试看 这个


freezhATsis 2009-03-26
  • 打赏
  • 举报
回复
[Quote=引用 3 楼 IBM_hoojo 的回复:]
你用的是什么图片 jpg
要ico 的
ImageIcon
[/Quote]

我用ico 和 jpg的都不行
hoojo 2009-03-26
  • 打赏
  • 举报
回复
随便啦
觉得那个说的有道理 或是 比较接近答案
或是 解决问题了
就给
只是多少的问题 大家有不会怪你
0分的帖也有人会
只要是问题 不是广告
freezhATsis 2009-03-26
  • 打赏
  • 举报
回复


抱歉,我之前不晓得要结账的。

难道每个人都要给吗?

我的分不够 啊
freezhATsis 2009-03-26
  • 打赏
  • 举报
回复

//添加事件监听

newItem.addActionListener(this);
openItem.addActionListener(this);
saveItem.addActionListener(this);
printItem.addActionListener(this);
exitItem.addActionListener(this);
copyItem.addActionListener(this);
cutItem.addActionListener(this);
pasteItem.addActionListener(this);
selectItem.addActionListener(this);
aboutItem.addActionListener(this);
toolNewFile.addActionListener(this);
toolOpenFile.addActionListener(this);
toolSaveFile.addActionListener(this);
toolCut.addActionListener(this);
toolCopy.addActionListener(this);
toolPaste.addActionListener(this);
toolSelectAll.addActionListener(this);


}


//实现监听接口

public void actionPerformed(ActionEvent e){

Object eventSource = e.getSource();

if((eventSource == newItem) || (eventSource == toolNewFile)){
textArea.setText("");
}else if((eventSource == openItem) || (eventSource == toolOpenFile)){
openFileDialog.setVisible(true);
fileName = openFileDialog.getDirectory() + openFileDialog.getFile();
if(fileName != null)
readFile(fileName);
}else if((eventSource == saveAsItem) || (eventSource == toolSaveFile)){
saveAsFileDialog.setVisible(true);
fileName = saveAsFileDialog.getDirectory() + saveAsFileDialog.getFile();
if(fileName != null)
writeFile(fileName);
}else if((eventSource == selectItem) || (eventSource == toolSelectAll)){
textArea.selectAll();
}else if((eventSource == copyItem) || (eventSource == toolCopy)){
String text = textArea.getSelectedText();
StringSelection selection = new StringSelection(text);
clipBoard.setContents(selection,null);
}else if((eventSource == cutItem) || (eventSource == toolCut)){
String text = textArea.getSelectedText();
StringSelection selection = new StringSelection(text);
clipBoard.setContents(selection,null);
//剪切时用空串代替被选的字符
textArea.replaceRange("",textArea.getSelectionStart(),textArea.getSelectionEnd());
}else if((eventSource == pasteItem) || (eventSource == toolPaste)){
Transferable contents = clipBoard.getContents(this);
if(contents == null)
return;
String text;
text = "";
try{
text = (String)contents.getTransferData(DataFlavor.stringFlavor);
}catch(Exception exception){}
textArea.replaceRange(text,textArea.getSelectionStart(),textArea.getSelectionEnd());
}else if(eventSource == printItem){
PrinterJob prtme = PrinterJob.getPrinterJob();
prtme.printDialog();
}else if(eventSource == exitItem){
System.exit(0);
}else if(eventSource == aboutItem){
JFrame aboutFrame = new JFrame("关于记事本");
JPanel aboutPanel = new JPanel(new GridLayout(0,1));
JPanel exitPanel = new JPanel(new FlowLayout(FlowLayout.RIGHT));
JLabel label1 = new JLabel("感谢使用");
JLabel label2 = new JLabel("欢迎技术交流");
JLabel label3 = new JLabel("邮箱:good-_-man@live.cn");
JButton exitButton = new JButton("确定");

//Container contentPane = getContentPane();
/* exitButton.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
System.exit(0);
}
});
*/
aboutPanel.add(label1);
aboutPanel.add(label2);
aboutPanel.add(label3);
//exitPanel.add(exitButton);
aboutFrame.add(aboutPanel);
//aboutFrame.add(exitPanel);
//aboutFrame.add(exitButton);

//contentPane.add(aboutPanel,BorderLayout.NORTH);
//contentPane.add(exitPanel,BorderLayout.CENTER);
aboutFrame.setDefaultLookAndFeelDecorated(true);
//aboutFrame.setSize(300,200);
aboutFrame.pack();
aboutFrame.setVisible(true);

}
}

/* protected JButton buttonIcon(String imageName,String buttonName){

String imgLocation = "images/" + imageName + ".gif";
URL imageURL = EditerDemo.class.getResource(imgLocation);
JButton button = new JButton();
button.setIcon(new ImageIcon(imageURL));

return button;
}
*/

//读文件
public void readFile(String fileName){
try{
File file = new File(fileName);
FileReader readIn = new FileReader(file);
int size = (int)file.length();
int charsRead = 0;
char[] content = new char[size];
while(readIn.ready())
charsRead += readIn.read(content,charsRead,size-charsRead);
readIn.close();
textArea.setText(new String(content,0,charsRead));
}catch(Exception e){
System.out.println("Error opening file");
}
}

//写文件
public void writeFile(String fileName){
try{
File file = new File(fileName);
FileWriter writerOut = new FileWriter(file);
writerOut.write(textArea.getText());
writerOut.close();
}catch(Exception e){
System.out.println("Error writing file");
}

}




public static void main(String []args){


JFrame frame = new EditerDemo();
frame.setDefaultLookAndFeelDecorated(true);

//frame.pack();
frame.setVisible(true) ;

}

}

hoojo 2009-03-26
  • 打赏
  • 举报
回复


JLabel w4 = new JLabel("");

w4.setBounds(0,35,325,220);
w4.setBackground(Color.white);
img2=new ImageIcon("q.gif");
w4.setIcon(img2);
试试看 这个
freezhATsis 2009-03-26
  • 打赏
  • 举报
回复

//添加事件监听

newItem.addActionListener(this);
openItem.addActionListener(this);
saveItem.addActionListener(this);
printItem.addActionListener(this);
exitItem.addActionListener(this);
copyItem.addActionListener(this);
cutItem.addActionListener(this);
pasteItem.addActionListener(this);
selectItem.addActionListener(this);
aboutItem.addActionListener(this);
toolNewFile.addActionListener(this);
toolOpenFile.addActionListener(this);
toolSaveFile.addActionListener(this);
toolCut.addActionListener(this);
toolCopy.addActionListener(this);
toolPaste.addActionListener(this);
toolSelectAll.addActionListener(this);


}


//实现监听接口

public void actionPerformed(ActionEvent e){

Object eventSource = e.getSource();

if((eventSource == newItem) || (eventSource == toolNewFile)){
textArea.setText("");
}else if((eventSource == openItem) || (eventSource == toolOpenFile)){
openFileDialog.setVisible(true);
fileName = openFileDialog.getDirectory() + openFileDialog.getFile();
if(fileName != null)
readFile(fileName);
}else if((eventSource == saveAsItem) || (eventSource == toolSaveFile)){
saveAsFileDialog.setVisible(true);
fileName = saveAsFileDialog.getDirectory() + saveAsFileDialog.getFile();
if(fileName != null)
writeFile(fileName);
}else if((eventSource == selectItem) || (eventSource == toolSelectAll)){
textArea.selectAll();
}else if((eventSource == copyItem) || (eventSource == toolCopy)){
String text = textArea.getSelectedText();
StringSelection selection = new StringSelection(text);
clipBoard.setContents(selection,null);
}else if((eventSource == cutItem) || (eventSource == toolCut)){
String text = textArea.getSelectedText();
StringSelection selection = new StringSelection(text);
clipBoard.setContents(selection,null);
//剪切时用空串代替被选的字符
textArea.replaceRange("",textArea.getSelectionStart(),textArea.getSelectionEnd());
}else if((eventSource == pasteItem) || (eventSource == toolPaste)){
Transferable contents = clipBoard.getContents(this);
if(contents == null)
return;
String text;
text = "";
try{
text = (String)contents.getTransferData(DataFlavor.stringFlavor);
}catch(Exception exception){}
textArea.replaceRange(text,textArea.getSelectionStart(),textArea.getSelectionEnd());
}else if(eventSource == printItem){
PrinterJob prtme = PrinterJob.getPrinterJob();
prtme.printDialog();
}else if(eventSource == exitItem){
System.exit(0);
}else if(eventSource == aboutItem){
JFrame aboutFrame = new JFrame("关于记事本");
JPanel aboutPanel = new JPanel(new GridLayout(0,1));
JPanel exitPanel = new JPanel(new FlowLayout(FlowLayout.RIGHT));
JLabel label1 = new JLabel("感谢使用");
JLabel label2 = new JLabel("欢迎技术交流");
JLabel label3 = new JLabel("邮箱:good-_-man@live.cn");
JButton exitButton = new JButton("确定");

//Container contentPane = getContentPane();
/* exitButton.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
System.exit(0);
}
});
*/
aboutPanel.add(label1);
aboutPanel.add(label2);
aboutPanel.add(label3);
//exitPanel.add(exitButton);
aboutFrame.add(aboutPanel);
//aboutFrame.add(exitPanel);
//aboutFrame.add(exitButton);

//contentPane.add(aboutPanel,BorderLayout.NORTH);
//contentPane.add(exitPanel,BorderLayout.CENTER);
aboutFrame.setDefaultLookAndFeelDecorated(true);
//aboutFrame.setSize(300,200);
aboutFrame.pack();
aboutFrame.setVisible(true);

}
}

/* protected JButton buttonIcon(String imageName,String buttonName){

String imgLocation = "images/" + imageName + ".gif";
URL imageURL = EditerDemo.class.getResource(imgLocation);
JButton button = new JButton();
button.setIcon(new ImageIcon(imageURL));

return button;
}
*/

//读文件
public void readFile(String fileName){
try{
File file = new File(fileName);
FileReader readIn = new FileReader(file);
int size = (int)file.length();
int charsRead = 0;
char[] content = new char[size];
while(readIn.ready())
charsRead += readIn.read(content,charsRead,size-charsRead);
readIn.close();
textArea.setText(new String(content,0,charsRead));
}catch(Exception e){
System.out.println("Error opening file");
}
}

//写文件
public void writeFile(String fileName){
try{
File file = new File(fileName);
FileWriter writerOut = new FileWriter(file);
writerOut.write(textArea.getText());
writerOut.close();
}catch(Exception e){
System.out.println("Error writing file");
}

}




public static void main(String []args){


JFrame frame = new EditerDemo();
frame.setDefaultLookAndFeelDecorated(true);

//frame.pack();
frame.setVisible(true) ;

}

}

freezhATsis 2009-03-26
  • 打赏
  • 举报
回复
package demo;


import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.event.*;
import java.io.*;
import java.lang.*;
import java.awt.datatransfer.*;
import java.awt.print.*;
//import java.net.URL;
import javax.swing.Icon;
import java.io.File;
import javax.swing.ImageIcon;



public class EditerDemo extends JFrame implements ActionListener{


JMenuBar menuBar = new JMenuBar();
JToolBar toolBar = new JToolBar();

JTextArea textArea = new JTextArea();
{
textArea.setLineWrap(true);
}
//textArea.setEditbale(true);
JScrollPane scrollPane = new JScrollPane(textArea);

//菜单栏
//JMenuBar menuBar = new JMenuBar();
//菜单项
JMenu fileMenu = new JMenu("文件(F)");
JMenuItem newItem = new JMenuItem("新建(N)");
JMenuItem openItem = new JMenuItem("打开(O)");
JMenuItem saveItem = new JMenuItem("保存(S)");
JMenuItem saveAsItem = new JMenuItem("另存为(A)");
JMenuItem printItem = new JMenuItem("打印(P)");
JMenuItem exitItem = new JMenuItem("退出(X)");

JMenu editMenu = new JMenu("编辑(E)");
JMenuItem copyItem = new JMenuItem("复制(C)");
JMenuItem cutItem = new JMenuItem("剪切(T)");
JMenuItem pasteItem = new JMenuItem("粘贴(P)");
JMenuItem selectItem = new JMenuItem("全选(E)");


JMenu helpMenu = new JMenu("帮助(H)");
JMenuItem aboutItem = new JMenuItem("关于记事本");

//工具栏
//JButton toolNewFile = new JButton("新建");
JButton toolNewFile = new JButton(new ImageIcon("images/toolNewFile.jpg"));
//JButton toolOpenFile = new JButton("打开");
JButton toolOpenFile = new JButton(new ImageIcon("images/toolOpenFile.jpg"));
//JButton toolSaveFile = new JButton("保存");
JButton toolSaveFile = new JButton(new ImageIcon("images/toolSaveFile.jpg"));
//JButton toolCopy = new JButton("复制");
JButton toolCopy = new JButton(new ImageIcon("images/toolCopy.jpg"));
//JButton toolCut = new JButton("剪切");
JButton toolCut = new JButton(new ImageIcon("images/toolCut.jpg"));
//JButton toolPaste = new JButton("粘贴");
JButton toolPaste = new JButton(new ImageIcon("images/toolPaste.jpg"));
//JButton toolSelectAll = new JButton("全选");
JButton toolSelectAll = new JButton(new ImageIcon("images/toolSelectAll.jpg"));

String fileName = "NoName";
Toolkit toolkit = Toolkit.getDefaultToolkit();
Clipboard clipBoard =toolkit.getSystemClipboard();

/**打开文件对话框和保存对话框 */
private FileDialog openFileDialog = new FileDialog(this,"Open File",FileDialog.LOAD);

private FileDialog saveAsFileDialog = new FileDialog(this,"Save File As",FileDialog.SAVE);


public EditerDemo(){

setTitle("记事本Demo版");
setFont(new Font("Times New Roman",Font.PLAIN,12));
// SetBackground(Color.while);
setSize(400,300);

fileMenu.setMnemonic('F');
editMenu.setMnemonic('E');
helpMenu.setMnemonic('H');

fileMenu.add(newItem);
newItem.setMnemonic('N');
newItem.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_N,InputEvent.CTRL_MASK));

fileMenu.add(openItem);
openItem.setMnemonic('O');
openItem.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_O,InputEvent.CTRL_MASK));

fileMenu.add(saveItem);


fileMenu.add(saveAsItem);
saveAsItem.setMnemonic('A');
saveAsItem.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_A,InputEvent.CTRL_MASK));

fileMenu.addSeparator();

fileMenu.add(printItem);
printItem.setMnemonic('P');
printItem.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_P,InputEvent.CTRL_MASK));

fileMenu.addSeparator();

fileMenu.add(exitItem);
exitItem.setMnemonic('X');
exitItem.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_X,InputEvent.CTRL_MASK));

editMenu.add(copyItem);
copyItem.setMnemonic('C');
copyItem.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_C,InputEvent.CTRL_MASK));

editMenu.add(cutItem);
cutItem.setMnemonic('T');
cutItem.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_T,InputEvent.CTRL_MASK));

editMenu.add(pasteItem);
pasteItem.setMnemonic('P');
pasteItem.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_P,InputEvent.CTRL_MASK));

editMenu.addSeparator();

editMenu.add(selectItem);
selectItem.setMnemonic('E');
selectItem.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_E,InputEvent.CTRL_MASK));

helpMenu.add(aboutItem);


toolBar.add(toolNewFile);
toolBar.add(toolOpenFile);
toolBar.add(toolSaveFile);

toolBar.add(Box.createHorizontalStrut(4));
//toolBar.add(new JSeparator(SwingConstants.VERTICAL));
toolBar.add(Box.createHorizontalStrut(4));

toolBar.add(toolCopy);
toolBar.add(toolCut);
toolBar.add(toolPaste);
toolBar.add(toolSelectAll);

menuBar.add(fileMenu);
menuBar.add(editMenu);
menuBar.add(helpMenu);



setJMenuBar(menuBar);
//setJMenuBar(toolBar);
add(toolBar,BorderLayout.PAGE_START);
add(scrollPane,BorderLayout.CENTER);

//add(textArea);
addWindowListener(new WindowAdapter(){
public void windowClosing(WindowEvent e){
System.exit(0);
}
});
hoojo 2009-03-26
  • 打赏
  • 举报
回复
老大 你结贴率 还真高啊
上次帮你回答的帖 还没有结
真不厚道
你用的是什么图片 jpg
要ico 的
ImageIcon
大家都是搞软件的 经常会遇到问题的
问问 没有什么的
干嘛 挂分 要不结贴呢
如果没有分 大家同样也会帮你回复的
这样做我觉得不怎么好
不善^ 2009-03-26
  • 打赏
  • 举报
回复
你 添加 button没啊??
add(toolNewFile) 等
jeson168 2009-03-26
  • 打赏
  • 举报
回复
把你的全部代码COPE过来
有时头文件没引用图片显示不了

62,614

社区成员

发帖
与我相关
我的任务
社区描述
Java 2 Standard Edition
社区管理员
  • Java SE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧