The following class creates a window/frame.
public class Window {
private int width, height;
private JFrame frame;
private Canvas canvas;
private String title;
private JButton button;
private JPanel panel;
public Window(String title){
System.out.println("Initialization Window...");
this.title = title;
width = Reference.width;
height = Reference.height;
button = new JButton("cool button");
CreateWindow();
}
private void CreateWindow(){
frame = new JFrame(title);
frame.setSize(width, height);
frame.setResizable(false);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setLocationRelativeTo(null);
frame.setVisible(true);
panel = new JPanel();
panel.add(button);
canvas = new Canvas();
canvas.setPreferredSize(new Dimension(width, height));
canvas.setMaximumSize(new Dimension(width, height));
canvas.setMinimumSize(new Dimension(width, height));
canvas.setFocusable(false);
frame.add(canvas);
frame.add(panel);//my problem is in this line
frame.pack();
}
i added to the frame canvas and jpanel when i run it. the size of frame is set to very small a size of a button that i have made. but removing "frame.add(panel)
will make it back to normal size. did i miss something?
in case why im using jpanel and canvas. well im using canvas because i use bufferstategy for the drawing graphic, and i need jpanel to add buttons and other things too.
null
layout like the plague. I must say, It would have been simpler if you had just extended JFrame
unless you want to extend something else. You need to understand that for code readability and reuseability, you need to follow conventional Java rules and best practices.
@Hovercraft Full Of Eels has explained all you need above. All I do here is to set you right by example so there is no need duplicating what he has said. FlowLayout may be the easiest and simplest layout manager in Java but it is not that very powerful compared to say GridLayout
or GrdiBagLayout
. Here is the code:
public class Window extends JFrame {
private int width, height;
private Canvas canvas;
private String title;
private JButton button;
private JPanel panel;
public Window(String title){
super( title );
System.out.println("Initialization Window...");
this.title = title;
setLayout( new FlowLayout() );
//width = Reference.width;
//height = Reference.height;
button = new JButton("cool button");
createWindow();
}
private void createWindow(){
setSize(width, height);
setResizable(false);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setLocationRelativeTo(null);
setVisible(true);
panel = new JPanel();
panel.add(button);
canvas = new Canvas();
canvas.setPreferredSize(new Dimension(200, 200));
canvas.setMaximumSize(new Dimension(400, 400));
canvas.setMinimumSize(new Dimension(200, 200));
canvas.setFocusable(false);
add(canvas);
add(panel);//my problem is in this line
pack();
}
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.