窗口----JFrame:
1.普遍使用的方法:
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 设置窗口点击关闭按钮时执行窗口关闭命令
setVisible(true); 设置窗口可见
设置大小、位置:setSize(width,height); setBounds(x,y,width,height); setLocationRelativeTo(null);
setResizable(boolean b); 设置窗口大小是否可调(默认可调)
setTitle(title); 设置窗口名
2.如果给Jframe使用setLocationRelativeTo(null)方法,并且通过setSize(200,200)设置大小时候,要想窗口位于中央,则应该先设置大小,后设置居中,否则会以左上角的点为中心居中;
3.Java编程思想中,一般定义一个类继承自JFrame类,为其初始化,然后再在入口函数中创建对象;
Example:
import javax.swing.JFrame;
class BlankWin extends JFrame
{
public BlankWin(String title){
setTitle(title);
setSize(500,300);
setLocationRelativeTo(null);
setDefaultCloseOperation(EXIT_ON_CLOSE);
setVisible(true);
}
}
class Test1
{
public static void main(String[] args)
{
BlankWin bw=new BlankWin("Hello");
}
}
窗口----JMenuBar(菜单条),JMenu(菜单),JMenuItem(菜单项):
关系:菜单条包含菜单,菜单包含菜单项,需提前弄清逻辑关系,实际编写时才不会思路乱掉。
1.创建菜单条(所有菜单的容器):JMenuBar mb=new JMenuBar();
将菜单条附加在窗口上: JFrame.setJMenuBar(mb);
2.创建菜单:JMenu m1=new JMenu("菜单")
将菜单附加在菜单条上:mb.add(m1);
3.菜单条类似上述操作
import javax.swing.*;
class MenuWindow extends JFrame{
public MenuWindow(String title){
init(title);
createMenu();
setVisible(true);//放最后
}
public void init(String title){
setTitle(title);
setSize(500,300);
setDefaultCloseOperation(EXIT_ON_CLOSE);
setLocationRelativeTo(null);
}
public void createMenu(){
JMenuBar menubar = new JMenuBar();
JMenu menufile = new JMenu("File");
JMenu menuedit = new JMenu("Edit");
menubar.add(menufile);
menubar.add(menuedit);
JMenuItem itemopen = new JMenuItem("Open");
JMenu menudocument = new JMenu("Document");
JMenuItem item1 = new JMenuItem("Document1");
JMenuItem item2 = new JMenuItem("Document2");
menudocument.add(item1);
menudocument.add(item2);
menufile.add(itemopen);
menufile.add(menudocument);//创建菜单的子菜单
setJMenuBar(menubar);
}
}
class Test{
public static void main(String[] args){
MenuWindow mw = new MenuWindow("Test Menu");
}
}
组件:等等等。。。
1、将多个单选框JRadioButton放入一个ButtonGroup中,可以实现单选功能(只能选中一个);
2、布局:简单应用流式布局:this.setLayout(new FlowLayout());//流布局,Jframe默认布局为边界布局,中间只能添加1个组件
3、多选框:应用到泛型,声明时需指明数据类型,然后创建一个对应数据类型的数组,将其传给JComboBox<String> comboBox中
4、validate()函数放在最后,每当容器添加新的组件或者移除时,可保证容器中的组件的正确显示
5、设置JFrame背景色:下列代码运行后看不出颜色???
JFrame当中会默认使用流式布局管理器(FlowLayout)将整个窗体进行覆盖操作,也就是说设置的颜色确实是存在的,只是被布局管理器给覆盖住了,所以无法看见。
解决:在JFrame当中添加一个面板容器,使得面板容器对窗体形成一个覆盖后,直接设置面板的背景颜色就可以达到相当于对窗体背景颜色进行设置的效果,然后将组建都添加到面板容器当中去。
简单布局实例:
import java.awt.*
import javax.swing.*;
public class Test {
public static void main(String[] args) {
Winn win1=new Winn("Hello");
}
}
class Winn extends JFrame{
public Winn(String title) {
setTitle(title);
setSize(300,500);
setLocationRelativeTo(null);
setDefaultCloseOperation(EXIT_ON_CLOSE);
setVisible(true);
init();
setBackground(Color.BLUE);
}
JLabel lb1,lb2,lb3;
JPasswordField passwordField;
JTextField jTextField;
JRadioButton radioButton1;
JRadioButton radioButton2;
ButtonGroup group;//组框,组中的按钮只能选一个,本身不是控件,不能放容器里
JCheckBox box1,box2,box3;
JComboBox<String> comboBox;//泛型
JTextArea textArea;
JButton button1;
public void init() {
setLayout(new FlowLayout());//流布局,Jframe默认布局为边界布局,中间只能添加1个组件
lb1=new JLabel("姓名:");
add(lb1);
jTextField=new JTextField("name",10);
add(jTextField);
lb2=new JLabel("性别:");
add(lb2);
radioButton1=new JRadioButton("男");
radioButton2=new JRadioButton("女");
//生成实例之后应用!!!!
group=new ButtonGroup();
group.add(radioButton1);
group.add(radioButton2);
add(radioButton1);add(radioButton2);
lb3=new JLabel("密码:");
add(lb3);
passwordField=new JPasswordField("234", 5);
add(passwordField);
box1=new JCheckBox("看书");
box2=new JCheckBox("睡觉");
box3=new JCheckBox("跑步");
add(box1);add(box2);add(box3);
String[] combMsg= {"选项1","选项2","选项3"};
comboBox=new JComboBox<>(combMsg);
add(comboBox);
textArea=new JTextArea( 10, 10);
add(textArea);
button1=new JButton("提交");
add(button1);
validate();
}
}
在init()函数中添加一下代码:
Container pane=this.getContentPane();
//在窗体当中添加一个面板操作,并进行对象的上转型操作。使得窗体面板容器占满整个窗体容器
pane.setBackground(Color.BLUE);//直接对窗体面板当中的背景颜色进行设置就行
其他组件注意点:
1、单选框锁定在一个ButtonGroup中,可实现单选功能():
ButtonGroup group=new ButtonGroup();
group.add(radioButton1);
group.add(radioButton2);
add(radioButton1);add(radioButton2);
2、还有密码框这个组件的哦~
passwordField=new JPasswordField("234", 5); 分别设置密码初始值和密码长度
3、变态的多选框:
声明的时候声明其泛型的类型,然后再~~~嘿嘿~~~生成一个对应类型的数组,赋给多选框对象:
JComboBox<String> comboBox;//泛型
String[] combMsg= {"选项1","选项2","选项3"};
comboBox=new JComboBox<>(combMsg);
add(comboBox);
4、神奇的validate()函数,实时更新,保证容器中的组件能正确显示出来
附:以下效果图的代码:
import java.awt.*;
import javax.swing.*;
public class Test {
public static void main(String[] args) {
Winn win1=new Winn("Hello");
}
}
class Winn extends JFrame{
public Winn(String title) {
setTitle(title);
setSize(300,500);
setLocationRelativeTo(null);
setDefaultCloseOperation(EXIT_ON_CLOSE);
setVisible(true);
init();
}
JLabel lb1,lb2,lb3;
JPasswordField passwordField;
JTextField jTextField;
JRadioButton radioButton1;
JRadioButton radioButton2;
ButtonGroup group;//组框,组中的按钮只能选一个,本身不是控件,不能放容器里
JCheckBox box1,box2,box3;
JComboBox<String> comboBox;//泛型
JTextArea textArea;
JButton button1;
public void init() {
Container pane=this.getContentPane();
//在窗体当中添加一个面板操作,并进行对象的上转型操作。使得窗体面板容器占满整个窗体容器
pane.setBackground(Color.lightGray);//直接对窗体面板当中的背景颜色进行设置就行
setLayout(new FlowLayout());//流布局,Jframe默认布局为边界布局,中间只能添加1个组件
lb1=new JLabel("姓名:");
add(lb1);
jTextField=new JTextField("name",10);
add(jTextField);
lb2=new JLabel("性别:");
add(lb2);
radioButton1=new JRadioButton("男");
radioButton2=new JRadioButton("女");
//生成实例!!!!
group=new ButtonGroup();
group.add(radioButton1);
group.add(radioButton2);
add(radioButton1);add(radioButton2);
lb3=new JLabel("密码:");
add(lb3);
passwordField=new JPasswordField("234", 5);
add(passwordField);
box1=new JCheckBox("看书");
box2=new JCheckBox("睡觉");
box3=new JCheckBox("跑步");
add(box1);add(box2);add(box3);
String[] combMsg= {"选项1","选项2","选项3"};
comboBox=new JComboBox<>(combMsg);
add(comboBox);
textArea=new JTextArea( 10, 10);
add(textArea);
button1=new JButton("提交");
add(button1);
validate();
}
}