java吧 关注:1,250,172贴子:12,734,558
  • 7回复贴,共1

新手求解一个问题。

只看楼主收藏回复

import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JFrame;
import javax.swing.JButton;
import javax.swing.JPanel;
@SuppressWarnings("serial")
public class Test extends JFrame implements ActionListener{
private JPanel jp = new JPanel();
private JButton[] jbarray = new JButton[] {new JButton("1"),new JButton("2"),new JButton("3"),new JButton("4"),new JButton("5")};
private FlowLayout f1 = new FlowLayout();
public Test(){
this.setLayout(f1); //这个位置搞不懂到底是JPanel还是JFrame里调用
for(int i=0; i < jbarray.length; i++){
jp.add(jbarray[i]);
jbarray[i].addActionListener(this);
}
this.add(jp);
this.setTitle("管理方式");
this.setBounds(100,100,400,150);
this.setVisible(true);
}
public void actionPerformed(ActionEvent e){
for(int i=0; i<jbarray.length; i++){
if(e.getSource() == jbarray[i]){
f1.setAlignment(FlowLayout.LEFT+i);
jp.revalidate();
}
}
}
public static void main(String[] args){
new Test();
}
}
注释那位置我用this或者不用对象还有super来调用则能得到想要的结果。
我想问到底什么调用的。其中有什么区别。
刚解除到图像,不太懂。


1楼2013-04-15 22:19回复
    还有解释下这行
    if(e.getSource() == jbarray[i]){
    e.getSource() 他返还的是Object类型的。怎么比?
    可能理解有误,解释下。谢谢。


    2楼2013-04-15 22:22
    回复
      test继承了JFrame,this.setLayout(f1)是JFrame的子类test的构造方法所包涵的


      3楼2013-04-15 22:25
      收起回复
        如果还不懂就回去看看书吧


        4楼2013-04-15 22:45
        回复