qq_35384299
2016-07-08 12:47
采纳率: 100%
浏览 1.4k
已采纳

java中子类调用父类构造函数中定义的变量

package jm;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class dl extends JFrame implements ActionListener{
GridBagLayout gridbag=new GridBagLayout();

public dl(){
super("登陆界面");
setSize(500,500);

setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

// setLookAndFell();

setLayout(gridbag);
JLabel yhm=new JLabel("用户名:");
JTextField yyhm=new JTextField(20);
JLabel mmm=new JLabel("密码:");
JPasswordField mm=new JPasswordField(20);
mm.setEchoChar('#');
JButton tj=new JButton("登陆");
addComponent(yhm,0,0,10,20,1,1,GridBagConstraints.NONE,GridBagConstraints.EAST);
addComponent(yyhm,1,0,10,20,1,4,GridBagConstraints.NONE,GridBagConstraints.WEST);
addComponent(mmm,0,1,10,20,1,1,GridBagConstraints.NONE,GridBagConstraints.EAST);
addComponent(mm,1,1,10,20,1,1,GridBagConstraints.NONE,GridBagConstraints.WEST);
addComponent(tj,1,40,10,20,1,1,GridBagConstraints.NONE,GridBagConstraints.WEST);
pack();
setVisible(true);
}
public void addComponent(Component component,int gridx,int gridy,int ipadx,int ipady,int weightx,int weighty,int fill,int anchor){
GridBagConstraints constraint=new GridBagConstraints();
constraint.gridx=gridx;
constraint.gridy=gridy;
constraint.ipadx=ipadx;
constraint.ipady=ipady;
constraint.weightx=weightx;
constraint.fill=fill;
constraint.anchor=anchor;
gridbag.setConstraints(component,constraint);
add(component);
}
public void actionPerformed(ActionEvent event){

}

public static void main(String[] arg){
dl dld=new dl();
}

}

 父类的构造函数中定义了一个按钮tj,用来作为登录按钮,没有定义行为,准备在子类中定义

package jm;
import java.awt.event.*;
import java.awt.*;
import javax.swing.*;

public class sczj extends dl{

public sczj(){


}
public void actionPerformed(ActionEvent event){
    Object source=event.getSource();
    if(source==super.tj){
     System.out.println("其实你没有错");        //准备在这里定义tj按钮的行为,但是NETBEANS提示找不到符号tj,动行的时候也没有效果
    }

}

public static void main(String[] arg){
new sczj();

}

}

  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

2条回答 默认 最新

  • zxgmlcj 2016-07-08 14:09
    已采纳

    首先,你这种写法就不符合规范,
    其次,你把按钮定义在构造方法中,这个变量的作用域也是在构造方法中,如何能在外部调用,除非你将按钮的变量设置为类的成员变量,
    即在构造方法外部申明按钮变量

    已采纳该答案
    打赏 评论
  • getsim 2016-07-08 13:39

    same name.

    class father;

    class son: father
    {
    son () { father : father)
    }

    打赏 评论

相关推荐 更多相似问题