[英]Performing action when closing JFrame
在我的程序中,我有一個包含按鈕的主JFrame。 單擊此按鈕時,會出現一個新的JFrame,我可以在其中更改某些信息。 每當我完成編輯,我按下新JFrame上的保存按鈕,保存更改並處理JFrame。 現在,完成此操作后,我還想在主JFrame中執行操作,但前提是更改了一些內容。 如果我打開新的JFrame並在不使用保存按鈕的情況下再次關閉它,我不想在主框架中執行任何操作。 我已經嘗試在網上搜索解決方案,但似乎沒有任何有用的東西..
我到目前為止的代碼示例:主框架......
public class MainFrame extends JFrame
{
public MainFrame()
{
super("Main Frame");
JButton details = new JButton("Add Detail");
add(details);
details.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e)
{
new DetailFrame().setVisible(true);
}
});
}
}
細節框架......
public class DetailFrame extends JFrame
{
public DetailFrame()
{
super("Detail Frame");
JButton save = new JButton("Save");
add(save);
save.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e)
{
// Save whatever content
dispose();
}
});
}
}
因此,當我單擊Detail Frame上的“Save”按鈕時,我想在主框架中執行某些操作,而當在Detail Frame上單擊“x”時,我不想做任何事情。
希望有人能幫助我,對不起我的英語..
您可以將MainFrame句柄傳遞給DetailFrame構造函數。 然后,在單擊“保存”按鈕時,DetailFrame將調用MainFrame中的函數並將更改傳遞給它。
另一種方法是在DetailFrame中創建一個public boolean
變量,並在單擊“保存”按鈕時將其設置為true
。 這樣MainFrame就會知道DetailFrame是關閉還是Save'd。
編輯:一些更多的想法:
使用JDialog
而不是JFrame
。 JDialog.setVisible
是模態的,即它將阻止調用函數,直到對話框關閉; 這樣,您可以在同一個“詳細信息”按鈕偵聽器中處理對話框的結果。
要在調用對話框后訪問該對話框,請將對話框存儲在單獨的變量中。 首先構造對話框,然后顯示它,然后通過分析其變量來處理結果。
將編輯結果存儲在DetailFrame
其他公共變量中(或者將其稱為DetailDialog
)。 只有在單擊“保存”按鈕時才會發生這種情況。 這甚至可以允許沒有布爾變量(取決於您正在編輯的值的類型)。
DetailDialog dlg = new DetailDialog();
dlg.setVisible(true);
if(dlg.approvedResult != null) {
// process the result...
}
編輯:對不起, JDialog
默認情況下不是模態的。 需要調用一個特殊的super
構造函數來使其成為模態。
此外,在這里您必須將對MainFrame
的引用傳遞給對話框構造函數,但您仍然可以將其聲明為簡單的JFrame
並避免不必要的依賴項。
要從匿名ActionListener中獲取對封閉MainFrame
的引用,請使用MainFrame.this
。
為了能夠在創建按鈕文本后更改按鈕文本,您必須將按鈕存儲在成員變量中。
主框架......
public class MainFrame extends JFrame
{
private JButton details = new JButton("Add Detail");
public MainFrame()
{
super("Main Frame");
getContentPane().add(details);
details.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e)
{
DetailDialog dlg = new DetailDialog(MainFrame.this);
dlg.setVisible(true);
if(dlg.approved){
details.setText("Edit Detail");
}
}
});
}
}
細節對話框...(不是框架)
public class DetailDialog extends JDialog
{
public boolean approved = false;
public DetailDialog(JFrame parent)
{
super(parent,"Detail Dialog",true); // modal dialog parented to the calling frame
JButton save = new JButton("Save");
getContentPane().add(save);
save.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e)
{
// Save whatever content
approved = true;
dispose();
}
});
}
}
在主框架中創建細節框架,並使用windowadapter類向其添加windowlistener。 通過檢查更改,處理這些更改,然后處置詳細信息框來實現windowclosing事件。 這一切都在大型機中完成。
細節框架應該在關閉集上不執行任何操作,以防止在記錄更改之前處理細節框架。
您可能希望實現檢查詳細信息框中的更改,作為返回保存有趣數據的類的方法。 這樣你的windowlistener可以很小到一點。
忘掉第二個JFrame
。 改為使用模態對話框。 它會阻止輸入直到被解雇。 一旦被解雇,唯一要做的就是決定是否更新原始數據。 JOptionPane
具有一些內置功能,可以輕松實現。 如果用戶按下Cancel
或esc
鍵,則showInputDialog()
方法將返回null
作為結果。
import java.awt.BorderLayout;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.border.EmptyBorder;
class EditInfo {
public static void main(String[] args) {
Runnable r = new Runnable() {
public void run() {
final JFrame f = new JFrame("Uneditable");
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JPanel p = new JPanel(new BorderLayout(10,10));
final JTextField tf = new JTextField("Hello World!", 20);
tf.setEnabled(false);
p.add(tf, BorderLayout.CENTER);
JButton edit = new JButton("Edit");
edit.addActionListener( new ActionListener(){
public void actionPerformed(ActionEvent ae) {
String result = JOptionPane.showInputDialog(
f,
"Edit text",
tf.getText());
if (result!=null) {
tf.setText(result);
}
}
} );
p.add(edit, BorderLayout.EAST);
p.setBorder(new EmptyBorder(10,10,10,10));
f.setContentPane(p);
f.pack();
f.setLocationByPlatform(true);
f.setVisible(true);
}
};
SwingUtilities.invokeLater(r);
}
}
如果需要在JOptionPane
中一次編輯多個字段,請使用JPanel
將它們全部包含showMessageDialog()
,並將它們放在showMessageDialog()
調用中。 檢查基於整數的返回結果,以確定用戶是否確定了更改。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.