public class FF2 extends JFrame{
private JPanel contentPane;
private userDialog userDialog;
private JTextArea textArea;
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
FF2 frame = new FF2();
frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
public FF2() {
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setBounds(100, 100, 450, 300);
contentPane.add(toolBar);
JButton button = new JButton("\u67E5\u8BE2");
button.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
userDialog.setVisible(true);
}
});
toolBar.add(button);
JButton button_1 = new JButton("\u5220\u9664");
button_1.addMouseListener(new MouseAdapter() {
@Override
public void mouseClicked(MouseEvent e) {
}
});
toolBar.add(button_1);
JButton button_2 = new JButton("\u9000\u51FA");
toolBar.add(button_2);
JScrollPane scrollPane = new JScrollPane();
scrollPane.setBounds(20, 58, 404, 172);
contentPane.add(scrollPane);
JTextArea textArea = new JTextArea();
scrollPane.setViewportView(textArea);
}
class userDialog extends JDialog implements ActionListener
{
JPanel p;
JLabel lblname,lblpw;
JTextField txtname;
JPasswordField txtpw;
JButton b1,b2;
public userDialog(JFrame f)
{
super(f,"对话框",true);
p=new JPanel (new GridLayout(4,2));
lblname =new JLabel("用户名");
lblpw=new JLabel("密码");
txtname=new JTextField(10);
txtpw=new JPasswordField(10);
b1=new JButton("确定");
b2=new JButton("取消");
b1.addActionListener(this);
b2.addActionListener(this);
p.add(lblname);
p.add(txtname);
p.add(lblpw);
p.add(txtpw);
p.add(b1);
p.add(b2);
getContentPane().add(p);
this.pack();
}
@Override
public void actionPerformed(ActionEvent e) {
if(e.getSource()==b1)
{
String strName=txtname.getText();
String strPwd=new String(txtpw.getPassword());
if(strName.equals(""))
{
JOptionPane.showMessageDialog(b1, "用户名不能为空");
return;
}
if(strPwd.equals(""))
{
JOptionPane.showMessageDialog(b2, "密码不能为空");
return;
}
textArea.append("用户名:"+strName +"\n");
textArea.append("密码:"+strPwd +"\n");
this.setVisible(false);
}
if(e.getSource()==b2)
{
txtname.setText("");
txtpw.setText("");
this.setVisible(false);
}
}
}
}
可以点出出来那个对话框了,但是显示窗口那里又不行了,出来了,但是,确定的时候有问题,不能把信息读到主界面
追答你那个是你的需求问题,不是这个报错的问题,报错的问题我已经解决了,你应该采纳我