在选项卡式窗格中访问JButtons [英] Accessing JButtons in tabbed pane

查看:160
本文介绍了在选项卡式窗格中访问JButtons的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I am creating a tabbed pane for the amount of rooms that I have and I am creating a light button for each light that each room has and adding it to the tabbed pane.

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

How can I distinguish between these JButtons(lights) in each tabbed pane? say I wanted to do something to light 2 in room2(tabbed pane 2) how can I call it?.

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

At the moment I am just creating a JButton called light, attaching the event class, and adding it to the tabbed pane?. The event class knows what room and light it is, but I need to know how to access it from outside the event class.

如果我想刺激动作事件没有单击的按钮。原因是我有一个程序的客户端版本和一个主程序,当客户端发生某些事情时,它需要在主服务器上发生。非常感谢任何帮助。

Like if I wanted to stimulate the actionevent of that button without it being clicked. The reason being is I have a client version of the program and a master, and when something happens on the client it needs to happen on the master. Any help is greatly appreciated.

public class MasterGUI extends GUI{
      public static ArrayList<Rooms> 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> 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仅是数据的视图。为什么不直接操纵数据?如果你想办法找到那个按钮并触发它点击(这并不是那么难),那么当你决定改变你的布局后,你就会被搞砸了。

Why on earth would you try to access the button and fire the event. The UI is only a view on the data. Why not manipulating the data directly ? If you figure out a way to find that button and trigger a click on it (which is not that hard), you are screwed when you decide to change your layout afterwards.

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

So just share your data behind your UI between master and client, and make sure your UI is updated when something is changed on the data (and vice versa, the UI should update the data when the user makes changes in the UI). That way you can share the state.

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

Try a quick search on Google for MVC (model-view-controller) if you want some more reading on this.

但如果仍想点击,每个按钮都有一个 doClick 方法,它与用户按下并释放的方法相同按钮(来自javadoc的引用)。

But if still want to do a click, each button has a doClick method which does the same thing as if the user had pressed and released the button (quote from the javadoc).

这篇关于在选项卡式窗格中访问JButtons的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆