package texet;
import javax.swing.*;
import java.awt.*;
public class text extends JFrame {
public text(){
this.setTitle("第一个界面");
this.setBounds(300, 300, 600, 450);
this.setDefaultCloseOperation(this.EXIT_ON_CLOSE);
this.setVisible(true);
}
public static void main(String[] args){
new text();
}
}
程序功能:添加控件,使用按钮控制布局。
package texet;
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class text extends JFrame implements ActionListener{
private Container cont;
private JButton btnLeft;
private JButton btnCenter;
private JButton btnRight;
private JButton btnBorder;
private JButton btnGrid;
private JLabel labHit;
public text (){
this.setTitle("第一个界面");
this.setBounds(300, 300, 600, 450);
this.setDefaultCloseOperation(this.EXIT_ON_CLOSE);
cont = this.getContentPane();
cont.setLayout(new GridLayout(3, 9));
initCompent();
addCompent();
this.setVisible(true);
}
public void initCompent(){
btnLeft = new JButton("LEFT");
btnLeft.addActionListener(this);
btnRight = new JButton("RIGHT");
btnRight.addActionListener(this);
btnCenter = new JButton("CENTER");
btnCenter.addActionListener(this);
btnBorder = new JButton("BORDER");
btnBorder.addActionListener(this);
btnGrid = new JButton("GRID");
btnGrid.addActionListener(this);
labHit = new JLabel("提示");
}
public void addCompent(){
cont.add(btnLeft);
cont.add(btnCenter);
cont.add(btnRight);
cont.add(btnBorder);
cont.add(btnGrid);
cont.add(labHit);
}
public void addBorderCompent(){
cont.add(btnLeft, "West");
cont.add(btnCenter, "Center");
cont.add(btnRight, "East");
cont.add(btnBorder, "North");
cont.add(btnGrid, "South");
}
public void actionPerformed(ActionEvent aEvt) {
if (btnLeft == aEvt.getSource()) {
cont.setLayout(new FlowLayout(FlowLayout.LEFT));
labHit.setText("流式布局管理器左靠齐");
} else {
if (btnRight == aEvt.getSource()) {
cont.setLayout(new FlowLayout(FlowLayout.RIGHT));
labHit.setText("流式布局管理器右靠齐");
} else {
if (btnCenter == aEvt.getSource()) {
cont.setLayout(new FlowLayout(FlowLayout.CENTER));
labHit.setText("流式布局管理器中靠齐");
} else {
if (btnBorder == aEvt.getSource()</