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();
}
}