引用选项卡窗格中的按钮



可能重复:
访问选项卡窗格中的JButtons

嗨,我正在创建Jbuttons并将它们添加到窗格中,然后将窗格添加到选项卡窗格中,如何从其他地方引用按钮?所以引用选项卡窗格中的一个按钮?比如选项卡窗格。窗格。按钮??感谢提供的任何帮助

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);
}

有两种方法可以访问组件。维护对控件的引用的一种方法。在这种情况下,每个控件都需要一个集合,其中可能会使用一个索引来引用正确的选项卡式窗格。

另一种方法是使用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");

如果选项卡窗格的名称为"房间",并且每个选项卡的名称与其标题相同。

最新更新