一.学习的内容
练习代码
(1)
package Demo1;
import java.awt.GridLayout;
import javax.swing.JButton;
import javax.swing.JFrame;
import java.awt.GridLayout;
public class GLayout extends JFrame{
private JButton b1=new JButton("First Button");
private JButton b2=new JButton("Second Button");
private JButton b3=new JButton("THisd Button");
private JButton b4=new JButton("Fourth Button");
public GLayout(){
super("GridLayout 示例");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
getContentPane().setLayout(new GridLayout());
add(b1);
add(b2);
add(b3);
add(b4);
setSize(300,200);
setLocation(400,400);
}
public static void main(String[] args) {
GLayout frame=new GLayout();
frame.setVisible(true);
}
}
(2)
package Demo1;
import java.awt.Container;
import java.awt.GridLayout;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
//代码8-4
public class MyJLabel {
JFrame frame=new JFrame ("标签演示");
JLabel label1,label2,label3,label4,label5;
public static void main(String[] args) {
MyJLabel ld=new MyJLabel();
ld.go();
}
private void go() {
// TODO 自动生成的方法存根
label1=new JLabel("Text Label");
label1.setVerticalAlignment(JLabel.TOP);
label2=new JLabel("Center Label",JLabel.CENTER);
javax.swing.ImageIcon icon=new javax.swing.ImageIcon("heoll1.png");
label3 =new JLabel(icon);
label3.setVerticalAlignment(JLabel.BOTTOM);
label4=new JLabel("Text and Image",icon,JLabel.RIGHT);
label4.setVerticalAlignment(JLabel.TOP);
label4.setVerticalAlignment(JLabel.BOTTOM);
label4.setHorizontalTextPosition(JLabel.CENTER);//设置文本与图标中间对齐
JPanel panel =new JPanel();
panel.setLayout(new GridLayout(4,1));
panel.add(label1);
panel.add(label2);
panel.add(label3);
panel.add(label4);
Container cp =frame.getContentPane();
cp.setLayout(new GridLayout(1,1));
cp.add(panel);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(300, 350);
frame.setVisible(true);
}
}