2012-03-22 65 views
0

可能重複引用一個按鈕:
Accessing JButtons in tabbed pane在選項卡窗格

嗨我創造Jbuttons中,並把它們添加到窗格中,然後加入窗格中的選項卡窗格,我怎麼能引用從其他地方的按鈕?所以引用選項卡窗格中的一個按鈕?像tabbedpane.pane.button?任何幫助表示讚賞

public void initComponents(){ 
    JFrame master = new JFrame("Solar Master Control Panel"); 
    master.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
    Container content = master.getContentPane(); 
    content.setBackground(Color.lightGray); 
    JTabbedPane tabbedPane = new JTabbedPane(); 

    for(Rooms room : rooms){ 
     JPanel tmpPanel = new JPanel(); 
     String roomName = room.getName(); 
     int roomId = room.getId(); 
     tabbedPane.addTab(roomName + " Room " + roomId, tmpPanel); 
     JPanel lightsPane = new JPanel(new BorderLayout()); 
     lightsPane.setLayout(new GridLayout(0, 2, 0, 5)); 

     for(Lights light : room.roomLights){ 
      int lightId = light.getId(); 
      JButton lights = new JButton("Off"); 
      lights.setBackground(Color.red); 
      lights.addActionListener(new LightButtonEvent(roomId, lightId)); 
      lights.setPreferredSize(new Dimension(60, 30)); 
      lights.setBorder(BorderFactory.createRaisedBevelBorder()); 
      JLabel lightLabel = new JLabel("Light" + lightId); 
      Font curFont = lightLabel.getFont(); 
      lightLabel.setFont(new Font(curFont.getFontName(), curFont.getStyle(), 13)); 
      lightsPane.add(lightLabel); 
      lights.add(lights); 
      lightsPane.add(lights); 
      tabbedPane.setTabComponentAt(0, lightspane); 
     } 

     solarLabels.add(new JLabel("", JLabel.CENTER)); 
     UpDateGuiLabels(roomId); 
     JSlider heaterSlider = new JSlider(68, 73); 
     heaterSlider.setPaintTicks(true); 
     heaterSlider.setPaintLabels(true); 
     heaterSlider.setMajorTickSpacing(1); 
     heaterSlider.addChangeListener(new HeaterSliderEvent(roomId)); 
     heaterSlider.setEnabled(false); 
     JButton heater = new JButton("Heater"); 
     heater.setBackground(Color.red); 
     heater.addActionListener(new HeaterButtonEvent(roomId, heaterSlider)); 
     ((JPanel) tabbedPane.getComponentAt(roomId - 1)).add(heater); 
     ((JPanel) tabbedPane.getComponentAt(roomId - 1)).add(heaterSlider); 
     ((JPanel) tabbedPane.getComponentAt(roomId - 1)).add(solarLabels.get(roomId - 1)); 
    } 
     master.add(tabbedPane, BorderLayout.CENTER); 
     master.setSize(800, 600); 
     content.add(tabbedPane); 
     master.setVisible(true); 
} 
+0

你在這裏問同樣的問題兩次嗎?如果是這樣,爲什麼不簡單地編輯和改進原來的帖子,而不是不必要地分開討論? – 2012-03-22 19:38:37

+0

好抓@HovercraftFullOfEels。我發現他以前的問題更好,因爲它說明了他想做什麼以及爲什麼。所以投票結束這一個,並回答了另一個問題 – Robin 2012-03-22 22:45:39

回答

2

有兩種方式訪問​​您的組件。一種方法來維護對您的控件的引用。在這種情況下,每個控件都需要一個集合,其中可能使用索引來引用正確的選項卡式窗格。

另一種方法是使用name屬性,並使用類似於路徑的名稱表達式查找組件。請注意,沒有API的廣泛支持這種做法,所以你需要寫自己的名字,路徑行走功能:

Component findComponent(Component root, String... namePath) { 
    assert root != null; 
    assert namePath != null; 
    Component current = root; 
    for (int i = 0; i < namePath.length; ++i) { 
    if (!(current instanceof Container)) { 
     throw new IllegalArgumentException("Cannot follow path at [" + i + "] -- " + namePath[i]); 
    } 
    Container container = (Container) current; 
    for (int j = 0; j < container.getComponentCount(); ++j) { 
     if (namePath[i].equals(container.getComponent(j).getName())) { 
     current = container.getComponent(j); 
     break; 
     } 
    } // each component within container 
    throw new IllegalArgumentException("No component named " + namePath[i] + " found."); 
    } // each name in name path. 
    return current; 
} 

您將需要設置各個部件的名稱。然後,你就可以找到「關閉」按鈕,例如,喜歡的東西:

JButton button = (JButton) findComponent(masterFrame, "rooms", "Room 12", "OffButton"); 

如果選項卡窗格的名稱是「房」,每個選項卡被命名一樣,它的標題是。

+0

爲什麼不遞歸調用findComponent函數。現在您強制將按鈕直接添加到容器中。 – Robin 2012-03-22 22:44:24

相關問題