作业帮 > 综合 > 作业

java题目要求我们按left排布就按从做向右排布,按center排布就按从中间排布,按right就按从右向左排布!

来源:学生作业帮 编辑:拍题作业网作业帮 分类:综合作业 时间:2024/05/17 14:56:58
java题目要求我们按left排布就按从做向右排布,按center排布就按从中间排布,按right就按从右向左排布!
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
public class MyFlowLayout extends JFrame implements ActionListener
{
private JButton leftButton;
private JButton centerButton;
private JButton rightButton;
JPanel contentPane=(JPanel) this.getContentPane();
public MyFlowLayout(){
leftButton=new JButton("left");
centerButton=new JButton("center");
rightButton=new JButton("right");
contentPane.setLayout(new FlowLayout(FlowLayout.CENTER));
contentPane.add(leftButton);
contentPane.add(centerButton);
contentPane.add(rightButton);
setSize(500,100);
setVisible(true);
show();
leftButton.addActionListener(this);
centerButton.addActionListener(this);
rightButton.addActionListener(this);
}
public static void main(String[] args){
javax.swing.SwingUtilities.invokeLater(new Runnable(){public void run(){
MyFlowLayout frame=new MyFlowLayout();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
});
}
public void actionPerformed(ActionEvent e){
String command=e.getActionCommand();
if(command.equals("left")){
contentPane.setLayout(new FlowLayout(FlowLayout.LEFT));
}
if(command.equals("center")){
contentPane.setLayout(new FlowLayout(FlowLayout.CENTER));
}
if(command.equals("right")){
contentPane.setLayout(new FlowLayout(FlowLayout.RIGHT));
}
super.repaint();
}
}
这是我写的代码,运行后如果按left只有最大化才能重新排布.再按right和第一次按一样只有最小化才能重新排布,这是为什么!
这个知识点你还没有学到!