可能重複引用一個按鈕:
Accessing JButtons in tabbed pane在選項卡窗格
嗨我創造Jbuttons中,並把它們添加到窗格中,然後加入窗格中的選項卡窗格,我怎麼能引用從其他地方的按鈕?所以引用選項卡窗格中的一個按鈕?像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);
}
你在這裏問同樣的問題兩次嗎?如果是這樣,爲什麼不簡單地編輯和改進原來的帖子,而不是不必要地分開討論? – 2012-03-22 19:38:37
好抓@HovercraftFullOfEels。我發現他以前的問題更好,因爲它說明了他想做什麼以及爲什麼。所以投票結束這一個,並回答了另一個問題 – Robin 2012-03-22 22:45:39