import java.awt.Color;
import java.awt.FlowLayout;
import java.awt.Graphics;
import java.awt.Image;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;import javax.swing.Icon;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JOptionPane;public class TestButton extends JButton {
JFrame f; JButton0 b0; Image img; JButton b; ImageIcon ii; public TestButton() {
init();
} public void init() { img = Toolkit.getDefaultToolkit().getImage(
TestButton.class.getResource("1.jpg"));
ii = new ImageIcon(img);
f = new JFrame("test");
b0 = new JButton0("OK", img); f.add(b0);
b0.setBounds(100, 100, 100, 100);
b0.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent arg0) {
JOptionPane.showMessageDialog(f, "hehe!");
} });
f.setLayout(new FlowLayout());
f.setSize(400, 400);
f.setVisible(true); } public static void main(String args[]) {
new TestButton();
}}class JButton0 extends JButton { Image img; String text; public JButton0() {
this.setSize(100, 100);
} public JButton0(Image img) {
this.img = img;
} public JButton0(Icon icon) {
this.setIcon(icon);
} public JButton0(String text) {
this.text = text;
} public JButton0(String text, Image img) {
this.text = text;
this.img = img;
} public void paint(Graphics g) {
int[] x = { 0, 50, 100, 50 };
int[] y = { 50, 0, 50, 100 };
g.drawPolygon(x, y, 4);
g.setColor(Color.blue);
g.fillPolygon(x, y, 4);
if (img != null) {
g.drawImage(img, 25, 25, this.getHeight() / 2, this.getWidth() / 2,
this);
}
g.setColor(Color.RED);
g.drawString(text, 43, 55);
}
}
在eclipse
运行错误提示
Uncaught error fetching image:
java.lang.NullPointerException
at sun.awt.image.URLImageSource.getConnection(URLImageSource.java:97)
at sun.awt.image.URLImageSource.getDecoder(URLImageSource.java:106)
at sun.awt.image.InputStreamImageSource.doFetch(InputStreamImageSource.java:240)
at sun.awt.image.ImageFetcher.fetchloop(ImageFetcher.java:172)
at sun.awt.image.ImageFetcher.run(ImageFetcher.java:136)高手们帮忙改下??谢谢各位?
import java.awt.FlowLayout;
import java.awt.Graphics;
import java.awt.Image;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;import javax.swing.Icon;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JOptionPane;public class TestButton extends JButton {
JFrame f; JButton0 b0; Image img; JButton b; ImageIcon ii; public TestButton() {
init();
} public void init() { img = Toolkit.getDefaultToolkit().getImage(
TestButton.class.getResource("1.jpg"));
ii = new ImageIcon(img);
f = new JFrame("test");
b0 = new JButton0("OK", img); f.add(b0);
b0.setBounds(100, 100, 100, 100);
b0.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent arg0) {
JOptionPane.showMessageDialog(f, "hehe!");
} });
f.setLayout(new FlowLayout());
f.setSize(400, 400);
f.setVisible(true); } public static void main(String args[]) {
new TestButton();
}}class JButton0 extends JButton { Image img; String text; public JButton0() {
this.setSize(100, 100);
} public JButton0(Image img) {
this.img = img;
} public JButton0(Icon icon) {
this.setIcon(icon);
} public JButton0(String text) {
this.text = text;
} public JButton0(String text, Image img) {
this.text = text;
this.img = img;
} public void paint(Graphics g) {
int[] x = { 0, 50, 100, 50 };
int[] y = { 50, 0, 50, 100 };
g.drawPolygon(x, y, 4);
g.setColor(Color.blue);
g.fillPolygon(x, y, 4);
if (img != null) {
g.drawImage(img, 25, 25, this.getHeight() / 2, this.getWidth() / 2,
this);
}
g.setColor(Color.RED);
g.drawString(text, 43, 55);
}
}
在eclipse
运行错误提示
Uncaught error fetching image:
java.lang.NullPointerException
at sun.awt.image.URLImageSource.getConnection(URLImageSource.java:97)
at sun.awt.image.URLImageSource.getDecoder(URLImageSource.java:106)
at sun.awt.image.InputStreamImageSource.doFetch(InputStreamImageSource.java:240)
at sun.awt.image.ImageFetcher.fetchloop(ImageFetcher.java:172)
at sun.awt.image.ImageFetcher.run(ImageFetcher.java:136)高手们帮忙改下??谢谢各位?
解决方案 »
- 问大家一个很菜鸟的问题!
- 读word文档出错
- JavaGroups—构建分布式通信的基础(上)
- 帮忙解决个netbeans的问题,谢谢啊
- jbuilder的classpath设在什么地方?
- 关于UI的一个问题,很挠头,请大家帮忙,必高分酬谢!!!
- 谁有Borland Web Services for Java(jbuilder 7)的注册码?及Borland MobileSet3 的注册码(Jbuilder)??
- 请问:怎样用JDBC与MSSQL建立联接 DriverManage.getConnection(url,user,pwd); 其中的url 应怎么写哦 ?
- 搞了一个星期了,真的急死我了,请各位大哥大姐帮帮忙!看看我的关于查询数据库的问题!一定加分!
- 关于JBuilder4的IIOP生成问题
- 请问 符号 |= 是什么意思?哪位好心人帮帮忙
- 请问关于我在netbean下做拖拉Swing 的问题
TestButton.class.getResource("1.jpg")); // 没读出图片来
或者用
ImageIcon icon = new ImageIcon(this.getClass().getClassLoader().getResource("1.jpg"));
TestButton.class.getResource("1.jpg")); // 没读出图片来
或者用
ImageIcon icon = new ImageIcon(this.getClass().getClassLoader().getResource("1.jpg"));
----------------------------------------------------------------------------------
----------------------------------------------------------------------------------
谢谢1楼的,改了下,现在没有错误了,但是出来的FRAME是空白的没有按钮,怎么把按钮显示出来啊。
f.setLayout(new FlowLayout());错了,已经设置过位置了。
应该是f.setLayout(null);