引用tabbedpane中的按钮

时间:2012-03-22 19:16:01

标签: java swing jbutton jtabbedpane

  

可能重复:
  Accessing JButtons in tabbed pane

您好我正在创建Jbuttons并将它们添加到窗格然后将窗格添加到tabbedpane,我如何从其他地方引用按钮?所以参考选项卡式窗格中的一个按钮?喜欢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);
}

1 个答案:

答案 0 :(得分: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");

如果选项卡式窗格的名称为“rooms”,则每个选项卡的名称与标题相同。