给个例子你。import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.event.*;class test extends JFrame implements ActionListener 
{  static String name;
   static String passwd;
public test(){
  Container c=getContentPane();
  c.setLayout(new FlowLayout());
  c.add(new JLabel("测试"));
  JButton b=new JButton("登陆");
  b.addActionListener(this);
  c.add(b);
}
public void actionPerformed(ActionEvent e){
  JTextField tf=new JTextField(20);
  JPasswordField pass=new JPasswordField(20);
  Object[] msgs=new Object[]{new String("用户名"),tf,new String("密码"),pass};
  if(JOptionPane.showConfirmDialog(this,msgs,"登陆",JOptionPane.OK_CANCEL_OPTION,JOptionPane.INFORMATION_MESSAGE)==JOptionPane.OK_OPTION){
    name=tf.getText();
passwd=pass.getText();
  }
       
}
public static void main(String[] args) 
{
test t=new test();
t.setSize(300,300);
t.setDefaultCloseOperation(DISPOSE_ON_CLOSE);
t.show();

}
}

解决方案 »

  1.   

    这个随便找本Swing的书应该都有例子。或者看看sun的demo。
    下面是从j2sdk1.4.2\demo\jfc\TableExample\src\TableExample.java选出来的,供参考。 JButton showConnectionInfoButton = new JButton("Configuration");
            showConnectionInfoButton.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                activateConnectionDialog();
            }
        }
    );    void activateConnectionDialog() {
    if(JOptionPane.showOptionDialog(tableAggregate, connectionPanel, ConnectTitle,
       JOptionPane.DEFAULT_OPTION, JOptionPane.INFORMATION_MESSAGE,
                       null, ConnectOptionNames, ConnectOptionNames[0]) == 0) {
        connect();
                frame.setVisible(true);
    }
    else if(!frame.isVisible())
        System.exit(0);
        }