簡體   English   中英

使用另一個 Jframe 窗體在 J 框架窗體中顯示或隱藏按鈕

[英]Show or hide a button in a J frame form using another Jframe form

請幫忙。 在我的 java 項目中,我從 Jframe 表單“界面”獲取用戶輸入並按下“check btn”。 然后新的 Jframe 表單“CheckEntry”彈窗顯示輸入的數據是否正確。 在“CheckEntry”表單中有一個確認按鈕。 當按下確認按鈕時,我需要消失“在表單界面中檢查 btn”並顯示“保存按鈕”以將數據發送到數據庫。

首先,我將保存按鈕隱藏在“界面”表單中

public Interface() {
        initComponents();
        conn = db.java_db();
        Toolkit toolkit = getToolkit();
        Dimension size = toolkit.getScreenSize();
        setLocation(size.width/2 - getWidth()/2,size.height/2 - getHeight()/2);
        
        btn_save.setVisible(false);
        
    }

然后在“CheckEntry form”中,按下事件確認 btn 我將檢查 btn 設置為消失並顯示保存按鈕

private void btn_confirm ActionPerformed(java.awt.event.ActionEvent evt) {                                       
       
        Interface i = new Interface();

        i.btn_chk.setVisible(false);
       
        i.btn_save.setVisible(true);

        this.dispose();
      
    }                                      

這不會對表單界面進行任何更改。 請幫忙。 謝謝你。

你的問題在這里:

private void btn_confirm ActionPerformed(java.awt.event.ActionEvent evt) {
    Interface i = new Interface(); // ***** HERE *****
    i.btn_chk.setVisible(false);
    i.btn_save.setVisible(true);
    this.dispose();
}    

問題是,雖然此代碼確實使 JButton 在接口 object 中不可見,但它是錯誤的接口 object,因為它是在該接口 class 中新創建的接口,而不是對用戶可見的接口。

一個解決方案是不這樣做,不創建一個新實例,而是將正確的可視化實例傳遞給子 window 可以對其進行操作,並且可以向用戶顯示其 state 更改。

所以你可以在創建子window class時將正確的實例傳給子window,並將父window class賦值給一個變量,然后你可以根據需要更改它。

旁注:子 window 應該是 JDialog,可能是模態 JDialog 而不是另一個 JFrame。

例如:

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

@SuppressWarnings("serial")
public class MainForm extends JFrame {
    private CheckEntryForm checkEntryForm = new CheckEntryForm(this);
    private JButton showCheckEntryDialogBtn = new JButton("Show Check Entry Form");
    private JButton saveEntryBtn = new JButton("Save");
    private JPanel mainPanel = new JPanel();

    public MainForm() {
        super("Main GUI");
        setDefaultCloseOperation(EXIT_ON_CLOSE);
        showCheckEntryDialogBtn.addActionListener(e -> {
            checkEntryForm.setVisible(true);
        });
        saveEntryBtn.setVisible(false);
        mainPanel.add(showCheckEntryDialogBtn);
        mainPanel.add(saveEntryBtn);
        mainPanel.setPreferredSize(new Dimension(400, 300));
        add(mainPanel);
        pack();
        setLocationRelativeTo(null);
    }

    public static void main(String[] args) {
        SwingUtilities.invokeLater(() -> new MainForm().setVisible(true));
    }

    public void entryCheckOK() {
        showCheckEntryDialogBtn.setVisible(false);
        saveEntryBtn.setVisible(true);

        // let the layout managers do their thing...
        // and also remove dirty pixels
        mainPanel.revalidate();
        mainPanel.repaint();
    }
}
@SuppressWarnings("serial")
class CheckEntryForm extends JDialog {
    private MainForm mainForm;
    private JButton confirmBtn = new JButton("Confirm");

    public CheckEntryForm(MainForm mainForm) {
        super(mainForm, "Check Entry", ModalityType.APPLICATION_MODAL);
        this.mainForm = mainForm;
        confirmBtn.addActionListener(e -> confirmAction());
        JPanel mainPanel = new JPanel();
        mainPanel.add(confirmBtn);
        mainPanel.setPreferredSize(new Dimension(300, 200));
        add(mainPanel);
        pack();
        setLocationRelativeTo(mainForm);
    }

    private void confirmAction() {
        mainForm.entryCheckOK();
        dispose();
    }
}

解釋

在這里,子 window 是一個對話框 window,准確地說是一個 JDialog:

class CheckEntryForm extends JDialog {

此外,我在創建它的時間和地點將 MainForm 主 GUI 的引用傳遞到此對話框中:

private CheckEntryForm checkEntryForm = new CheckEntryForm(this);

並用它在孩子 window class 中設置一個字段:

class CheckEntryForm extends JDialog {
    private MainForm mainForm;
    private JButton confirmBtn = new JButton("Confirm");
    
    public CheckEntryForm(MainForm mainForm) {
        super(mainForm, "Check Entry", ModalityType.APPLICATION_MODAL);
        this.mainForm = mainForm; // **** here ***

然后在子按鈕的 ActionListener 中,我調用了主 Window 的一個公共方法,它刪除了按鈕,並重新設置了容器的布局管理器:

在孩子 window 中:

    private void confirmAction() {
        mainForm.entryCheckOK();
        dispose();
    }

在主要 window 中:

    public void entryCheckOK() {
        showCheckEntryDialogBtn.setVisible(false);
        saveEntryBtn.setVisible(true);
        
        // let the layout managers do their thing...
        // and also remove dirty pixels
        mainPanel.revalidate();
        mainPanel.repaint();
    }


一些方面的建議:

  • 最好使用 CardLayout 來交換視圖,而不是直接顯示/隱藏或刪除組件。
  • 最好避免直接擴展 JFrame 或 JDialog,而是擴展或創建 JPanel,因為這會產生更靈活的代碼。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM