简体   繁体   English

Java Swing ContentPane令人困惑的语句

[英]Java Swing ContentPane Confusing Statements

Same thing in two ways. 同一件事有两种方式。 What is different in these lines? 这些方面有什么不同?

in 1st Statement getContentPane() Method is used ,What is the Purpose of it? 在第一条语句中使用了getContentPane()方法,它的目的是什么? is this Shorthand of 2nd Statements 这是第二句话的简写

JLabel lblNewLabel = new JLabel("New label");
getContentPane().add(lblNewLabel, BorderLayout.NORTH);

in this An Object Declared contentPane and after set the Layout and then set the value of setContentPane by passing and then add without using getContentPane() 在此对象声明的contentPane中,设置布局后,然后通过传递并设置setContentPane的值,然后不使用getContentPane()进行添加

contentPane = new JPanel();             
 contentPane.setLayout(new BorderLayout(0, 0));
 setContentPane(contentPane);

 JLabel lblNewLabel = new JLabel("Name");
 contentPane.add(lblNewLabel, BorderLayout.NORTH);

Plz help me .these lines confusing me. 请帮我。这些台词使我感到困惑。

There's little difference between the two. 两者之间没有什么区别。 The first example simply uses the default content pane create by the parent window, where by the second creates its own content pane and uses the reference directly. 第一个示例仅使用由父窗口创建的默认内容窗格,而第二个示例则使用其自身的内容窗格并直接使用引用。

You could also use... 您还可以使用...

Container contentPane = getContentPane();

JLabel lblNewLabel = new JLabel("Name");
contentPane.add(lblNewLabel, BorderLayout.NORTH);

Take a look at How to use Root Panes for more details 看看如何使用根窗格了解更多详细信息

I should highlight the fact that, by default, JFrame 's content pane uses a BorderLiayout , meaning you first example doesn't need to set the layout, but JPanel uses FlowLayout , so it changed the layout manager to confirm to the expections of a JFrame ...I guess ;) 我应该强调一个事实,默认情况下, JFrame的内容窗格使用BorderLiayout ,这意味着您的第一个示例不需要设置布局,但是JPanel使用FlowLayout ,因此它更改了布局管理器以确认符合预期JFrame ...我想;)

simply create a panel and add your components to that panel at the end of code 只需创建一个面板,然后在代码末尾将组件添加到该面板中

write this one 写这个

getContentPane().add(yourPanel);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM