JAVA按钮打开另一个可视化窗口,可以打开第一个界面点击按钮,但是会出错

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);
}

}
}

}

import java.awt.EventQueue;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;

import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JPasswordField;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.JTextField;
import javax.swing.JToolBar;

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);
JToolBar toolBar = new JToolBar();
contentPane = new JPanel();
userDialog = new userDialog(this);
textArea = new JTextArea();
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);
add(contentPane);
}

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);
}
}
}
}

追问

可以点出出来那个对话框了,但是显示窗口那里又不行了,出来了,但是,确定的时候有问题,不能把信息读到主界面

追答

你那个是你的需求问题,不是这个报错的问题,报错的问题我已经解决了,你应该采纳我

温馨提示:答案为网友推荐,仅供参考
相似回答