在选项卡式窗格中访问JButtons

我正在为我拥有的房间数创建一个标签式窗格,我正在为每个房间的每个灯创建一个灯光按钮,并将其添加到选项卡式窗格。

如何在每个选项卡式窗格中区分这些JButton(灯光)? 说我想在房间2(选项卡式窗格2)中点亮2,我怎么称呼它?

目前我只是创建一个名为light的JButton,附加事件类,并将其添加到选项卡式窗格? 事件类知道它是什么房间和灯光,但我需要知道如何从事件类外部访问它。

就像我想刺激该按钮的动作事件而不点击它一样。 原因是我有一个程序的客户端版本和一个主程序,当客户端发生某些事情时,它需要在主服务器上发生。 任何帮助是极大的赞赏。

public class MasterGUI extends GUI{ public static ArrayList rooms; public MasterGUI(){ rooms = Building.getRoomList(); } public static void UpDateGuiLabels(final int roomNo){ SwingUtilities.invokeLater(new Runnable(){ public void run() { try{ rooms.get(roomNo - 1).roomHeatLoss(); solarLabels.get(roomNo - 1).setText("Room BTU is: " + round(rooms.get(roomNo - 1).getHeatLoss())); }catch (RuntimeException e){ } } }); } 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); lightsPane.add(lights); ((JPanel) tabbedPane.getComponentAt(roomId - 1)).add(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); } 

活动类别

 public class LightButtonEvent implements ActionListener{ ArrayList rooms; int lightNumber; int roomNumber; public LightButtonEvent(int room, int light){ lightNumber = light; roomNumber = room; rooms = Building.getRoomList(); } public void actionPerformed(ActionEvent e){ if(rooms.get(roomNumber - 1).roomLights.get(lightNumber - 1).getLightStatus() == true){ rooms.get(roomNumber - 1).roomLights.get(lightNumber - 1).setLightOff(); ((JButton)e.getSource()).setText("Off"); ((JButton)e.getSource()).setBackground(Color.red); MasterGUI.UpDateGuiLabels(roomNumber); }else{ rooms.get(roomNumber - 1).roomLights.get(lightNumber - 1).setLightOn(); ((JButton)e.getSource()).setText("On"); ((JButton)e.getSource()).setBackground(Color.green); MasterGUI.UpDateGuiLabels(roomNumber); } } 

}

为什么你试图访问按钮并解雇事件。 UI仅是数据的视图。 为什么不直接操纵数据? 如果你想办法找到那个按钮并触发它点击(这并不难),那么当你决定改变你的布局后,你就会被搞砸了。

因此,只需在主服务器和客户端之间共享UI后面的数据,并确保在数据发生更改时更新UI(反之亦然,UI应在用户在UI中进行更改时更新数据)。 这样你就可以分享状态了。

如果您想要更多阅读,请尝试在Google上快速搜索MVC(模型 – 视图 – 控制器)。

但是如果仍然想要点击,每个按钮都有一个doClick方法,就像用户按下并释放按钮一样(来自javadoc的引用)。