2013-01-19 94 views
1

我有兩個類mainpanel.java和subpanel.java。 subpanel.class包含一個複選框和一些標籤。當我點擊mainpanel.java中的某些按鈕時,我想更改這些組件的setSelected()和setText()。訪問其他類的swing組件

我在subpanel.java中創建了一個方法,我從mainpanel.java調用並傳遞布爾值。

public void schedulerchange(boolean check){ 
     System.out.println("checked"+check); 
     scheduleenabler.setEnabled(check); 
     scheduleenabler.setSelected(check); 
     scheduleinfo.setText("Scheduler in On"); 
     //subpanel21.updateUI(); 
    } 

當我打電話從mainpanel.java函數被調用此功能,但值不會改變,除非我做JCheckBox的和靜態的JLabel。但從我學到的東西我們不應該使用靜態組件,除非非常必要。 有沒有其他的方法來改變組件?

+0

我想在設置啓用調用** scheduleenabler.revalidate()**將做的伎倆。 – Amarnath

+0

*「我有兩個類mainpanel.java和subpanel.java」*不要擴展任何一個。只需保留一個參考。保持對文本組件的引用,並解決問題。還請學習類的常用[Java命名約定](http://java.sun.com/docs/books/jls/second_edition/html/names.doc.html#73307)(具體用於名稱的情況)方法和屬性名稱並一致使用它。 –

+0

@che revalidate()不起作用。它仍然更新複選框,如果我使用靜態 –

回答

3

如果我明白你的問題,然後我想你想編寫一個單獨ActionListener類,並沒有執行操作,這將使還是在UI級禁用JCheckBox。以下代碼顯示。將您的複選框引用傳遞給PerformAction類,並通過單擊該按鈕啓用或禁用它。

import java.awt.EventQueue; 
import java.awt.FlowLayout; 
import java.awt.event.ActionEvent; 
import java.awt.event.ActionListener; 
import javax.swing.JButton; 
import javax.swing.JCheckBox; 
import javax.swing.JFrame; 
import javax.swing.JLabel; 
import javax.swing.JPanel; 

public class MainClass { 

MainClass() { 
    JFrame jfrm = new JFrame("JTable Demo"); 
    jfrm.setLayout(new FlowLayout()); 
    jfrm.setSize(460, 180); 
    jfrm.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 

    JCheckBox check = null; 
    // Get the Panel from the subclass; 
    JPanel panel = new CheckBox().getCheckBoxPanel(); 

    // From the compoenents present in the panel get the CheckBox compoenent. 
    for(int i = 0; i < panel.getComponentCount(); i++) { 
     if(panel.getComponent(i) instanceof JCheckBox) { 
     check = (JCheckBox) panel.getComponent(i); 
     } 
    } 

    JButton button = new JButton("Click"); 

    // Pass the CheckBox Compoenent to the ActionListener. 
button.addActionListener(new PerformAction(check)); 

    jfrm.add(button); 
    jfrm.add(panel); 
    jfrm.setVisible(true); 
} 

    public static void main(String args[]) { 
    EventQueue.invokeLater(new Runnable() { 

     @Override 
     public void run() { 
      new MainClass(); 
     } 
    }); 
} 
} 

class PerformAction implements ActionListener { 

JCheckBox check = null; 
public PerformAction(JCheckBox checkBox) { 
    check = checkBox; 
} 
@Override 
public void actionPerformed(ActionEvent e) { 
    boolean checkStatus = check.isSelected(); 
    if(checkStatus == true) { 
     check.setEnabled(false); 
     check.setSelected(false); 
    } else { 
     check.setEnabled(true); 
     check.setSelected(true); 
     } 
} 
} 

class CheckBox { 
public JPanel getCheckBoxPanel() { 
    JPanel checkPanel = new JPanel(); 
    JCheckBox check = new JCheckBox(); 
    checkPanel.add(new JLabel("CheckBox")); 
    checkPanel.add(check); 

    return checkPanel; 
} 
} 
+0

我使用兩個不同的類。子面板類將面板返回到mainpanel類。這個面板包含一個複選框和標籤,當我點擊mainpanel類中的一個按鈕時,我想改變它的屬性。 –

+0

看看我的更新代碼。這將獲得Panel並從面板獲取組件,然後將其傳遞給ActionPerformed類。希望這會有所幫助.. ;-) – Amarnath

+0

@RohanKandwal:不要忽視你發現有幫助的答案。 – trashgod

3

這不是updateUI()的適當使用,它將「將UI屬性重置爲當前外觀的值」。正如註釋中所建議的,使用revalidate()僅當將組件添加到封閉的Container或從其中刪除時纔會有幫助。相反,直接在子面板實例上調用repaint()。爲獲得更大的靈活性,請使用觀察者pettern建議here

附錄:本示例使用Action來封裝按鈕的行爲。由於複選框的選定狀態是綁定屬性,因此組件會自動重新繪製,但如果需要,您可以明確調用repaint()

附錄:更新以傳遞參考作爲參數。

附錄:在此變體中,該參數是對導出的Action的引用。

import java.awt.EventQueue; 
import java.awt.GridLayout; 
import java.awt.event.ActionEvent; 
import javax.swing.AbstractAction; 
import javax.swing.Action; 
import javax.swing.JButton; 
import javax.swing.JCheckBox; 
import javax.swing.JFrame; 
import javax.swing.JPanel; 

/** @see https://stackoverflow.com/a/14412516/230513 */ 
public class Example { 

    private void display() { 
     JFrame f = new JFrame("Example"); 
     f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
     f.setLayout(new GridLayout(0, 1)); 
     JPanel panel = new JPanel(); 
     final JCheckBox check = new JCheckBox("Check"); 
     Action checkAction = new AbstractAction("Update") { 

      @Override 
      public void actionPerformed(ActionEvent e) { 
       check.setSelected(!check.isSelected()); 
      } 
     }; 
     panel.add(check); 
     f.add(panel); 
     f.add(new SubPanel(checkAction)); 
     f.pack(); 
     f.setLocationRelativeTo(null); 
     f.setVisible(true); 
    } 

    private static class SubPanel extends JPanel { 

     public SubPanel(final Action action) { 
      this.add(new JButton(action)); 
     } 
    } 

    public static void main(String[] args) { 
     EventQueue.invokeLater(new Runnable() { 

      @Override 
      public void run() { 
       new Example().display(); 
      } 
     }); 
    } 
} 
+0

我幾乎是一個noob,所以我不明白深層編碼。一個簡單的例子,兩個類將是很好的 –

+0

我已經闡述了上面。 – trashgod

+0

感謝您的示例,但您的示例使用了一個在actionPerformed上進行更新的類。我想在從其他課程執行操作時選擇/取消選中該複選框。 –