当前位置:编程学习 > JAVA >>

java登入界面代码补充完整

我的图形界面运行已经可以用了,但是 我编写事件的时候又出现很多问题,我只想让大家在我以有的基础上帮我实现,点击确定弹出主界面。退出关闭界面,我的图形界面已经没问题,关键就是事件实现不了希望达人努力帮我补充完整我要交作业。在线等,达人编译通过有效果了给我源代码哦import java.util.*;import javax.swing.*;public class Test extends JFrame implements ActionListener{//以下只是定义一些变量 private JPanel pan; private JLabel labName; private JTextField tfName; private JLabel labPass; private JPasswordField tfPass; private JButton butConfirm; private JButton tuichu; private ImageIcon ii; private JLabel lab;   Test(){  this.setBounds(600, 600, 710, 500);  pan  = new JPanel();  labName = new JLabel("用户名");  tfName = new JTextField(20);  labPass = new JLabel("密     码");  tfPass = new JPasswordField(20);  butConfirm = new JButton("登入");  tuichu=new JButton("退出");  pan.add(labName);  pan.add(tfName);  pan.add(labPass);  pan.add(tfPass);  pan.add(butConfirm);  pan.add(tuichu);//退出安按钮  ii  = new ImageIcon("good.jpg");//你只需要把这里的图片1.jpg给改了就可以了  lab  = new JLabel(ii);  lab.setBounds(0, 0,ii.getIconWidth(), ii.getIconHeight());  this.getLayeredPane().setLayout(null);  this.getLayeredPane().add(lab, new Integer(Integer.MIN_VALUE));  this.setContentPane(pan);  pan.setOpaque(false);  this.setVisible(true);  this.setTitle("福软《学生学籍管理系统》");
  this.setSize(600,400);  this.setVisible(true);  this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);  this.setResizable(false);  butConfirm.addActionListener(this);//确定按钮注册监听器  tuichu.addActionListener(this);//退出按钮注册监听器
 }
  public void actionPerformed(ActionEvent e) //这个函数,因为继承了ActionListener接口,所以必须实现
 {   if(e.getSource()==butConfirm)//当单击确定时做下面事情     {     //当==什么时登入         }
    else if(e.getSource()==tuichu)    {    // 怎么退出    }    }    

   public static void main(String args[]) {  new Test(); }

}
答案:import java.awt.AWTEvent;
import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.Frame;
import java.awt.GridLayout;
import java.awt.Point;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowEvent;

import javax.swing.BorderFactory;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JPasswordField;
import javax.swing.JTextField;

public class Test extends JFrame implements ActionListener {
 /**
  *
  */
 private static final long serialVersionUID = -90081696130943194L;
 // 以下只是定义一些变量
 private JPanel pan;
 private JLabel labName;
 private JTextField tfName;
 private JLabel labPass;
 private JPasswordField tfPass;
 private JButton butConfirm;
 private JButton tuichu;
 private ImageIcon ii;
 private JLabel lab;

 Test() {
  this.setBounds(600, 600, 710, 500);
  pan = new JPanel();
  labName = new JLabel("用户名");
  tfName = new JTextField(20);
  labPass = new JLabel("密     码");
  tfPass = new JPasswordField(20);
  butConfirm = new JButton("登入");
  tuichu = new JButton("退出");
  pan.add(labName);
  pan.add(tfName);
  pan.add(labPass);
  pan.add(tfPass);
  pan.add(butConfirm);
  pan.add(tuichu);
  // 退出安按钮
  ii = new ImageIcon("good.jpg");
  // 你只需要把这里的图片1.jpg给改了就可以了
  lab = new JLabel(ii);
  lab.setBounds(0, 0, ii.getIconWidth(), ii.getIconHeight());
  this.getLayeredPane().setLayout(null);
  this.getLayeredPane().add(lab, new Integer(Integer.MIN_VALUE));
  this.setContentPane(pan);
  pan.setOpaque(false);
  this.setVisible(true);
  this.setTitle("福软《学生学籍管理系统》");

  this.setSize(600, 400);
  this.setVisible(true);
  this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  this.setResizable(false);
  butConfirm.addActionListener(this);// 确定按钮注册监听器
  tuichu.addActionListener(this);// 退出按钮注册监听器
 }

 public void actionPerformed(ActionEvent e) // 这个函数,因为继承了ActionListener接口,所以必须实现
 {
  if (e.getSource() == butConfirm)// 当单击确定时做下面事情
  { // 当==什么时登入

   String passwd = new String(tfPass.getPassword());
   if (tfName.getText().equals("abc") && passwd.equals("123")) {
    AboutBox dlg = new AboutBox(this);
    Dimension dlgSize = dlg.getPreferredSize();
    Dimension frmSize = getSize();
    Point loc = getLocation();
    dlg.setLocation((frmSize.width - dlgSize.width) / 2 + loc.x,
      (frmSize.height - dlgSize.height) / 2 + loc.y);
    dlg.setModal(true);
    dlg.pack();
    dlg.show();
   }
  } else if (e.getSource() == tuichu) { // 怎么退出
   System.exit(0);
  }
 }

 public static void main(String args[]) {
  new Test();
 }

}

class AboutBox extends JDialog implements ActionListener {

 private static final long serialVersionUID = 1L;
 private JPanel panel1 = new JPanel();
 private JPanel panel2 = new JPanel();
 private JPanel insetsPanel1 = new JPanel();
 private JPanel insetsPanel2 = new JPanel();
 private JPanel insetsPanel3 = new JPanel();
 private JButton button1 = new JButton();
 private JLabel imageLabel = new JLabel();
 private JLabel label1 = new JLabel();
 private JLabel label2 = new JLabel();
 private JLabel label3 = new JLabel();
 private JLabel label4 = new JLabel();
 private BorderLayout borderLayout1 = new BorderLayout();
 private BorderLayout borderLayout2 = new BorderLayout();
 private FlowLayout flowLayout1 = new FlowLayout();
 private GridLayout gridLayout1 = new GridLayout();
 private String product = "";
 private String version = "1.0";
 private String copyright = "Copyright (c) 2011";
 private String comments = "";

 public AboutBox(Frame parent) {
  super(parent);
  enableEvents(AWTEvent.WINDOW_EVENT_MASK);
  try {
   jbInit();
  } catch (Exception e) {
   e.printStackTrace();
  }
 }

 // Component initialization
 private void jbInit() throws Exception {
  // imageLabel.setIcon(new
  // ImageIcon(AboutBox.class.getResource("[Your Image]")));
  this.setTitle("About");
  panel1.setLayout(borderLayout1);
  panel2.setLayout(borderLayout2);
  insetsPanel1.setLayout(flowLayout1);
  insetsPanel2.setLayout(flowLayout1);
  insetsPanel2.setBorder(BorderFactory.createEmptyBorder(10, 10, 10, 10));
  gridLayout1.setRows(4);
  gridLayout1.setColumns(1);
  label1.setText(product);
  label2.setText(version);
  label3.setText(copyright);
  label4.setText(comments);
  insetsPanel3.setLayout(gridLayout1);
  insetsPanel3.setBorder(BorderFactory.createEmptyBorder(10, 60, 10, 10));
  button1.setText("Ok");
  button1.addActionListener(this);
  insetsPanel2.add(imageLabel, null);
  panel2.add(insetsPanel2, BorderLayout.WEST);
  this.getContentPane().add(panel1, null);
  insetsPanel3.add(label1, null);
  insetsPanel3.add(label2, null);
  insetsPanel3.add(label3, null);
  insetsPanel3.add(label4, null);
  panel2.add(insetsPanel3, BorderLayout.CENTER);
  insetsPanel1.add(button1, null);
  panel1.add(insetsPanel1, BorderLayout.SOUTH);
  panel1.add(panel2, BorderLayout.NORTH);
  setResizable(true);
 }

 // Overridden so we can exit when window is closed
 protected void processWindowEvent(WindowEvent e) {
  if (e.getID() == WindowEvent.WINDOW_CLOSING) {
   cancel();
  }
  super.processWindowEvent(e);
 }

 // Close the dialog
 void cancel() {
  dispose();
 }

 // Close the dialog on a button event
 public void actionPerformed(ActionEvent e) {
  if (e.getSource() == button1) {
   cancel();
  }
 }
}

上一个:怎样用java实现打印功能
下一个:java中如何实现Job Scheduing

CopyRight © 2012 站长网 编程知识问答 www.zzzyk.com All Rights Reserved
部份技术文章来自网络,