2012-03-22 68 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

相关问题