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来调用则能得到想要的结果。
我想问到底什么调用的。其中有什么区别。
刚解除到图像,不太懂。
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来调用则能得到想要的结果。
我想问到底什么调用的。其中有什么区别。
刚解除到图像,不太懂。
