Source code
package myPackage;
import java.awt.Color;
import javax.swing.JFrame;
/*JPanle import below
make sure to import
*/
import javax.swing.JPanel;
public class myClass{
public static void main(String[]args){
JFrame frame = new JFrame();
/*create a JPanel object Here*/
JPanel panel = new JPanel();
frame.setDefaultCloseOperation
(JFrame.EXIT_ON_CLOSE);
frame.setTitle("create Jpanel");
frame.setVisible(true);
frame.setSize(450, 500);
panel.setBackground(Color.BLACK);
frame.add(panel);
}
}
And if you run this it would look like
Create a method for JPanel in java and add to the frame..So for this we'll hava two classes a main and a mehthods class. so let's work
this is the main class
package myPackage;
public class myMain {
public void main(String[]args){
myClass clas = new myClass();
}
}
mehtods class
package myPackage;
import java.awt.Color;
import javax.swing.JFrame;
import javax.swing.JPanel;
public class myClass {
/*create a Jframe here*/
JFrame frame = new JFrame();
/*create a JPanel here*/
JPanel panel = new JPanel();
public myClass() {
frame();
}
public void frame(){
frame.setDefaultCloseOperation
(JFrame.EXIT_ON_CLOSE);
frame.setTitle("create Jpanel");
frame.setVisible(true);
frame.setSize(450, 500);
/*join both together
* cause we are call
* this mehtod in
* the constructer
* so call the panel
* method
*/
panel();
}
public void panel(){
panel.setBackground(Color.black);
frame.add(panel);
}
}
and same result
If right now if you have any problem just contact me i have given the contact form in each of my page.So just click on that and leave me a comment or just message me and trust me.i'd love help you out from every difficulties you have..
No comments:
Post a Comment