添加组件 两种方法的 区别

脉动回来 2010-10-09 02:46:22
import javax.swing.*;

public class useJFrame {
JFrame mainFrame;
JLabel myJLabel;
public useJFrame() {
mainFrame=new JFrame("JFrame演示窗口");
myJLabel=new JLabel("世界,你好");
mainFrame.getContentPane().setLayout(new FlowLayout());//设置布局
mainFrame.getContentPane().add(myJLabel); //注意这种添加组件的方法
mainFrame.pack();
mainFrame.setVisible(true);
mainFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}

public static void main(String[] args) {
new useJFrame();
}
}


...全文
125 7 打赏 收藏 转发到动态 举报
写回复
用AI写文章
7 条回复
切换为时间正序
请发表友善的回复…
发表回复
0轰隆隆0 2010-10-19
  • 打赏
  • 举报
回复
这是一面墙,你要在这面墙上画画,怎么画呢?如果你直接在墙上画的话,你怎么画呢,如果你给上面贴上一层布,你将所有元素都画在上面,方便布局,也方便管理,比如动态隐藏,动态添加,动态修改元素等等
脉动回来 2010-10-19
  • 打赏
  • 举报
回复
受教 结贴了 !
huntor 2010-10-18
  • 打赏
  • 举报
回复
[Quote]As a conveniance add and its variants, remove and setLayout have been overridden to forward to the contentPane as necessary. This means you can write:

frame.add(child);

And the child will be added to the contentPane. [/Quote]
凉岑玉 2010-10-17
  • 打赏
  • 举报
回复
请参阅 The Java Tutorial 中的 How to Make Frames 一节。
脉动回来 2010-10-09
  • 打赏
  • 举报
回复
重贴一下
脉动回来 2010-10-09
  • 打赏
  • 举报
回复
public class useJFrame {
JFrame mainFrame;
JLabel myJLabel;
public useJFrame() {
mainFrame=new JFrame("JFrame演示窗口");
myJLabel=new JLabel("世界,你好");
mainFrame.getContentPane().setLayout(new FlowLayout());//设置布局
mainFrame.getContentPane().add(myJLabel); //注意这种添加组件的方法
mainFrame.pack();
mainFrame.setVisible(true);
mainFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}

public static void main(String[] args) {
new useJFrame();
}
}
脉动回来 2010-10-09
  • 打赏
  • 举报
回复
和这种添加组件的方法有什么不同
mainFrame.add(myJLabel);

62,616

社区成员

发帖
与我相关
我的任务
社区描述
Java 2 Standard Edition
社区管理员
  • Java SE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧