星空网 > 软件开发 > Java

JFrome 登陆/注册/回显无数据库连接小程序

当离开RCP插件区重新回顾一下JFrame窗口程序的标签、页面间的跳转。

完成一个登陆、注册界面。(界面完成后练习输入输出流,将前台的注册信息保存到一个文件夹下的.txt文件中)

首先向通过JFrame写出一个登陆窗体来,然后给注册按钮绑定actionListener监听。

以下为login页面

 1 package demo; 2  3 import java.awt.GridLayout; 4 import java.awt.event.ActionEvent; 5 import java.awt.event.ActionListener; 6  7 import javax.swing.JButton; 8 import javax.swing.JFrame; 9 import javax.swing.JLabel;10 import javax.swing.JPanel;11 import javax.swing.JPasswordField;12 import javax.swing.JTextField;13 //导入必要的包14 15 public class Demo_login extends JFrame{16  17   JTextField jTextField ; //定义文本框组件18   JPasswordField jPasswordField; //定义密码框组件19   JLabel jLabel1,jLabel2;20   JPanel jp1,jp2,jp3;21   JButton jb1,jb2,jb3; //创建按钮22   public Demo_login(){23     jTextField = new JTextField(12);24     jPasswordField = new JPasswordField(13);25     jLabel1 = new JLabel("用户名");26     jLabel2 = new JLabel("密码");27     jb1 = new JButton("确认");28     jb2 = new JButton("取消");29     jb3 = new JButton("注册");30     jp1 = new JPanel();31     jp2 = new JPanel();32     jp3 = new JPanel();33     34     //设置布局  35     this.setLayout(new GridLayout(3,1));36     37     jp1.add(jLabel1); 38     jp1.add(jTextField);//第一块面板添加用户名和文本框 39     40     jp2.add(jLabel2);41     jp2.add(jPasswordField);//第二块面板添加密码和密码输入框42     43     jp3.add(jb1);44     jp3.add(jb2);45     jp3.add(jb3);//第三块面板添加确认和取消46     jb3.addActionListener(new ActionListener() {47       @Override48       public void actionPerformed(ActionEvent e) {49         Demo_add();  50         }51 52       public void Demo_add() {53         new Demo_add();54         55       }56     });57    58   59    60     //    jp3.setLayout(new FlowLayout());   //因为JPanel默认布局方式为FlowLayout,所以可以注销这段代码.61     this.add(jp1);62     this.add(jp2);63     this.add(jp3); //将三块面板添加到登陆框上面64     //设置显示65     this.setSize(300, 200);66     //this.pack();67     this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);68     this.setVisible(true);69     this.setTitle("登陆");70     71   }72   public static void main(String[] args){73     new Demo_login();74     75   }76 }

下一步规划注册信息和完成注册界面

 1 package demo; 2  3  4 import java.awt.Color; 5 import java.awt.FlowLayout; 6 import java.awt.Graphics; 7 import java.awt.Image; 8 import java.awt.event.ActionEvent; 9 import java.awt.event.ActionListener; 10  11 import javax.swing.Box; 12 import javax.swing.ButtonGroup; 13 import javax.swing.ImageIcon; 14 import javax.swing.JButton; 15 import javax.swing.JFrame; 16 import javax.swing.JLabel; 17 import javax.swing.JPanel; 18 import javax.swing.JRadioButton; 19 import javax.swing.JTextArea; 20 import javax.swing.JTextField; 21  22  23 public class Demo_add extends JFrame{ 24  25   public static void main(String[] args) { 26     new Demo_add(); 27   } 28  29   String [][] arr = new String [100][5]; 30   JLabel id,name,age, gender,introduction; 31   ButtonGroup bg; 32   JRadioButton female,male; 33   JTextArea textArea; 34   JTextField tf_id,tf_name,tf_age,jf_introduction; 35   JButton btn_register; 36   MyPanel jp; 37   Box box_id,box_name,box_age,box_gender,box_introduction,baseBox; 38  39   public Demo_add() { 40  41     setVisible(true); 42     setSize(400, 450); 43     setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 44     instantiateFunction(); 45     addFunction(); 46      47  48   } 49  50   public void instantiateFunction(){ 51     jp = new MyPanel(new ImageIcon("./src/picture/pic16.jpg").getImage()); 52     baseBox = Box.createVerticalBox(); 53     box_id = Box.createHorizontalBox(); 54     box_name = Box.createHorizontalBox(); 55     box_age = Box.createHorizontalBox(); 56     box_gender = Box.createHorizontalBox(); 57     box_introduction = Box.createHorizontalBox(); 58     btn_register = new JButton("注册");   59     tf_id = new JTextField(10); 60     tf_name = new JTextField(10); 61     tf_age = new JTextField(10); 62     bg = new ButtonGroup(); 63     female = new JRadioButton("女"); 64     male = new JRadioButton("男"); 65     textArea = new JTextArea(9,20); 66     listenerFunction(); 67  68   } 69  70   /** 71    * 实现页面跳转和数组传递 72    * */ 73  74   public void listenerFunction(){ 75     btn_register.addActionListener(new ActionListener() { 76  77       @Override 78       public void actionPerformed(ActionEvent e) { 79          80         for (int i = 0; i < arr.length; i++) { 81           //先做一个判断,如果ID不为空,则传过去 82           if(arr[i][0]==null || arr[i][0].equals("")){ 83             arr[i][0] =tf_id.getText(); 84             arr[i][1] =tf_name.getText(); 85             arr[i][2] =tf_age.getText(); 86              87             if(male.isSelected()){ 88               arr[i][3] = male.getText(); 89             }else{ 90               arr[i][3] = female.getText(); 91             } 92             arr[i][4] =textArea.getText(); 93              94             break; 95           } 96         } 97         new Demo_addMessage(arr); 98  99       }100     });101   }102 103   public void addFunction(){104     add(jp);105     box_id.add(new JLabel("ID:    "));106     box_id.add(tf_id);107 108     box_name.add(new JLabel("姓名: "));109     box_name.add(tf_name);110 111     box_age.add(new JLabel("年龄: "));112     box_age.add(tf_age);113     114     box_gender.add(new JLabel("性别: "));115     bg.add(male);116     bg.add(male);117     box_gender.add(male);118     box_gender.add(female);  119     120     box_introduction.add(textArea);121 122 123     baseBox.add(Box.createVerticalStrut(15));124     baseBox.add(box_id);125     baseBox.add(Box.createVerticalStrut(15));126     baseBox.add(box_name);127     baseBox.add(Box.createVerticalStrut(15));128     baseBox.add(box_age);129     baseBox.add(Box.createVerticalStrut(15));130     baseBox.add(box_gender);131     baseBox.add(Box.createVerticalStrut(15));132     baseBox.add(new JLabel("个人简介: "));133     baseBox.add(box_introduction);134     baseBox.add(Box.createVerticalStrut(15));135     baseBox.add(btn_register);136     jp.add(baseBox);137     138   }139 }140 141 class MyPanel extends JPanel{142   Image img;143   public MyPanel(Image img) {144     this.img = img;145   }146   @Override147   protected void paintComponent(Graphics g) {148     super.paintComponent(g);149     g.drawImage(img,0,0, this.getWidth(), this.getHeight(), this);150   }151 }

正常开发中注册一般会伴随着回显信息的,因为是对JFrame的熟悉所以数据不是来自数据库,我们直接对这个界面中输入的数据回显

 1 package demo; 2  3  4  5  6  7  8 import javax.swing.ImageIcon; 9 import javax.swing.JFrame;10 import javax.swing.JPanel;11 import javax.swing.JScrollPane;12 import javax.swing.JTable;13 14 15 16 public class Demo_addMessage extends JFrame {17 18   19   JTable table;20   JPanel jp;21   //MyDate model;22   JTable tabel;23   ImageIcon img1,img2,img3;24   25   public Demo_addMessage(String [][] arr) {26     System.out.println("跳过来了");27     setVisible(true);28     setSize(500, 500);29     setTitle("保存信息界面");30     setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);31     init(arr);32 33   }34 35   36   private void init(String [][] arr) {37     //标题数组38         String [] columns = {"ID","姓名","年龄","性别","个人简介"};39 40         //项目的相对路径./代表项目名称41         /*img1 = new ImageIcon("./src/picture/dm.png");42         img2 = new ImageIcon("./src/picture/pic12.jpg");43         img3 = new ImageIcon("./src/picture/pic2.jpg");*/44         //model = new MyDate(arr,columns);45         jp = new JPanel();46         table = new JTable(arr,columns);//没有图片时的实例化47         //table = new JTable(model);//存在图片时的实例化48         table.setRowHeight(20);49 50         JScrollPane scrollPanel = new JScrollPane(table);51         jp.add(scrollPanel);52         add(jp);53     54   }55 56 }

到这儿JFrame的界面就简单完成了,下一步就是注册界面练习输入输出流。详情关注下一篇随笔~

JFrome 登陆/注册/回显无数据库连接小程序




原标题:JFrome 登陆/注册/回显无数据库连接小程序

关键词:数据库

*特别声明:以上内容来自于网络收集,著作权属原作者所有,如有侵权,请联系我们: admin#shaoqun.com (#换成@)。
相关文章
我的浏览记录
最新相关资讯
海外公司注册 | 跨境电商服务平台 | 深圳旅行社 | 东南亚物流