2012年6月1日 星期五

Java Basic(83)-FlowLayout


import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

public class Layout extends JFrame{
private JButton lb;
private JButton cb;
private JButton rb;
private FlowLayout layout;
private Container container;
public Layout(){
super("the title");
layout = new FlowLayout();
container = getContentPane();
setLayout(layout);
/ /左的東西在這裡
lb = new JButton("left");
add(lb);
lb.addActionListener(
new ActionListener(){
public void actionPerformed(ActionEvent event){
layout.setAlignment(FlowLayout.LEFT);
layout.layoutContainer(container);
}
}
);
/ /中心的東西在這裡
cb = new JButton("center");
add(cb);
cb.addActionListener(
new ActionListener(){
public void actionPerformed(ActionEvent event){
layout.setAlignment(FlowLayout.CENTER);
layout.layoutContainer(container);
}
}
);
//的東西在這裡
rb = new JButton("right");
add(rb);
rb.addActionListener(
new ActionListener(){
public void actionPerformed(ActionEvent event){
layout.setAlignment(FlowLayout.RIGHT);
layout.layoutContainer(container);
}
}
);
}

}


import javax.swing.*;

public class bucky {
public static void main(String[] args){
Layout l = new Layout();
l.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
l.setSize(300,100);
l.setVisible(true);
}

}




沒有留言:

張貼留言