Java面向对象第十章
一.JFrame窗口容器
1.JFrame窗口需要设置setVisible(当前窗口是否可见)和窗口大小。
2.作业实列:
package com.chapter11;
import java.awt.*;
import javax.swing.*;
public class TestJFrame extends JFrame{
public TestJFrame(){
super("一个nt的窗口");
this.setBounds(300,200,500,600);
ImageIcon icon = new ImageIcon("D:\\捕获.png");
this.setIconImage(icon.getImage());
this.setResizable(false);
this.setVisible(true);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
}
package com.chapter11;
public class Test {
public static void main(String[] args) {
TestJFrame ts = new TestJFrame();
}
}
二.布局管理器
1.FlowLayout流式布局:
(1)FlowLayou流式布局管理器默认为居中放置。
(2)设置右对齐
setLayout(new FlowLayout(FlowLayout.RIGHT,20,40));
(3)设置左对齐
setLayout(new FlowLayout(FlowLayout.LEFT,20,40));
2.GridLayout网格布局:
setLayout(new GridLayout(行,列));
3.边框布局:
JButton 对象名=new JButton("东");
BroderLayout 对象名1=new BoderLayout();
this.setLayout(对象名1);
this.add(对象名,BroderLayout.EAST);
4.作业实列:
package com.chapter12;
import java.awt.GridLayout;
import javax.swing.*;
public class GridLayoutTest extends JFrame {
public GridLayoutTest(){
super("网格布局");
this.setBounds(200,300,600,600);
this.setResizable(false);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
GridLayout gl = new GridLayout(4,4,5,5);
this.setLayout(gl);
for (int i = 0;i<4;i++){
for (int j = 0;j<4;j++){
int k = i*4+j;
this.add(new JButton((1+k)+""));
}
}
this.setVisible(true);
}
}
package com.chapter12;
public class Test {
public static void main(String[] args) {
GridLayoutTest gl = new GridLayoutTest();
gl.setVisible(true);
}
}
三.常用控件:
1.组件
JLabel:标签组件
JTextField:文本框组件
JPasswordField:密码框组件
JButton:按钮组件
JPanel:面板组件
JComboBox:下拉选择框组件
JRadioButton:单选按钮组件
JCheckBox:复选框组件
2.作业实列:
package com.chapter13;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPasswordField;
import javax.swing.JTextField;
public class LoginFrame extends JFrame {
public LoginFrame(){
super("登录窗口");
this.setSize(300,180);
this.setLayout(null);
JLabel jl1 = new JLabel("账号");
jl1.setBounds(20,10,60,60);
JTextField jf1 = new JTextField();
jf1.setBounds(90,30,160,20);
JLabel jl2 = new JLabel("密码");
jl2.setBounds(20,40,60,60);
JPasswordField jp1 = new JPasswordField();
jp1.setBounds(90,60,160,20);
JButton jb1 = new JButton("登录");
jb1.setBounds(90,100,60,20);
JButton jb2 = new JButton("注册");
jb2.setBounds(187,100,60,20);
this.add(jl1);
this.add(jf1);
this.add(jl2);
this.add(jp1);
this.add(jb1);
this.add(jb2);
}
}
package com.chapter13;
public class Test {
public static void main(String[] args) {
LoginFrame lf = new LoginFrame();
lf.setVisible(true);
}
}