簡體   English   中英

在我的客戶JPanel上調用方法,以更改JFrame中所有JInternalFrame的標簽

[英]Call the method on my customer JPanel for changing label for all JInternalFrames in JFrame

我有MDI項目。 我在JInternalFrame中添加了客戶JPanel。 我的客戶JPanel有一個公共方法來更改某些組件背景顏色。 單擊JFrame上的按鈕時,我希望它更改客戶JPanel上所有InternalFrame的標簽或文本。 我怎么稱呼這個方法? 提前致謝

以下代碼是JFrame上按鈕的操作

 private void categoryAction(ActionEvent e){
    try{
        JButton b=(JButton)e.getSource();
        Color c=b.getBackground();
         JInternalFrame[] allframes = desktop.getAllFrames();
         int count = allframes.length;
         for (int i=0; i<allframes.length-1; i++){
            //call the method on the PDFJPanel
         }

    }
    catch(Exception  err){
        Utility.DisplayErrorMsg(err.toString());

    }

以下代碼將內部框架添加到桌面

private void AddNote(File file){        
        JInternalFrame internalFrame = new     JInternalFrame("PDFAnnotation"
                + file.getName(), true, true, true, true);      
       internalFrame.setBounds(0, 0, 600, 100);
       desktop.add(internalFrame);    


       PDFJPanel p=new PDFJPanel(file);       
       internalFrame.add(p, BorderLayout.CENTER);
       internalFrame.setVisible(true);
try {
           internalFrame.setSelected(true);
       }
       catch (java.beans.PropertyVetoException e) {}
       this.add(desktop, BorderLayout.CENTER);

      //resize the internal frame as full screen  
      Dimension size = desktop.getSize();
      int w = size.width ; 
      int h = size.height ; 
      int x=0;
      int y=0;
      desktop.getDesktopManager().resizeFrame(internalFrame, x, y, w, h);
    }

我的客戶JPanel上有方法

Public void setDefaultColor(Color c){
      //change the label and textbox color
 }

您可以使用返回當前活動幀的JDesktopPane.getSelectedFrame 您可以從布局管理器中檢索PDFJPanel ,即使用BorderLayout.getLayoutComponent() 或者更簡單,更清潔,您可以擴展JInternalFrame ,即:

class PDFFrame extends JInternalFrame {
    private PDFJPanel panel;

    public PDFFrame(File file) {
        panel = new PDFJPanel(file); 
        add(panel, BorderLayout.CENTER);
    }

    public void setDefaultColor(Color c){
        panel.setDefaultColor();
    }
}

然后,訪問它:

JDesktopPane desktop = ...;

PDFFrame frame = (PDFFrame) desktop.getSelectedFrame();
frame.setDefaultColor(Color.BLUE);

暫無
暫無

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

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