Swing程序设计
重点类:
- Component:组件类
- Container:容器类
- JComponent:组件父类
常用组件:
例子:
import javax.swing.*;
import java.awt.*;
public class winF {
public static void JF_use_H(JFrame jf){
//设置窗体的位置和大小
jf.setBounds(150,150,500,500);
//获取主容器
Container container = jf.getContentPane();
//设置文本标签
JLabel jl = new JLabel("Enter System");
jl.setHorizontalAlignment(SwingConstants.CENTER);
container.add(jl);
//设置窗口关闭方式
//DO_NOTHING_ON_CLOSE:关闭窗体不触发任何操作即关不上
// HIDE_ON_CLOSE:关闭窗体时仅隐藏不释放资源
//EXIT_ON_CLOSE:彻底关闭
// DISPOSE_ON_CLOSE:关闭窗体时,释放资源,窗体消失但程序不关闭
jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
//设置窗口可视化
jf.setVisible(true);
}
public static void main(String[] args) {
//设置一个叫Enter System的不可见窗体
JFrame jf = new JFrame("Enter System");
winF.JF_use_H(jf);
JDialog jd = new JDialog(jf,"jdialog");
jd.setBounds(200,200,400,200);
jd.setVisible(true);
//JDialog的退出没有EXIT_ON_CLOSE
jd.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
}
}
程序展示:
代码优化:
import javax.swing.*;
import java.util.Scanner;
public class JF_self {
public void JF_S_Form(JFrame jf){
Scanner sc = new Scanner(System.in);
System.out.print("please enter the Form width:");
int Formwidth = sc.nextInt();
System.out.print("please enter the Form height:");
int Formheight = sc.nextInt();
System.out.print("plaese enter the name of the Form\ncalled:");
String Formname = sc.next();
jf.setBounds(100,100,Formwidth,Formheight);
jf.setTitle(Formname);
jf.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
jf.setVisible(true);
}
public static void main(String[] args) {
JFrame jf = new JFrame();
JF_self jsf = new JF_self();
jsf.JF_S_Form(jf);
}
}
程序展示:
JDialog对话框
功能:
从一个窗体中弹出另一个窗体
语法:
JFrame jf = new JFrame("title");
JDialog jd = new JDialog(jf,"jdialog");
JLabel标签
功能:
可以显示文本图标等
构造方法:
- public JLabel(String text,Icon icon,int aligment):创建一个带有文本,图标的标签,并且设置它的对齐方式
- public JLabel(String text,int aligment)
- public JLabel(,Icon icon,int aligment)
- public JLabel()
综合编写:
import javax.swing.*;
import java.awt.*;
public class Jlabel {
public static void main(String[] args) {
JFrame jf = new JFrame("Enter");
//设置为流布局
jf.setLayout(new FlowLayout(FlowLayout.CENTER));
jf.setBounds(200,200,400,400);
//设置标签组件
JLabel jl = new JLabel("账号",SwingConstants.CENTER);
jf.add(jl);
//设置按钮模块
JButton jb1 = new JButton("登录");
JButton jb2 = new JButton("注册");
jf.add(jb1);
jf.add(jb2);
//设置按钮为可用
jb1.setEnabled(true);
jb2.setEnabled(true);
//设置按钮边界显示
jb1.setBorderPainted(true);
jb2.setBorderPainted(true);
//设置单选按钮
JRadioButton jrb1 = new JRadioButton("男");
JRadioButton jrb2 = new JRadioButton("女");
//将单选按钮加入按键组中
ButtonGroup group = new ButtonGroup();
group.add(jrb1);
group.add(jrb2);
jf.add(jrb1);
jf.add(jrb2);
//设置复选框
JCheckBox jcb1 = new JCheckBox("choose1",true);
JCheckBox jcb2 = new JCheckBox("choose2",false);
JCheckBox jcb3 = new JCheckBox("choose3",false);
JCheckBox jcb4 = new JCheckBox("choose4",false);
jf.add(jcb1);
jf.add(jcb2);
jf.add(jcb3);
jf.add(jcb4);
//设置下拉框
JComboBox jcombobox = new JComboBox();
jcombobox.addItem("---请选择学历---");
jcombobox.addItem("本科");
jcombobox.addItem("高中");
jcombobox.addItem("初中");
jf.add(jcombobox);
//设置多级菜单栏
JMenuBar jmb = new JMenuBar();
//设置菜单
JMenu menu = new JMenu("菜单1");
//设置菜单选项
JMenuItem jmi1 = new JMenuItem("选项1");
JMenuItem jmi2 = new JMenuItem("选项2");
menu.add(jmi1);
menu.add(jmi2);
jmb.add(menu);
jf.add(jmb);
//设置文本组件
JTextField jtf = new JTextField("请输入",22);
jf.add(jtf);
//文本组件和标签综合使用
//jf.setLayout(new FlowLayout(FlowLayout.LEFT));
JLabel jl2= new JLabel("账号:");
jf.add(jl2);
JTextField jtf2 = new JTextField("请输入账号",22);
jf.add(jtf2);
//密码框组件
JPasswordField pwd = new JPasswordField("",22);
//设置密码为星号
pwd.setEchoChar('*');
jf.add(pwd);
//文本域组件
JTextArea textArea = new JTextArea(10,10);
//设置文本自动换行
textArea.setLineWrap(true);
jf.add(textArea);
jf.setVisible(true);
jf.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
}
}
程序展示: