Java:等待第一帧,同时从第二帧检索数据,然后单击关闭按钮,控件也进入第一帧 [英] Java: wait first frame while retrieving data from second frame then on hitting on close button and control also come to the first frame
问题描述
在我的应用程序中有两个框架.在主框架中,有服务器"按钮.当我单击服务器"按钮时,第二个框架将打开,当我单击第二个框架中的提交"按钮时,输入一些详细信息后,该框架将关闭并再次控制到服务器"按钮,在此我将该框架称为我会做剩下的工作. 当第二个框架打开时,我想在我的应用程序中再做一件事,那就是第一帧没有任何活动.
In my application two frame is there. In main frame "Server" button is there. When I click on "Server" button second frame will open and after enter some detail when i click on "submit" button which is on second frame this frame will close and control again go to the "Server" button where I call this frame so I will do my rest of task. And one more thing I want in my application when the second frame is open then no activity is on first frame.
我的第一帧是: JFrame帧;
在此框架中,有一个服务器"按钮: JButton btnSaveOnServer;
In this frame one Server button is there: JButton btnSaveOnServer;
在此按钮的addActionListener中,我编写以下代码以打开第二帧:
In addActionListener of this button I write this code to open second frame:
btnSaveOnServer.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent arg0) {
ServerClass window = new ServerClass();
window.serverFrame.setVisible(true);
}
});
第二帧代码:
public class ServerClass{
public JFrame serverFrame;
private JLabel userName;
private JLabel password;
private JTextField textUsername;
private JPasswordField textPassword;
private JButton btnOk;
protected ServerClass(){
try {
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
serverFrame = new JFrame("Server Entry Token");
serverFrame.setBounds(500, 280, 335, 250);
serverFrame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
serverFrame.getContentPane().setLayout(null);
font = new Font("Arial", Font.BOLD, 15);
userName = new JLabel("Username");
userName.setBounds(4, 40, 100, 15);
userName.setFont(font);
password = new JLabel("PassKey");
password.setBounds(4, 69, 100, 15);
password.setFont(font);
textUsername = new JTextField();
textUsername.setBounds(110, 35, 200, 25);
textUsername.setFont(font);
textPassword = new JPasswordField(10);
textPassword.setBounds(110, 65, 200, 25);
textPassword.setEchoChar(ch);
btnOk = new JButton("Submit");
btnOk.setBounds(30, 170, 85, 25);
serverFrame.add(userName);
serverFrame.add(password);
serverFrame.add(textUsername);
serverFrame.add(textPassword);
serverFrame.add(btnOk);
btnOk.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent arg0) {
if (!textUsername.getText().equals("")
&& !textPassword.getText().equals("")) {
tArea.append("Username:\t"+textUsername.getText()+"\n");
tArea.append("Pass:\t"+textPassword.getText()+"\n");
//tArea is the area in first frame where I display the entered data.
serverFrame.dispose();
}
}
});
}
}
在这里,我在提交"按钮中添加代码 serverFrame.dispose(); ,该代码会关闭框架,但控件不会出现在第一帧. 所以我的问题是:
Here I add the code serverFrame.dispose(); in "Submit" button which close the frame but control not come to the first frame. So my problem is:
- 打开第二帧时如何禁用或禁用第一帧.
- 在我的代码中,我正在第一帧的服务器"按钮中调用第二帧.这是将第二帧称为第一帧的写方法吗?
- 我如何关闭第二个框架(单击提交按钮后),以便框架关闭并且控制权转到第一个框架的服务器"按钮.
推荐答案
1)In my application two frame is there
不要使用两个 JFrames ,那就是如果不使用(JNI or JNA
)是不可能的,那么这些容器就不会实现parent
,很难移动一个JFrame
toFront()
/toBack()
/真正的FocusRecycle
是残酷的笑话
don't use two JFrames, thats isn't possible without using (JNI or JNA
), these containers doesn't implements parent
, there is hard job move one JFrame
toFront()
/ toBack()
/ real FocusRecycle
is cruel joke
2)将 JDialog 与setModal
一起使用,或ModalityType
,
2) use JDialog, with setModal
or ModalityType
,
3)普通香草 JOptionPane 是最好的选择做到
3) plain vanilla JOptionPane is best way ho to do it,
4)JFrame / JDialog / JWindow
没有实现finalize()
,因此无论您呼叫setVisible(false)
还是dispose()
4) JFrame / JDialog / JWindow
doesn't implements finalize()
, then doesn't matter if you call for setVisible(false)
or dispose()
这篇关于Java:等待第一帧,同时从第二帧检索数据,然后单击关闭按钮,控件也进入第一帧的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!