| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 2325 人关注过本帖, 1 人收藏
标题:怎么把登陆窗体和主程序窗体连接起来,而且先后运行?
只看楼主 加入收藏
m506435665
Rank: 1
等 级:新手上路
帖 子:4
专家分:0
注 册:2014-12-23
结帖率:0
收藏(1)
已结贴  问题点数:20 回复次数:10 
怎么把登陆窗体和主程序窗体连接起来,而且先后运行?
登陆窗体
package loginwindow;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.util.*;
public class LoginWindow extends JFrame implements ActionListener,ItemListener
{
JLabel user=new JLabel("用 户 名:");
JLabel password=new JLabel("密 码:");
JLabel usertype=new JLabel("用户类型:");
JButton login=new JButton("登陆");
JButton cancel=new JButton("取消");
ButtonGroup buttongroup=new ButtonGroup();
JRadioButton manager=new JRadioButton("管理员");
JRadioButton normaluser=new JRadioButton("普通用户");
JTextField text1=new JTextField(18);
JPasswordField text2=new JPasswordField(18);
public LoginWindow()
{
super("登陆窗口");
setSize(300,200);
setVisible(true);
setResizable(false);
setLayout(new FlowLayout(FlowLayout.CENTER,15,10));
add(user);
add(text1);
add(password);
add(text2);
add(usertype);
add(manager);
add(normaluser);
buttongroup.add(manager);
buttongroup.add(normaluser);
add(login);
add(cancel);
manager.addItemListener(this);
normaluser.addItemListener(this);
login.addActionListener(this);
cancel.addActionListener(this);
Dimension screen=Toolkit.getDefaultToolkit().getScreenSize();
setLocation((screen.width-300)/2,(screen.height-220)/2);
addWindowListener(new WindowAdapter()
{
public void windowClosing(WindowEvent e)
{
dispose();
System.exit(0);
}
});
}
public void actionPerformed(ActionEvent e)
{
if(e.getSource()==login)
{
;
}
else if(e.getSource()==cancel)
{
dispose();
System.exit(0);
}
}
public void itemStateChanged(ItemEvent e)
{
if(e.getSource()==manager)
{
;
}
else if(e.getSource()==normaluser)
{
;
}
}
public static void main(String args[])
{
new LoginWindow();
}
}



主程序
package studentmanager;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import *;
import java.util.*;

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.util.*;

class Student implements {
    String number,name,specialty,grade,thing,sex,dorm;
    public Student(){};
    public void setNumber(String number){ this.number=number;}
    public String getNumber(){ return number;}
    public void setName(String name){ this.name=name;}
    public String getName(){ return name;}
    public void setSex(String sex){ this.sex=sex;}
    public String getSex(){ return sex;}
    public void setSpecialty(String specialty){ this.specialty=specialty;}
    public String getSpecialty(){ return specialty;}
    public void setGrade(String grade){ this.grade=grade;}
    public String getGrade(){ return grade;}
    public void setthing(String thing){ this.thing=thing;}
    public String getthing(){ return thing;}
    public void setDorm(String dorm){ this.dorm=dorm;}
    public String getDorm(){ return dorm;}
   
}
public class StudentManager extends JFrame{
    JLabel lb=new JLabel("录入请先输入记录,查询、删除请先输入学号,修改是对查询" +
            "内容改后的保存!");
    JTextField 学号,姓名,专业,年级,物品,宿舍;
    JRadioButton 男,女;
    ButtonGroup group=null;
    JButton 录入,查询,删除,修改,显示;
    JPanel p1,p2,p3,p4,p5,p6,p7,pv,ph;
    Student 学生=null;
    Hashtable 学生散列表=null;
    File file=null;
    FileInputStream inOne=null;
    ObjectInputStream inTwo=null;
    FileOutputStream outOne=null;
    ObjectOutputStream outTwo=null;
    public StudentManager(){
      super("学生基本信息管理系统");
      学号=new JTextField(10);
      姓名=new JTextField(10);
      专业=new JTextField(10);
      年级=new JTextField(10);
      物品=new JTextField(10);
      宿舍=new JTextField(10);
      group=new ButtonGroup();
      男=new JRadioButton("男",true);
      女=new JRadioButton("女",false);
      group.add(男);
      group.add(女);
      录入=new JButton("录入");
      查询=new JButton("查询");
      删除=new JButton("删除");
      修改=new JButton("修改");
      显示=new JButton("显示");
      录入.addActionListener(new InputAct());
      查询.addActionListener(new InquestAct());
      修改.addActionListener(new ModifyAct());
      删除.addActionListener(new DeleteAct());
      显示.addActionListener(new ShowAct());
      修改.setEnabled(false);
      p1=new JPanel();
      p1.add(new JLabel("学号:",JLabel.CENTER));
      p1.add(学号);
      p2=new JPanel();
      p2.add(new JLabel("姓名:",JLabel.CENTER));
      p2.add(姓名);
      p3=new JPanel();
      p3.add(new JLabel("性别:",JLabel.CENTER));
      p3.add(男);
      p3.add(女);
      p4=new JPanel();
      p4.add(new JLabel("专业:",JLabel.CENTER));
      p4.add(专业);
      p5=new JPanel();
      p5.add(new JLabel("年级:",JLabel.CENTER));
      p5.add(年级);
      p6=new JPanel();
      p6.add(new JLabel("物品:",JLabel.CENTER));
      p6.add(物品);
      p7=new JPanel();
      p7.add(new JLabel("宿舍:",JLabel.CENTER));
      p7.add(宿舍);
      
      pv=new JPanel();
      pv.setLayout(new GridLayout(7,1));
      pv.add(p1);
      pv.add(p2);
      pv.add(p3);
      pv.add(p4);
      pv.add(p5);
      pv.add(p6);
      pv.add(p7);
      ph=new JPanel();
      ph.add(录入);
      ph.add(查询);
      ph.add(修改);
      ph.add(删除);
      ph.add(显示);
      file=new File("学生信息.txt");
      学生散列表=new Hashtable();
        if(!file.exists()){
            try{
                FileOutputStream out=new FileOutputStream(file);
                ObjectOutputStream objectOut=new ObjectOutputStream(out);
                objectOut.writeObject(学生散列表);
                objectOut.close();
                out.close();
            }
            catch(IOException e){}
        }
        Container con=getContentPane();
        con.setLayout(new BorderLayout());
        con.add(lb, BorderLayout.NORTH);
        con.add(pv, BorderLayout.CENTER);
        con.add(ph, BorderLayout.SOUTH);
        setDefaultCloseOperation(EXIT_ON_CLOSE);
        setBounds(100,100,600,300);
        setVisible(true);
    }
    public static void main(String[] args) {new StudentManager();}
    class InputAct implements ActionListener{
        public void actionPerformed(ActionEvent e){
           修改.setEnabled(false);
           String number="";
           number=学号.getText();
          if(number.length()>0){
              try{
                  inOne=new FileInputStream(file);
                  inTwo=new ObjectInputStream(inOne);
                  学生散列表=(Hashtable)inTwo.readObject();
                  inOne.close();
                  inTwo.close();
              }
              catch(Exception ee){System.out.println("创建散列表出现问题!");}
              if(学生散列表.containsKey(number)){
                  String warning="该生信息已存在,请到修改页面修改!";
                  JOptionPane.showMessageDialog(null,warning,"警告",
                          JOptionPane.WARNING_MESSAGE);
              }//end if1
              else{
                  String m="该生信息将被录入!";
                  int ok=JOptionPane.showConfirmDialog(null,m,"确认",
                          JOptionPane.YES_NO_OPTION,JOptionPane.INFORMATION_MESSAGE);
                  if(ok==JOptionPane.YES_OPTION){
                      String name=姓名.getText();
                      String specialty=专业.getText();
                      String grade=年级.getText();
                      String borth=物品.getText();
                      String dorm=宿舍.getText();
                      String sex=null;
                      if(男.isSelected()){sex=男.getText();}
                      else{sex=女.getText();}
                      学生=new Student();
                      学生.setNumber(number);
                      学生.setName(name);
                      学生.setSpecialty(specialty);
                      学生.setGrade(grade);
                      学生.setthing(borth);
                      学生.setSex(sex);
                      学生.setDorm(dorm);
                      try{
                          outOne=new FileOutputStream(file);
                          outTwo=new ObjectOutputStream(outOne);
                          学生散列表.put(number,学生);
                          outTwo.writeObject(学生散列表);
                          outTwo.close();
                          outOne.close();
                      }
                      catch(Exception ee){System.out.println("输出散列表出现问题!");}
                      学号.setText(null);
                      姓名.setText(null);
                      专业.setText(null);
                      年级.setText(null);
                      物品.setText(null);
                      宿舍.setText(null);
                  }
              }//end else1
          }//end if0
          else{
              String warning="必须输入学号!";
              JOptionPane.showMessageDialog(null,warning,
                      "警告",JOptionPane.WARNING_MESSAGE);
          }//end else0
      }//end actionPerformed
  }//end class
    class InquestAct implements ActionListener{
        public void actionPerformed(ActionEvent e){
           String number="";
           number=学号.getText();
          if(number.length()>0){
              try{
                  inOne=new FileInputStream(file);
                  inTwo=new ObjectInputStream(inOne);
                  学生散列表=(Hashtable)inTwo.readObject();
                  inOne.close();
                  inTwo.close();
              }
              catch(Exception ee){System.out.println("散列表有问题!");}
              if(学生散列表.containsKey(number)){
                修改.setEnabled(true);
                Student stu=(Student)学生散列表.get(number);
                姓名.setText(stu.getName());
                专业.setText(stu.getSpecialty());
                年级.setText(stu.getGrade());
                物品.setText(stu.getthing());
                宿舍.setText(stu.getDorm());
                if(stu.getSex().equals("男")){男.setSelected(true);}
                else{女.setSelected(true);}
              }
              else{
                  修改.setEnabled(false);
                  String warning="该学号不存在!";
              JOptionPane.showMessageDialog(null,warning,
                      "警告",JOptionPane.WARNING_MESSAGE);
              }
          }
          else{
          修改.setEnabled(false);
          String warning="必须输入学号!";
              JOptionPane.showMessageDialog(null,warning,
                      "警告",JOptionPane.WARNING_MESSAGE);
          }
        }
    }
     class ModifyAct implements ActionListener{
       public void actionPerformed(ActionEvent e){
           String number=学号.getText();
           String name=姓名.getText();
           String specialty=专业.getText();
           String grade=年级.getText();
           String thing=物品.getText();
           String dorm=宿舍.getText();
           String sex=null;
           if(男.isSelected()){sex=男.getText();}
           else{sex=女.getText();}
           Student 学生=new Student();
           学生.setNumber(number);
           学生.setName(name);
           学生.setSpecialty(specialty);
           学生.setGrade(grade);
           学生.setthing(thing);
           学生.setSex(sex);
           学生.setDorm(dorm);
           try{
               outOne=new FileOutputStream(file);
               outTwo=new ObjectOutputStream(outOne);
               学生散列表.put(number, 学生);
               outTwo.writeObject(学生散列表);
               outTwo.close();
               outOne.close();
               学号.setText(null);
               姓名.setText(null);
               专业.setText(null);
               年级.setText(null);
               物品.setText(null);
               宿舍.setText(null);
           }
           catch(Exception ee){
               System.out.println("录入修改出现异常!");
               修改.setEnabled(false);
           }
       }
   }
   class DeleteAct implements ActionListener{
       public void actionPerformed(ActionEvent e){
           修改.setEnabled(false);
           String number=学号.getText();
          if(number.length()>0){
              try{
                  inOne=new FileInputStream(file);
                  inTwo=new ObjectInputStream(inOne);
                  学生散列表=(Hashtable)inTwo.readObject();
                  inOne.close();
                  inTwo.close();
              }
              catch(Exception ee){}
              if(学生散列表.containsKey(number)){
                Student stu=(Student)学生散列表.get(number);
                姓名.setText(stu.getName());
                专业.setText(stu.getSpecialty());
                年级.setText(stu.getGrade());
                物品.setText(stu.getthing());
                if(stu.getSex().equals("男")){男.setSelected(true);}
                else{女.setSelected(true);}
              }
              String m="确定要删除该学生的记录吗?";
              int ok=JOptionPane.showConfirmDialog(null,m,"确认",
                 JOptionPane.YES_NO_OPTION,JOptionPane.QUESTION_MESSAGE);
              if(ok==JOptionPane.YES_OPTION){
                  学生散列表.remove(number);
                  try{
                      outOne=new FileOutputStream(file);
                      outTwo=new ObjectOutputStream(outOne);
                      outTwo.writeObject(学生散列表);
                      outTwo.close();
                      outOne.close();
                      学号.setText(null);
                      姓名.setText(null);
                      专业.setText(null);
                      年级.setText(null);
                      物品.setText(null);
                      宿舍.setText(null);
                  }
                  catch(Exception ee){System.out.println(ee);}
              }
              else if(ok==JOptionPane.NO_OPTION){
                  学号.setText(null);
                  姓名.setText(null);
                  专业.setText(null);
                  年级.setText(null);
                  物品.setText(null);
                  宿舍.setText(null);
              }
              else{
                  String warning="该学号不存在!";
                  JOptionPane.showMessageDialog(null,warning,
                          "警告",JOptionPane.WARNING_MESSAGE);
              }
          }
          else{
               String warning="必须输入学号!";
               JOptionPane.showMessageDialog(null,warning,
                      "警告",JOptionPane.WARNING_MESSAGE);
          }
       }
   }
   class ShowAct implements ActionListener{
       public void actionPerformed(ActionEvent e){
           new StudentShow(file);
       }
   }
   class StudentShow extends JDialog{
       Hashtable 学生散列表= null;
       JTextArea 显示=null;
       FileInputStream inOne=null;
       ObjectInputStream inTwo=null;
       File file=null;
       public StudentShow(File file){
           super(new JFrame(),"显示对话框");
           this.file=file;
           显示=new JTextArea(16,30);
           try{
               inOne=new FileInputStream(file);
               inTwo=new ObjectInputStream(inOne);
               学生散列表=(Hashtable)inTwo.readObject();
               inOne.close();
               inTwo.close();
           }
           catch(Exception ee){}
           if(学生散列表.isEmpty())显示.append("目前还没有学生的信息记录!\n");
           else{
               显示.setText("学号 姓名 性别 专业 年级 物品 宿舍\n");
               for(Enumeration enm=学生散列表.elements();enm.hasMoreElements();){
                   Student stu=(Student)enm.nextElement();
                   String sex="";
                   if(stu.getSex().equals("男"))sex="男";
                   else sex="女";
                   String str=stu.getNumber()+","+stu.getName()+","+sex+","
                           +stu.getSpecialty()+","+stu.getGrade()+","+stu.getthing()+","+stu.getDorm()+"\n";
                   显示.append(str);
               }
           }
           JScrollPane scroll=new JScrollPane(显示);
           Container con=getContentPane();
           con.add("Center",scroll);
           con.validate();
           setVisible(true);
           setBounds(200,200,400,300);
           addWindowListener(new WindowAdapter(){
               public void windowClosing(WindowEvent e){setVisible(false);}
           }
           );
       }
   }
}
搜索更多相关主题的帖子: password manager package public cancel 
2014-12-23 10:30
liucao
Rank: 10Rank: 10Rank: 10
来 自:恶灵之城
等 级:贵宾
威 望:13
帖 子:538
专家分:1575
注 册:2014-8-6
收藏
得分:7 
你这是要用纯Java代码写么?弄个jsp页面去跳转啊

one car come one car go ,two car peng peng people die.
2014-12-23 11:14
m506435665
Rank: 1
等 级:新手上路
帖 子:4
专家分:0
注 册:2014-12-23
收藏
得分:0 
回复 2楼 liucao
能不能期中添加一个监听的东西,还有这两个都是窗口实得,要用JSP的话能不能帮我操作一下?
2014-12-23 11:21
liucao
Rank: 10Rank: 10Rank: 10
来 自:恶灵之城
等 级:贵宾
威 望:13
帖 子:538
专家分:1575
注 册:2014-8-6
收藏
得分:0 
回复 3楼 m506435665
怎么帮你操作?

one car come one car go ,two car peng peng people die.
2014-12-23 12:40
C_fans
Rank: 2
等 级:论坛游民
帖 子:8
专家分:14
注 册:2014-12-23
收藏
得分:7 
我跟你说吧,我前段时间做了一个ATM取款机模拟程序,也有很多窗口,我是这样处理的,当要转换窗口时,把现在的窗口隐藏掉同时调用另一个窗口就OK了。想要程序的话跟我说!

[ 本帖最后由 C_fans 于 2014-12-23 13:46 编辑 ]
2014-12-23 13:45
m506435665
Rank: 1
等 级:新手上路
帖 子:4
专家分:0
注 册:2014-12-23
收藏
得分:0 
回复 4楼 liucao
帮我改一下代码吧   大神  用JSP让它显示出来吧  谢谢了
2014-12-23 17:08
liucao
Rank: 10Rank: 10Rank: 10
来 自:恶灵之城
等 级:贵宾
威 望:13
帖 子:538
专家分:1575
注 册:2014-8-6
收藏
得分:0 
回复 6楼 m506435665
汗,我可不是大神,你这么弄我也没搞过,我都是写jsp页面,用XML文件配置,跳转传值,再用JDBC这么做的,学的时候就是WEB方向,你这全是直接上Java框体啊

one car come one car go ,two car peng peng people die.
2014-12-23 17:57
m506435665
Rank: 1
等 级:新手上路
帖 子:4
专家分:0
注 册:2014-12-23
收藏
得分:0 
回复 7楼 liucao
昨天交课程设计了,没过关,老师说要BS结构的,我做的窗体是CS结构的,让我们重新做,那些什么都没做的就说运行不了竟然过了,还有交C#的,真悲剧。如果我的这个程序完全用JSP做成BS结构的要怎么做呀 能不能帮我一下 JSP学的很菜
2014-12-24 09:58
wp231957
Rank: 20Rank: 20Rank: 20Rank: 20Rank: 20
来 自:神界
等 级:贵宾
威 望:423
帖 子:13688
专家分:53332
注 册:2012-10-18
收藏
得分:7 
C/S又称Client/Server或客户/服务器模式。服务器通常采用高性能的PC、工作站或小型机,并采用大型数据库系统,如Oracle、Sybase、Informix或 SQL Server。客户端需要安装专用的客户端软件。

B/S是Brower/Server的缩写,客户机上只要安装一个浏览器(Browser),如Netscape Navigator或Internet Explorer,服务器安装Oracle、Sybase、Informix或 SQL Server等数据库。浏览器通过Web Server 同数据库进行数据交互。

DO IT YOURSELF !
2014-12-24 10:00
liucao
Rank: 10Rank: 10Rank: 10
来 自:恶灵之城
等 级:贵宾
威 望:13
帖 子:538
专家分:1575
注 册:2014-8-6
收藏
得分:0 
回复 8楼 m506435665
你让我直接做够呛,我也是年中左右开始才学,得两三天我这什么都不干我能完全搭出来,这样,我把我弄到的资料给你,自学克服下吧,具体有什么问题可以问我

one car come one car go ,two car peng peng people die.
2014-12-24 17:16
快速回复:怎么把登陆窗体和主程序窗体连接起来,而且先后运行?
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.033748 second(s), 7 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved