java dialog 用法_JAVA之JDialog用法详解

package test_1;

import java.awt.*;

import java.awt.event.*;

import javax.swing.*;

public class test23 extends JFrame implements ActionListener{

public test23(){

setTitle("java3");

setSize(300,300);

addWindowListener(new WindowAdapter(){

public void windowClosing(WindowEvent e){

System.exit(0);

}

});

JMenuBar mbar=new JMenuBar();

setJMenuBar(mbar);

JMenu fileMenu=new JMenu("File");

mbar.add(fileMenu);

connectItem=new JMenuItem("Connect");

connectItem.addActionListener(this);

fileMenu.add(connectItem);

exitItem=new JMenuItem("Exit");

exitItem.addActionListener(this);

fileMenu.add(exitItem);

}

public void actionPerformed(ActionEvent evt){

Object source=evt.getSource();

if(source==connectItem){

ConnectInfo transfer=new ConnectInfo("yourname","pw");

if(dialog==null)

dialog=new ConnectDialog(this);

if(dialog.showDialog(transfer)){

String uname=transfer.username;

String pwd=transfer.password;

Container contentPane=getContentPane();

contentPane.add(new JLabel("username="+uname+",password="+pwd),"South");

validate();

}

else if(source==exitItem){

System.exit(0);

}}}

public static void main(String[]args){

JFrame f=new test23();

f.show();

}

private ConnectDialog dialog=null;

private JMenuItem connectItem;

private JMenuItem exitItem;

}

class ConnectInfo{

public String username;

public String password;

public ConnectInfo(String u,String p){

username=u;password=p;

}

}

class ConnectDialog extends JDialog implements ActionListener{

public ConnectDialog(JFrame parent){

super(parent,"Connect",true);//这句话很重要!

Container contentPane=getContentPane();

JPanel p1=new JPanel();

p1.setLayout(new GridLayout(2,2));

p1.add(new JLabel("User name:"));

p1.add(username=new JTextField(""));

p1.add(new JLabel("Password:"));

p1.add(password=new JPasswordField(""));

contentPane.add("Center",p1);

Panel p2=new Panel();

okButton=addButton(p2,"Ok");

cancelButton=addButton(p2,"Cancel");

contentPane.add("South",p2);

setSize(240,120);

}

JButton addButton(Container c,String name){

JButton button=new JButton(name);

button.addActionListener(this);

c.add(button);

return button;

}

public void actionPerformed(ActionEvent evt){

Object source=evt.getSource();

if(source==okButton){

ok=true;

setVisible(false);

}

else if(source==cancelButton)

setVisible(false);

}

public boolean showDialog(ConnectInfo transfer){

username.setText(transfer.username);

password.setText(transfer.password);

ok=false;

show();

if(ok){

transfer.username=username.getText();

transfer.password=new String(password.getPassword());

}

return ok;

}

private JTextField username;

private JPasswordField password;

private boolean ok;

private JButton okButton;

private JButton cancelButton;

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值