新手提问:java JFrame.add()与JFrame.setcontentPane()区别
packageswt;importjava.awt.*;importjava.awt.event.*;importjavax.swing.*;publicclassDem...
package swt;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class Demo_13 extends JFrame{
JPanel pane1,pane2,pane3;
JButton button;
JLabel label01,label02;
JTextField jxt01,jxt02;
public static void main(String[] args){
new Demo_13();
}
public Demo_13(){
JPanel pane1=new JPanel();
JPanel pane2=new JPanel();
JPanel pane3=new JPanel();
JButton button=new JButton("登录");
JLabel label01=new JLabel("姓名");
JLabel label02=new JLabel("密码");
JTextField jxt01=new JTextField(10);
JTextField jxt02=new JTextField(10);
pane1.add(label01);
pane1.add(jxt01);
pane2.add(label02);
pane2.add(jxt02);
pane3.add(button);
this.add(pane1);
this.add(pane2);
this.add(pane3);
//this.setContentPane(pane1);
//this.setContentPane(pane2);
//this.setContentPane(pane3);
this.setLayout(new GridLayout(3,1));
this.setTitle("学生管理系统");
this.setSize(300, 300);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setVisible(true);
}
}
-----------------------------------
当我用
this.setContentPane(pane1);
this.setContentPane(pane2);
this.setContentPane(pane3);
替换this.add()方法时候运行结果如图:
用this.add()方法运行结果正常
add()与setContentPane()区别在哪里? 展开
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class Demo_13 extends JFrame{
JPanel pane1,pane2,pane3;
JButton button;
JLabel label01,label02;
JTextField jxt01,jxt02;
public static void main(String[] args){
new Demo_13();
}
public Demo_13(){
JPanel pane1=new JPanel();
JPanel pane2=new JPanel();
JPanel pane3=new JPanel();
JButton button=new JButton("登录");
JLabel label01=new JLabel("姓名");
JLabel label02=new JLabel("密码");
JTextField jxt01=new JTextField(10);
JTextField jxt02=new JTextField(10);
pane1.add(label01);
pane1.add(jxt01);
pane2.add(label02);
pane2.add(jxt02);
pane3.add(button);
this.add(pane1);
this.add(pane2);
this.add(pane3);
//this.setContentPane(pane1);
//this.setContentPane(pane2);
//this.setContentPane(pane3);
this.setLayout(new GridLayout(3,1));
this.setTitle("学生管理系统");
this.setSize(300, 300);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setVisible(true);
}
}
-----------------------------------
当我用
this.setContentPane(pane1);
this.setContentPane(pane2);
this.setContentPane(pane3);
替换this.add()方法时候运行结果如图:
用this.add()方法运行结果正常
add()与setContentPane()区别在哪里? 展开
- 你的回答被采纳后将获得:
- 系统奖励15(财富值+成长值)+难题奖励10(财富值+成长值)+提问者悬赏5(财富值+成长值)
推荐于2018-02-28
展开全部
add() 是添加到JFrame的容器里面
setContentPane() 是替换JFrame的容器
setContentPane() 是替换JFrame的容器
本回答被提问者和网友采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询
广告 您可能关注的内容 |