1.窗口
package com.itheima;
import javax.swing.*;
import java.awt.*;
public class Main {
public static void main(String[] args) {
JFrame window1 = new JFrame("第一个窗口");
JFrame window2 = new JFrame("第二个窗口");
Container con = window1.getContentPane();
con.setBackground(Color.yellow);
window1.setBounds(60,100,500,400);
window2.setBounds(600,100,500,400);
window1.setVisible(true);
window1.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);//释放窗口
window2.setVisible(true);
window2.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);//退出程序
}
}
- 设置大小
- 设置标题
- 设置可视
- 设置关闭窗口操作
2.常用组件
package com.itheima;
import javax.swing.*;
import java.awt.*;
import java.util.Scanner;
public class Component extends JFrame{
JCheckBox checkBox1,checkBox2;//复选框
JRadioButton radioM,radioF;//单选框
ButtonGroup group;
JComboBox<String> comBox;//下拉列表
public Component() throws HeadlessException {
init();
setVisible(true);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
void init(){
setLayout(new FlowLayout());
comBox = new JComboBox<String>();
checkBox1 = new JCheckBox("喜欢音乐");
checkBox2 = new JCheckBox("喜欢旅游");
group = new ButtonGroup();
radioM = new JRadioButton("男");
radioF = new JRadioButton("女");
group.add(radioM);
group.add(radioF); //归组才能实现单选
add(checkBox1);
add(checkBox2);
add(radioM);
add(radioF);
comBox.addItem("音乐天地");
comBox.addItem("武术天地");
add(comBox);
}
}
package com.itheima;
public class Main {
public static void main(String[] args) {
Component win = new Component();
win.setBounds(100,100,450,260);
win.setTitle("常用组件");
}
}
3.常用布局
这里我主要使用流式布局
package com.itheima;
import javax.swing.*;
import java.awt.*;
public class Component extends JFrame{
JButton button;
public Component(int n) {
init(n);
setVisible(true);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
void init(int n){
int sum = 0;
setLayout(new FlowLayout());
for(int i=0;i<n;i++){
for(int j=0;j<n;j++){
sum++;
String str = "按钮" + sum;
button = new JButton(str);
add(button);
}
}
}
}
package com.itheima;
public class Main {
public static void main(String[] args) {
Component win = new Component(7);
win.setBounds(100,100,450,260);
win.setTitle("常用布局");
}
}
当你调整窗口大小时,组件也会跟着调整自己的位置。利用鼠标调整窗口大小。
注意: JFrame默认布局方式是“东西南北中布局”。
做出以下界面:
package com.itheima;
import javax.swing.*;
import java.awt.*;
public class Component extends JFrame{
JLabel label1,label2;
JTextField text1,text2;
public Component() {
init();
setVisible(true);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
void init(){
setLayout(new FlowLayout());
text1 = new JTextField(10);//设置长度
text2 = new JTextField(10);
label1 = new JLabel("账号:");
label2 = new JLabel("密码:");
add(label1);
add(text1);
add(label2);
add(text2);
}
}
package com.itheima;
public class Main {
public static void main(String[] args) {
Component win = new Component();
win.setBounds(100,100,200,150);
win.setTitle("流式布局");
}
}
第一步: 在MyFrame类中先设置好自己需求的组件作为成员属性。
第二步: 在无参构造函数中,先执行init()函数设置可视,以及关闭后的操作。
第三步: init()函数中设置布局方式,组件实例化,添加add()到窗口里。
第四步: 在主函数中实例化MyFrame()对象,设置大小、标题。
也可通过有参构造函数在MyFrame()内部设置大小、标题。
空布局
package com.itheima;
import javax.swing.*;
import javax.swing.border.EtchedBorder;
import javax.swing.border.TitledBorder;
import java.awt.*;
public class MyFrame extends JFrame{
JLabel label1,label2;
PoliceListen policeListen;
public MyFrame() {
init();
setVisible(true);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
void init(){
setLayout(null);
label1 = new JLabel();
label1.setBounds(130,20,70,60);
label1.setText("I Love You");
label2 = new JLabel();
label2.setBounds(130,90,60,60);
label2.setText("我喜欢你");
add(label1);
add(label2);
}
}
package com.itheima;
public class Main {
public static void main(String[] args) {
MyFrame win = new MyFrame();
win.setBounds(100,100,350,300);
win.setTitle("空布局");
}
}