2012-03-14 73 views
0

我爲每個我正在創建的Tab添加了一個加熱器JButton和一個加熱器JSlider,加熱器JButton將它關閉並打開,加熱器JSlider選擇溫度。從另一個類訪問JSlider的GUI

的JSlider的狀態被預設爲假,所以當我點擊加熱器的JButton打開加熱器,

我需要它來設置JSlider的爲TRUE的狀態,我怎麼可以訪問加熱器JSlider的HEATER BUTTON類?如果不是,還有什麼其他方式可以做到這一點?

任何幫助表示讚賞,謝謝。

import java.util.ArrayList; 
import java.awt.*; 
import javax.swing.*; 
import java.awt.event.*; 

public class MasterGUI extends JFrame implements ActionListener{ 

public MasterGUI(){ 

} 

public void DisplayFrame(){ 
    ArrayList<Rooms> rooms; 
    rooms = Building.getRoomList(); 

    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(); 
    JPanel tmpPanel; 

    for(int x = 0; x < rooms.size(); x++){ 
     tmpPanel = new JPanel(); 
     String roomName = rooms.get(x).getName(); 
     int roomId = rooms.get(x).getId(); 
     tabbedPane.addTab(roomName + " Room " + roomId, tmpPanel); 

     for(int i = 0; i < rooms.get(x).roomLights.size(); i++){ 
      int lightId = rooms.get(x).roomLights.get(i).getId(); 
      JButton lights = new JButton("Light" + lightId); 
      lights.setBackground(Color.red); 
      lights.addActionListener(new LightButton(roomId, lightId)); 
      ((JPanel) tabbedPane.getComponentAt(x)).add(lights); 
     } 

     JButton heater = new JButton("Heater"); 
     heater.setBackground(Color.red); 
     heater.addActionListener(new HeaterButton(roomId)); 
     JSlider heaterSlider = new JSlider(68, 73); 
     heaterSlider.setPaintTicks(true); 
     heaterSlider.setPaintLabels(true); 
     heaterSlider.setMajorTickSpacing(1); 
     heaterSlider.addChangeListener(new HeaterSlider(roomId)); 
     heaterSlider.setEnabled(false); 
     ((JPanel) tabbedPane.getComponentAt(x)).add(heater); 
     ((JPanel) tabbedPane.getComponentAt(x)).add(heaterSlider); 
    } 
    master.add(tabbedPane, BorderLayout.CENTER); 
    master.setSize(800, 600); 
    content.add(tabbedPane); 
    master.setVisible(true); 
} 

HEATERBUTTON CLASS

import java.util.ArrayList; 
import java.awt.*; 
import javax.swing.*; 
import java.awt.event.*; 

public class HeaterButton implements ActionListener{ 
int roomNumber; 

public HeaterButton(int room){ 
roomNumber = room; 
} 

public void actionPerformed(ActionEvent e){ 
ArrayList<Rooms> rooms; 
rooms = Building.getRoomList(); 

    if(rooms.get(roomNumber - 1).roomHeater.getHeaterStatus() == true){ 
     rooms.get(roomNumber - 1).roomHeater.setHeaterOff(); 
     ((JButton)e.getSource()).setBackground(Color.red); 

    }else{ 
     rooms.get(roomNumber - 1).roomHeater.setHeaterOn(); 
     ((JButton)e.getSource()).setBackground(Color.green); 

    } 
} 
} 
+0

你應該看看到命令模式(http://en.wikipedia.org/wiki/Command_pattern)。 HeaterButton類有點混亂,因爲它沒有按鈕。 Swing有一個Action類,它可以用作接口而不是ActionListener。這樣你可以給按鈕一個特定的動作來執行:) – Terraego 2012-03-14 23:16:01

回答

2

首先創建滑塊,然後將它傳遞給該按鈕。

變化:

heater.addActionListener(new HeaterButton(roomId)); 

要:

heater.addActionListener(new HeaterButton(roomId, slider));