JButton Problem
why i can't view the button ?can somebody help me,so i can view the button..this is my code :
import java.applet.Applet;
import java.awt.Graphics;
import javax.swing.JButton;
publicclass testpiantextends Applet{
/**
* @param args
*/
publicvoid init(){
JButton test =new JButton("aaa");
add(test);
}
publicvoid paint(Graphics g){
}
}
thanks.
You have no code in the paint() method. So why did you overide it? The default paint() implementation is responsible for paint components added to the Applet.
Also, you should be extending JApplet, not Applet for a Swing applet.
Read the Swing tutorial on [url http://java.sun.com/docs/books/tutorial/deployment/applet/getStarted.html]Getting Started With Applets[/url]. In fact download an read the entire tutorial.
i got it now.this is my code...but now my question is how to resize the imageicon ?
import java.applet.Applet;
import java.awt.Graphics;
import javax.swing.ImageIcon;
import javax.swing.JButton;
public class testpiant extends Applet {
/**
* @param args
*/
JButton test;
ImageIcon middleButtonIcon;
public void init(){
this.setLayout(null);
middleButtonIcon = new ImageIcon(getClass().getResource("images/10.jpg"));
test = new JButton(middleButtonIcon);
test.setBounds(0,0,130,70);
add(test);
}
public void paint(Graphics g){
super.paint(g);
}
}
Gee, I made two suggestions and you didn't follow either one.
The Image class has a scale method. So you can scale an image and then create an ImageIcon.
So you need to:
a) get your file as an Image and then you can scale the image and create an icon
b) use the getImage() method of ImageIcon to get the Image and then scale it. I don't know if you would need to recreate the IconImage or not.
Whn you've loaded the ImageIcon you can use getImage() to get the Image. Then you'll find that Image has a method to get a scaled instance of itself. Fom that, you can create a new ImageIcon and discard the first.
It's not the nicest way, but hey.
If you want the proper solution, you should probably resize it in a paint application and save it as a new file. Resizing images can be a slow operation.
Oh and best to get out of this awful habit now:
this.setLayout(null);
test.setBounds(0,0,130,70);
Stick to layout managers.