日期:2014-05-20  浏览次数:20706 次

关于事件处理的一个问题
设计界面时,在窗口弹出一个对话框,填完一些确认信息后,点击“提交”按钮后,使该对话框setVisible(false),那么如何使母窗口里面相应的面板也能响应该事件进而做出一定的反应呢?比方说,在内容显示区可以显示“欢迎你,登录成功”等

关键是我不知道如何响应在你点击了对话框的”提交“按钮后,如何使原窗口的内容显示区获得该事件的处理。。。。。。。。。。

急求答案,十分感谢。。。

------解决方案--------------------
这个完全适合你的要求,请结贴


import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

public class DataExchangeTest
{
public static void main(String[] args)
{
DataExchangeFrame frame = new DataExchangeFrame();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
}
}

/**
A frame with a menu whose File-> Connect action shows a
password dialog.
*/
class DataExchangeFrame extends JFrame
{
public DataExchangeFrame()
{
setTitle( "DataExchangeTest ");
setSize(DEFAULT_WIDTH, DEFAULT_HEIGHT);

// construct a File menu

JMenuBar mbar = new JMenuBar();
setJMenuBar(mbar);
JMenu fileMenu = new JMenu( "File ");
mbar.add(fileMenu);

// add Connect and Exit menu items

JMenuItem connectItem = new JMenuItem( "Connect ");
connectItem.addActionListener(new ConnectAction());
fileMenu.add(connectItem);

// The Exit item exits the program

JMenuItem exitItem = new JMenuItem( "Exit ");
exitItem.addActionListener(new
ActionListener()
{
public void actionPerformed(ActionEvent event)
{
System.exit(0);
}
});
fileMenu.add(exitItem);

textArea = new JTextArea();
add(new JScrollPane(textArea), BorderLayout.CENTER);
}

public static final int DEFAULT_WIDTH = 300;
public static final int DEFAULT_HEIGHT = 200;

private PasswordChooser dialog = null;
private JTextArea textArea;

/**
The Connect action pops up the password dialog.
*/

private class ConnectAction implements ActionListener
{
public void actionPerformed(ActionEvent event)
{
// if first time, construct dialog

if (dialog == null)
dialog = new PasswordChooser();

// set default values
dialog.setUser(new User( "yourname ", null));

// pop up dialog
if (dialog.showDialog(DataExchangeFrame.this, "Connect "))
{
// if accepted, retrieve user input
User u = dialog.getUser();
textArea.append(
"user name = " + u.getName()
+ ", password = " + (new String(u.getPassword()))
+ "\n ");
}
}
}
}

/**
A password chooser that is shown inside a dialog
*/
class PasswordChooser extends JPanel
{
public PasswordChooser()
{
setLayout(new BorderLayout());

// construct a panel with user name and password fields

JPanel panel = new JPanel();
panel.setLayout(new GridLayout(2, 2));
panel.add(new JLabel( "User name: "));
panel.add(username = new JTextField( " "));
panel.add(new JLabel( "Password: "));
panel.add(password = new JPasswordField( " "));
add(panel, BorderLayout.CENTER);