[英]Java jframe adding canvas and jpanel to it
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. 我在运行时将其添加到框架canvas和jpanel中。 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? 但是删除"frame.add(panel)
将使其恢复正常大小。我错过了什么吗?
in case why im using jpanel and canvas. 以防为什么我使用jpanel和canvas。 well im using canvas because i use bufferstategy for the drawing graphic, and i need jpanel to add buttons and other things too. 我正在使用画布,因为我使用bufferstategy绘制图形,并且我也需要jpanel添加按钮和其他东西。
null
layout like the plague. 无论您做什么,都要避免像瘟疫这样的null
布局。 I must say, It would have been simpler if you had just extended JFrame
unless you want to extend something else. 我必须说,如果您只是扩展JFrame
除非您想扩展其他内容,否则会更简单。 You need to understand that for code readability and reuseability, you need to follow conventional Java rules and best practices. 您需要了解,为了使代码具有可读性和可重用性,您需要遵循常规的Java规则和最佳实践。
@Hovercraft Full Of Eels has explained all you need above. @Hovercraft Full Of Eels已经在上面解释了您所需要的。 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
. FlowLayout可能是Java中最简单的布局管理器,但与GridLayout
或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();
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.