当前位置:编程学习 > JAVA >>

这段代码中的frame.add(this);为什么

package 我的代码;

import java.awt.Button;
import java.awt.Checkbox;
import java.awt.CheckboxGroup;
import java.awt.Choice;
import java.awt.Color;
import java.awt.Dialog;
import java.awt.Frame;
import java.awt.Label;
import java.awt.Panel;
import java.awt.TextArea;
import java.awt.TextField;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;

public class GUI3 extends Panel {
public GUI3(String title){
Frame frame =new Frame(title);
frame.setSize(200, 400);
frame.setBackground(Color.GREEN);
frame.setLocationRelativeTo(null);
frame.setResizable(false);
frame.setVisible(true);
frame.add(this);
frame.addWindowListener(new WindowAdapter(){
public void windowClosing(WindowEvent e){
System.exit(0);
}
});
init();

}
private void init() {
Label label1=new Label("用户名:",Label.RIGHT);
this.add(label1);
final TextField userName=new TextField(15);
userName.setEchoChar('*');
this.add(userName);
Label label2=new Label("密码:",Label.RIGHT);
this.add(label2);
final TextField userPassword=new TextField(15);
userPassword.setEchoChar('*');
this.add(userPassword);
Button loginbtn=new Button("登录");
this.add(loginbtn);
Button resetbtn=new Button("清除");
this.add(resetbtn);
final TextArea textArea=new TextArea(10,20);
textArea.setBackground(Color.RED);
textArea.setText("hello,fkjava");
this.add(textArea);
Checkbox hooby1=new Checkbox("小女");
Checkbox hooby2=new Checkbox("美女");
Checkbox hooby3=new Checkbox("小美女",true);
this.add(hooby1);
this.add(hooby2);
this.add(hooby3);
Choice choice=new Choice();
choice.addItem("小学");
choice.addItem("小");
choice.addItem("学");
this.add(choice);
CheckboxGroup cg=new CheckboxGroup();
Checkbox 易做图1=new Checkbox("男",true,cg);
Checkbox 易做图2=new Checkbox("女",false,cg);
this.add(易做图1);
this.add(易做图2);
loginbtn.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e) {
textArea.setText(userName.getText()+"\n"+
userPassword.getText());
userName.setText("");
userPassword.setText("");
}

});
resetbtn.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e) {
textArea.setText("");


}

});
choice.addItemListener(new ItemListener(){
public void itemStateChanged(ItemEvent e) {
final Dialog dialog=new Dialog(null, "你好", null);
dialog.setLocationRelativeTo(null);
dialog.setResizable(false);
dialog.setSize(20,100);
Button btn=new Button("确定");
dialog.add(btn);
dialog.setVisible(true);
btn.addActionListener(new  ActionListener(){
public void actionPerformed(ActionEvent e) {
dialog.setVisible(false);
}

});

}


});





}


public static void main(String[] args) {

new GUI3("hello!");
}

}
--------------------编程问答-------------------- 说详细点啊飒飒 --------------------编程问答-------------------- frame.add(this);this指的是GUI3对象 --------------------编程问答-------------------- 你们要把我气死啊,我想要过程,就是说怎么一步一步转过来的 --------------------编程问答-------------------- Panel paner=new Paner();
Paner paner=new GUI3();
Gui3 paner=new GUI3();
易做图这样也不对啊,明明是GUI3的对象,可是那个this,明明就是panel的对象啊,后面都是this.add()了,不是panel.add()吗?继承了就牛逼啊,不懂 --------------------编程问答--------------------
引用 4 楼 u013036449 的回复:
Panel paner=new Paner();
Paner paner=new GUI3();
Gui3 paner=new GUI3();
易做图这样也不对啊,明明是GUI3的对象,可是那个this,明明就是panel的对象啊,后面都是this.add()了,不是panel.add()吗?继承了就牛逼啊,不懂


GUI3 extends Panel,那GUI3也是jpanel啊,是jpanel的子类。 --------------------编程问答--------------------
引用 5 楼 huxiweng 的回复:
Quote: 引用 4 楼 u013036449 的回复:

Panel paner=new Paner();
Paner paner=new GUI3();
Gui3 paner=new GUI3();
易做图这样也不对啊,明明是GUI3的对象,可是那个this,明明就是panel的对象啊,后面都是this.add()了,不是panel.add()吗?继承了就牛逼啊,不懂


GUI3 extends Panel,那GUI3也是jpanel啊,是jpanel的子类。

同意楼上,你可以理解GUI3就是一个panel,this就是指向GUI3这个panel,在frame中显示一个面包,需要将panel添加到frame中
补充:Java ,  Java SE
CopyRight © 2022 站长资源库 编程知识问答 zzzyk.com All Rights Reserved
部分文章来自网络,