Java 在选项卡式窗格中访问JButtons
我正在为我拥有的房间数量创建一个选项卡式窗格,并为每个房间拥有的每个灯光创建一个灯光按钮,并将其添加到选项卡式窗格中 如何区分每个选项卡式窗格中的这些按钮(灯光)?假设我想做点什么来点亮2号房间的2号房间(选项卡式窗格2),我怎么称呼它 目前,我正在创建一个名为light的JButton,附加事件类,并将其添加到选项卡式窗格中?。event类知道它是什么房间和灯光,但我需要知道如何从event类外部访问它 就像我想在不点击按钮的情况下刺激该按钮的actionevent。原因是我有一个客户端版本的程序和一个主机,当客户端发生了什么事情,它需要在主机上发生。非常感谢您的帮助Java 在选项卡式窗格中访问JButtons,java,swing,Java,Swing,我正在为我拥有的房间数量创建一个选项卡式窗格,并为每个房间拥有的每个灯光创建一个灯光按钮,并将其添加到选项卡式窗格中 如何区分每个选项卡式窗格中的这些按钮(灯光)?假设我想做点什么来点亮2号房间的2号房间(选项卡式窗格2),我怎么称呼它 目前,我正在创建一个名为light的JButton,附加事件类,并将其添加到选项卡式窗格中?。event类知道它是什么房间和灯光,但我需要知道如何从event类外部访问它 就像我想在不点击按钮的情况下刺激该按钮的actionevent。原因是我有一个客户端版本的
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);
}
公共类MasterGUI扩展GUI{
公共静态阵列列表室;
公共MasterGUI(){
房间=Building.getRoomList();
}
公共静态void UpDateGuiLabels(final int roomNo){
SwingUtilities.invokeLater(新的Runnable(){
公开募捐{
试试{
roomNo-1.roomHeatLoss();
solarLabels.get(roomNo-1).setText(“房间BTU是:”+圆形(rooms.get(roomNo-1).getHeatLoss());
}捕获(运行时异常e){
}
}
});
}
公共组件(){
JFrame master=新JFrame(“太阳能主控制面板”);
master.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
容器内容=master.getContentPane();
内容。背景(颜色。浅灰色);
JTabbedPane tabbedPane=新的JTabbedPane();
用于(房间:房间){
JPanel tmpPanel=新的JPanel();
字符串roomName=room.getName();
int roomId=room.getId();
tabbedPane.addTab(roomName+“Room”+roomId,tmpPanel);
JPanel lightsPane=newjpanel(newborderlayout());
设置布局(新的网格布局(0,2,0,5));
用于(灯光:房间。房间灯光){
int lightId=light.getId();
JButton灯=新JButton(“关闭”);
灯光。背景(颜色。红色);
lights.addActionListener(新的lightButtoneEvent(roomId,lightId));
灯光。设置首选尺寸(新尺寸(60,30));
lights.setboorder(BorderFactory.createRaisedBevelBorder());
JLabel lightLabel=新的JLabel(“灯光”+灯光ID);
Font curFont=lightLabel.getFont();
setFont(新字体(curFont.getFontName(),curFont.getStyle(),13));
lightsPane.add(lightLabel);
lightsPane。添加(灯光);
((JPanel)tabbedPane.getComponentAt(roomId-1)).add(lightsPane);
}
添加(新的JLabel(“,JLabel.CENTER));
更新guilabels(roomId);
JSlider heaterSlider=新JSlider(68,73);
加热滑块。设置油漆滴答声(真);
加热器滑动。设置油漆标签(正确);
加热滑道设置主滑道间距(1);
heaterSlider.addChangeListener(新HeaterSliderEvent(roomId));
加热器滑块。设置启用(错误);
JButton加热器=新JButton(“加热器”);
加热器。立根背景(颜色。红色);
heater.addActionListener(新的HeaterButtoneEvent(roomId,heaterSlider));
((JPanel)选项卡窗格.getComponentAt(roomId-1)).add(加热器);
((JPanel)选项卡窗格.getComponentAt(roomId-1)).add(heaterSlider);
((JPanel)选项卡窗格.getComponentAt(roomId-1)).add(solarLabels.get(roomId-1));
}
添加(选项卡窗格,BorderLayout.CENTER);
主设置尺寸(800600);
content.add(选项卡窗格);
master.setVisible(真);
}
灯光事件类
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);
}
}
公共类LightButtonEvent实现ActionListener{
ArrayList房间;
int-lightNumber;
房间号;
公共照明按钮事件(内部房间、内部照明){
lightNumber=灯光;
房间号=房间;
房间=Building.getRoomList();
}
已执行的公共无效操作(操作事件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.UpdateGUI标签(房间号);
}否则{
rooms.get(roomNumber-1).roomLights.get(lightNumber-1.setLightOn();
((JButton)e.getSource()).setText(“On”);
((JButton)e.getSource()).setBackground(Color.green);
MasterGUI.UpdateGUI标签(房间号);
}
}
}你究竟为什么要尝试访问按钮并触发事件。UI只是数据的一个视图。为什么不直接操作数据?如果你想办法找到按钮并点击它(这并不难),那么当你决定以后更改布局时,你就完蛋了 因此,只需在主控和客户端之间共享UI后面的数据,并确保在数据发生更改时更新UI(反之亦然,当用户在UI中进行更改时,UI应更新数据)。这样,您就可以共享状态 如果您想了解更多有关MVC的信息,请尝试在Google上快速搜索MVC(model view controller)
但是,如果仍然想点击,每个按钮都有一个方法,就像用户按下并释放按钮一样(引自javadoc)。@Robin和JtabbedPane被打破了CardLayout(今天的投票数超过了)@mKorbel这要视情况而定。即使使用CardLayout,您也需要将按钮放置在某个位置以在布局之间切换。选项卡肯定是有意义的,因为每个人都知道,当您单击选项卡时,该选项卡将变为活动状态。用户界面中某个位置的按钮可能会更改另一个选项卡的内容,这可能不那么简单