java使用带有FlowLayout的getContentpane对布局应用更改,但不起作用为什么?
大家好,我正在尝试用actionListener制作3个按钮(左、中、右),因此当有人单击其中一个按钮时,框架的FlowLayout对齐方式会发生变化,但当我尝试使用getContentpane()方法时,它无法正常工作!这是我的密码:
import javax.swing.JFrame;
import javax.swing.JButton;
import java.awt.*;
import java.awt.event.*;
public class FLowLayoutExample extends JFrame implements ActionListener{
public static FlowLayout flowLayout;
public static JButton left,center,right;
public FLowLayoutExample(){
flowLayout = new FlowLayout();
left = new JButton("left");
center = new JButton("center");
right = new JButton("right");
setSize(800,400);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setLayout(flowLayout);
add(left);
add(center);
add(right);
//Register a componentes with the listener
left.addActionListener(this);
center.addActionListener(this);
right.addActionListener(this);
setVisible(true);
}
public static void main(String args[]){
new FLowLayoutExample();
}//End of main method
public void actionPerformed(ActionEvent e){
if(e.getSource() == left){
flowLayout.setAlignment(FlowLayout.LEFT);
}
flowLayout.layoutContainer(getContentpane());
}
}//End of class
我得到了这个错误:
FLowLayoutExample。java:56:错误:找不到符号 流程布局。layoutContainer(getContentpane()); ^ 符号:方法getContentpane() 位置:类FLowLayoutExample 1错误
# 1 楼答案
它的类型应为“getContentPane”,而不是“getContentPane”