个人感觉java的界面开发也算是其优势之一!只要用心,就能做出美轮美奂的界面!下面简单介绍下
java中的简单界面开发是如何实现的!
java提供了Swing开发工具包。它以抽象窗口工具包(AWT)为基础使跨平台应用程序可以使用任何可插
拔的外观风格。Swing开发人员只用很少的代码就可以利用Swing丰富、灵活的功能和模块化组件来创建优雅
的用户界面。
java的图形界面类
1.awt(包名:java.awt) 是在jdk1.4之前推出的图形界面类
2.swing(包名:javax.swing)[轻量级组件]
jdk1.4的时候推出的图形界面类
所有的图形界面类的第一个字母是大写J开头的。
awt严格意义上跨平台性能很差。swing是基于java的组件,所以完全具有跨平台特性。
虽然大家都是说awt好,但是感觉一旦需要跨平台时,swing的优势就体现出来了,这也是java的立足之本!
2.常用的组件类(javax.swing)
容器组件
JFrmae 窗体容器
JPanel 面板容器
常用的元素组件有
JLabel 标签
JTextField 文本输入框
JPasswordField 密码输入框
JComboBox 下拉框
JCheckBox 复选框
JButton 按钮
3.常用的布局类(java.awt)
BorderLayout 边框布局
FlowLayout 流式布局
GridLayout 网格布局
布局方面我们还可以按照一定的需要将界面设置为空布局,只是在设置为空布局之后,需要使用setBounds方法将所有添加的容器组件设定坐标!
如果要做一个简单的界面,我们首先要先定义一个类,在类中定义定义主函数和初始化界面的initGUI()方法。
在主函数中实例化类的对象,然后调用initGUI方法。在initGUI()方法中我们可以按需要添加自己想要添加的容器组件!现附上自己写的代码如下,这是我坐的简单的飞机订票系统的界面,在代码中我使用的就是空布局的方法,并且运用了卡牌布局!
package hangban; import javax.swing.*; import java.awt.CardLayout; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import javax.swing.JFrame; import javax.swing.JMenu; import javax.swing.JMenuBar; import javax.swing.JMenuItem; import javax.swing.JPanel; import javax.swing.event.TableModelListener; import javax.swing.table.TableModel; import java.awt.BorderLayout; import java.awt.Font; import java.awt.List; import java.util.ArrayList; public class Flight extends JFrame { public static void main(String [] args){ Flight dl = new Flight(); dl.initGUI(); } public void initGUI() { //JFrame jf2; // 主界面控件定义 开始 this.setTitle("飞机订票系统"); this.setSize(600, 500); this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); this.setLocation(400, 200); JMenuBar jm = new JMenuBar();// 实例化菜单栏的对象 JMenu jm1 = new JMenu("主界面");// 实例化菜单项的对象 jm.add(jm1);// 添加到菜单栏中 JMenu jm2 = new JMenu("航班信息");// 实例化菜单项的对象 jm.add(jm2);// 添加到菜单栏中 JMenu jm3 = new JMenu("路线查询");// 实例化菜单项的对象 jm.add(jm3);// 添加到菜单栏中 JMenu jm4 = new JMenu("票务管理");// 实例化菜单项的对象 jm.add(jm4);// 添加到菜单栏中 JMenuItem jm11 = new JMenuItem("主界面"); jm1.add(jm11); // 实例化菜单子项的对象 JMenuItem jm21 = new JMenuItem("航班信息录入"); JMenuItem jm22 = new JMenuItem("航班信息浏览"); JMenuItem jm23 = new JMenuItem("航班信息修改"); // 将对象添加到菜单项中 jm2.add(jm21); jm2.add(jm22); jm2.add(jm23); JMenuItem jm31 = new JMenuItem("按班次查询"); JMenuItem jm32 = new JMenuItem("按终点站查询"); jm3.add(jm31); jm3.add(jm32); JMenuItem jm41 = new JMenuItem("售票"); JMenuItem jm42 = new JMenuItem("退票"); jm4.add(jm41); jm4.add(jm42); this.setJMenuBar(jm); final CardLayout card = new CardLayout(); final JPanel jp = new JPanel();//主要的面板,该面板是被设定成卡片布局的面板。 jp.setLayout(card); JPanel j1 = new JPanel(); JPanel j2 = new JPanel(); JPanel j3 = new JPanel(); JPanel j4 = new JPanel(); JPanel j5 = new JPanel(); JPanel j6 = new JPanel(); JPanel j7 = new JPanel(); JPanel j8 = new JPanel(); JPanel j9 = new JPanel();//提供能够翻转的9个面板 JLabel jl12 = new JLabel("欢迎使用飞机订票系统"); j1.add(jl12); jl12.setFont(new Font("宋体", Font.BOLD, 50)); ImageIcon bk = new ImageIcon("3.jpg"); JLabel jl11 = new JLabel(bk); j1.add(jl11); ImageIcon bk1 = new ImageIcon("1.jpg"); JLabel jl21 = new JLabel(bk1); j2.add(jl21); ImageIcon bk2 = new ImageIcon("2.jpg"); JLabel jl22 = new JLabel(bk2); //j3.add(jl22); ImageIcon bk3 = new ImageIcon("4.jpg"); JLabel jl23 = new JLabel(bk3); j4.add(jl23); ImageIcon bk4 = new ImageIcon("5.jpg"); JLabel jl31 = new JLabel(bk4); j5.add(jl31); ImageIcon bk5 = new ImageIcon("6.jpg"); JLabel jl32 = new JLabel(bk5); j6.add(jl32); ImageIcon bk6 = new ImageIcon("7.jpg"); JLabel jl41 = new JLabel(bk6); j7.add(jl41); ImageIcon bk7 = new ImageIcon("8.jpg"); JLabel jl42 = new JLabel(bk7); j8.add(jl42);//添加9个可翻转的卡片,并添加背景图片 jp.add(j1,"11"); jp.add(j2,"21"); jp.add(j3,"22"); jp.add(j4,"23"); jp.add(j5,"31"); jp.add(j6,"32"); jp.add(j7,"41"); jp.add(j8,"42"); jp.add(j9,"5");//给9个卡片面板添加到主面板,并起名字 this.add(jp); //菜单项,主界面事件监听 jm11.addActionListener(new ActionListener(){ public void actionPerformed(ActionEvent e) { card.show(jp,"11"); }}); //菜单项,航班信息录入事件监听器 jm21.addActionListener(new ActionListener(){ public void actionPerformed(ActionEvent e) { JFrame jf = new JFrame(); jf.setTitle("航班浏览"); jf.setSize(700,300); jf.setLocation(200,200); jf.setDefaultCloseOperation(2); jf.setResizable(false);//禁止更改窗口大小 //设置航班信息录入的界面 JButton jb1 = new JButton("录入信息"); jb1.setFont(new Font("宋体", Font.BOLD, 20)); JButton jb2 = new JButton("保存信息"); jb2.setFont(new Font("宋体", Font.BOLD, 20)); JPanel jp24 = new JPanel(); jf.add(jp24,BorderLayout.SOUTH); jp24.add(jb1,BorderLayout.WEST); jp24.add(jb2,BorderLayout.EAST); //运用JTabel,设置表格, String[] columnNames={"班次","起飞时间","起点站","终点站","飞行时间","额定载量","已售票人数"}; final Object[][] rowData={{"1","8:00","天津","广汉","2","145","120"}, {"2","6:30","天津","成都","0.5","140","140"}, {"3","7:00","天津","成都","0.5","140","120"}, {"4","10:00","天津","成都","0.5","140","120"},}; JTable jt=new JTable(rowData,columnNames); JScrollPane sp=new JScrollPane(jt); jt.setRowHeight(35); jf.add(sp); card.show(jp,"21"); //具体航班信息的录入窗口 jb1.addActionListener(new ActionListener(){ public void actionPerformed(ActionEvent e){ JFrame jf1 = new JFrame(); jf1.setTitle("航班信息录入"); jf1.setLayout(null); jf1.setSize(700,200); jf1.setLocation(200,300); jf1.setDefaultCloseOperation(2); jf1.setResizable(false);//禁止更改窗口大小 JLabel jl211 = new JLabel("班次"); jl211.setBounds(0,0,80,50); JLabel jl212 = new JLabel("起飞时间"); jl212.setBounds(100,0,80,50); JLabel jl213 = new JLabel("起点站"); jl213.setBounds(200,0,80,50); JLabel jl214 = new JLabel("终点站"); jl214.setBounds(300,0,80,50); JLabel jl215 = new JLabel("起飞时间"); jl215.setBounds(400,0,80,50); JLabel jl216 = new JLabel("额定载量"); jl216.setBounds(500,0,80,50); JLabel jl217 = new JLabel("已售票人数"); jl217.setBounds(600,0,80,50); final JTextField jt211 = new JTextField(10); jt211.setBounds(0,50,80,50); final JTextField jt212 = new JTextField(10); jt212.setBounds(100,50,80,50); final JTextField jt213 = new JTextField(10); jt213.setBounds(200,50,80,50); final JTextField jt214 = new JTextField(10); jt214.setBounds(300,50,80,50); final JTextField jt215 = new JTextField(10); jt215.setBounds(400,50,80,50); final JTextField jt216 = new JTextField(10); jt216.setBounds(500,50,80,50); final JTextField jt217 = new JTextField(10); jt217.setBounds(600,50,80,50); jf1.add(jt211); jf1.add(jt212); jf1.add(jt213); jf1.add(jt214); jf1.add(jt215); jf1.add(jt216); jf1.add(jt217); jf1.add(jl211); jf1.add(jl212); jf1.add(jl213); jf1.add(jl214); jf1.add(jl215); jf1.add(jl216); jf1.add(jl217); JButton jb2 = new JButton("确定录入"); jb2.setFont(new Font("宋体", Font.BOLD,12)); jb2.setBounds(300,120,100,40); jf1.add(jb2); JButton jb3 = new JButton("添加航班"); jb3.setFont(new Font("宋体", Font.BOLD,12)); jb3.setBounds(200,120,100,40); jf1.add(jb3); jb3.addActionListener(new actionL(jt211,jt212,jt213,jt214,jt215,jt216,jt217){ public void actionPerformed(ActionEvent e) { String str1 = jt211.getText(); String str2 = jt211.getText(); String str3 = jt211.getText(); String str4 = jt211.getText(); String str5 = jt211.getText(); String str6 = jt211.getText(); String str7 = jt211.getText(); Object[][] rowData = new Object[][]{{"1","8:00","天津","广汉","2","145","120"}, {"2","6:30","天津","成都","0.5","140","140"}, {"3","7:00","天津","成都","0.5","140","120"}, {"4","10:00","天津","成都","0.5","140","120"}, {str1,str2,str3,str4,str5,str6,str7} };} }); actionL al = new actionL(jf1); jb2.addActionListener(al); jf1.setVisible(true); }}); jf.setVisible(true); }}); jm22.addActionListener(new ActionListener(){ public void actionPerformed(ActionEvent e) { /*JTable tab=new JTable();//创建一个表格对象 List list = getList();//得到要显示的用户列表对象 User us = new User(list);//创建我们实现的TableModel对象。 tab.setModel(us);//将模型设给表格 j3.add(tab); ArrayList nlist = new ArrayList();*/ String[] columnNames={"班次","起飞时间","起点站","终点站","飞行时间","额定载量","已售票人数"}; Object[][] rowData={{"1","8:00","天津","广汉","2","145","120"}, {"2","6:30","天津","成都","0.5","140","140"}, {"3","7:00","天津","成都","0.5","140","120"}, {"4","10:00","天津","成都","0.5","140","120"},}; JTable jt=new JTable(rowData,columnNames); JScrollPane sp=new JScrollPane(jt); jt.setRowHeight(35); jp.add(sp,"00"); card.show(jp,"22"); } private List getList() { // TODO Auto-generated method stub return null; }}); jm23.addActionListener(new ActionListener(){ public void actionPerformed(ActionEvent e){ card.show(jp,"23"); }}); jm31.addActionListener(new ActionListener(){ public void actionPerformed(final ActionEvent e){ card.show(jp,"5"); }}); jm32.addActionListener(new ActionListener(){ public void actionPerformed(final ActionEvent e){ card.show(jp,"32"); }}); jm41.addActionListener(new ActionListener(){ public void actionPerformed(final ActionEvent e){ card.show(jp,"41"); }}); jm42.addActionListener(new ActionListener(){ public void actionPerformed(final ActionEvent e){ card.show(jp,"42"); }}); //录入班次信息 jm21.addActionListener(new ActionListener(){ public void actionPerformed(ActionEvent e){ }}); this.setVisible(true); } } class actionL implements ActionListener{ JTextField jt; JFrame _jf; public actionL(JFrame jf){ this._jf = jf; } @Override public void actionPerformed(ActionEvent e) { // TODO Auto-generated method stub _jf.dispose(); } public actionL(JTextField jt11,JTextField jt12,JTextField jt13,JTextField jt14,JTextField jt15,JTextField jt16,JTextField jt17){ this.jt = jt11; this.jt = jt12; this.jt = jt13; this.jt = jt14; this.jt = jt15; this.jt = jt16; this.jt = jt17; } public void actionPerformed1(ActionEvent e) { // TODO Auto-generated method stub } }这是本学期期末做的学期末课程设计,因时间不够充裕,遂把用界面化实现所有功能的想法放弃了,目前只能显示界面!稍有不完善! 其中添加了监听器,并且运用了卡牌布局以及空布局方式,虽然结果是失败了,但是那几天我学会了很多东西,并且那种刻苦钻研之后的来的知识已经深深的映在我的脑海中了,换个角度想想,也许我也可以算是成功了!