正文

一个java布局管理的小程序2006-02-16 16:38:00

【评论】 【打印】 【字体: 】 本文链接:http://blog.pfan.cn/weoln/10158.html

分享到:

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class LoginDialog extends JFrame{
 
 /**
  * Method main
  *
  *
  * @param args
  *
  */
  JButton b = new JButton("click me");
  JPanel p = new JPanel();
 
 public static void main(String[] args) {
  // TODO: Add your code here
   LoginDialog frame = new LoginDialog();
 }

 /**
  * Method LoginDialog
  *
  *
  */
 public LoginDialog() {
  // TODO: Add your code here
  final JFrame frame = this;
  this.getContentPane().add(p,BorderLayout.SOUTH);
  p.add(b);
  b.addActionListener(new ActionListener(){
   public void actionPerformed(ActionEvent e){
    showLoginDialog(frame);
    }
   });
  this.setSize(300,200);
  this.setTitle("show dialog");
  this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  this.show();
 } 
 
 void showLoginDialog(JFrame frame)
 {
  JPanel pn= new JPanel(new GridLayout(0,1));
  JTextField tf = new JTextField(2);
  JPasswordField tfPass = new JPasswordField(2);
  pn.add(new JLabel("Username:"));
  pn.add(tf);
  pn.add(new JLabel("Password:"));
  pn.add(tfPass);
  
  //password
  if(JOptionPane.showConfirmDialog(frame,pn
                                   ,"Login",JOptionPane.OK_CANCEL_OPTION
                                   ,JOptionPane.PLAIN_MESSAGE)
                                   !=JOptionPane.OK_CANCEL_OPTION)
      {
       System.out.println("User Name:"+tf.getText());
       System.out.println("Password:"+new String(tfPass.getPassword()));
       }
  }
  
}

阅读(2727) | 评论(0)


版权声明:编程爱好者网站为此博客服务提供商,如本文牵涉到版权问题,编程爱好者网站不承担相关责任,如有版权问题请直接与本文作者联系解决。谢谢!

评论

暂无评论
您需要登录后才能评论,请 登录 或者 注册