日期:2014-05-20  浏览次数:20628 次

请问这是怎么错了啊?
import javax.swing.*;
import java.awt.event.*;
import java.awt.*;

public class MyFlowLayoutDemo extends Frame{
// private final JPanel jpanel = new JPanel();
private final String name[] = {"left","center","right"};
private final Button jbutton[] = new Button[3];
private final FlowLayout flowLayout = new FlowLayout();
ActionListener listener = new ActionListener(){
public void actionPerformed(ActionEvent event){
if(((Button)event.getSource()).getLabel().equals("left")){
//// System.out.print("left");
flowLayout.setAlignment(FlowLayout.LEFT);
flowLayout.layoutContainer(this);
}
if(((Button)event.getSource()).getLabel().equals("right")){
flowLayout.setAlignment(FlowLayout.RIGHT);
// System.out.println("right");
}
else flowLayout.setAlignment(FlowLayout.CENTER);
 
}
};

public MyFlowLayoutDemo(){
super();
// add(jpanel,BorderLayout.CENTER);
setLayout(flowLayout);
for(int i=0;i<jbutton.length;i++){
jbutton[i] = new Button(name[i]);
add(jbutton[i]);
jbutton[i].addActionListener(listener);
}
setSize(500,250);
setVisible(true);

}
public static void main(String[] args){
new MyFlowLayoutDemo();
}
}


MyFlowLayoutDemo.java:15: 无法将 layoutContainer 中的 方法 应用于 java.awt.Cont
iner(<匿名 java.awt.event.ActionListener>)
  flowLayout.layoutContainer(this);
  ^
1 错误


请问这是为什么呀?

------解决方案--------------------
另外 把你的 if语句改为if(e.getActionCommand().equals("left")) 这样判断会好一些...
------解决方案--------------------
flowLayout.layoutContainer(this); //这里的this指的是内部类对象本身,而你要用的外部类的this,所以改成
flowLayout.layoutContainer(MyFlowLayoutDemo.this);