Reputation: 374
i have created a JFrame in netbeans. But when i run the program, the Jframe size is too small. here is my code.
import javax.swing.JFrame;
public class Window {
private static void demo()
{
JFrame frame =new JFrame();
frame.setVisible(true);
}
public static void main(String[] args) {
javax.swing.SwingUtilities.invokeLater(new Runnable(){
public void run()
{
demo();
}
});
}
}
Upvotes: 8
Views: 17343
Reputation: 1
You must have a public component method like
public gestion_name_of_class() {
initComponents();
}
Upvotes: -2
Reputation: 8268
You just need to add one line line so that you can give your frame a size.The line is
frame.setSize(300,300);
Here is the full code:
import javax.swing.JFrame;
public class Window {
private static void demo()
{
JFrame frame =new JFrame();
frame.setSize(300,300);
frame.setVisible(true);
}
public static void main(String[] args) {
javax.swing.SwingUtilities.invokeLater(new Runnable(){
public void run()
{
demo();
}
});
}
}
Upvotes: 0
Reputation: 33534
Try this way...
Use the setSize(width, height)
method of JFrame.
public class Myframe extends JFrame{
public Myframe(){
this.setSize(300,300);
}
public static void main(String[] args){
EventQueue.invokeLater(new Runnable(){
public void run(){
Myframe f = new Myframe("Frame");
f.setVisible(true);
}
});
}
}
Upvotes: 2
Reputation: 189
If you want to maximize it you could try
this.setVisible(false);
this.setExtendedState(MAXIMIZED_BOTH);
this.setVisible(true);
this.setResizable(false);
Else for some specific size use
this.setSize(width,height);
Upvotes: 0
Reputation: 32391
You can use frame.setSize(width, height)
in order to set its size or frame.setBounds(x, y, width, height)
for setting both the location and size.
A better choice would be to call frame.pack()
after you add some components to its content pane.
Upvotes: 9