2011-01-19 2 views
1

중첩 클래스는 중첩 된 클래스의 변수를 사용할 수 있으므로이 중첩 클래스를 사용해야했습니다. 이 클래스를 something.java로 옮겨 코드를 단순화하고 클래스는 여전히 Jlabel과 같은 GUI 클래스를 제어합니까?Swing 클래스에 너무 많은 중첩 클래스가 있습니다.

이 중요한 부분

public class GUI { 

     public GUI(){ 

      VitaminDEngineStarter vdes = new VitaminDEngineStarter(); 
      Registry registry = null; 
      try { 
       registry = LocateRegistry.getRegistry(); 
      } catch (RemoteException e1) { 
       // TODO Auto-generated catch block 
       e1.printStackTrace(); 
      } 

      try { 
       vd = (VitaminD)registry.lookup(VitaminD.SERVICE_NAME); 
      } catch(Exception e) { 
       e.printStackTrace(); 
      } 



      SMS a = new SMS(5); 
      try { 
       arduino.connect("COM3"); 
      } catch (Exception e) { 
       // TODO Auto-generated catch block 
       e.printStackTrace(); 
      } 
      System.out.println("connecting:"+ a.connect()); 
      System.out.println("connected? :" + a.checkConnect()); 
      System.out.println("signal: "+a.checkSignal()); 
      System.out.println("deliver report :" + a.DeliveryReportOn()); 
      SMS.Read read = a.new Read(arduino); 


     } 

     class ShowSense implements Runnable { 


      @Override 
      public void run() { 
       String[] temp; 
       String light = ""; 
       String temperature = ""; 
       String hum = ""; 
       String sens = ""; 
       boolean humanact = false; 

       // TODO Auto-generated method stub 
       while (true){ 
        try { 
         humanact = vd.gethumanActivity(); 
        } catch (RemoteException e) { 
         // TODO Auto-generated catch block 
         e.printStackTrace(); 
        } 
        sens = arduino.getSensor(); 
        temp = sens.split(","); 
        light = temp[1]; 
        temperature = temp[0]; 
        hum = temp[2]; 

        LightIntensity.setText(light); 
        Temperature.setText(temperature); 
        humidity.setText(hum); 


        if (humanact){ 
         personActivity.setText("in place"); 
        } 
        else{ 
         personActivity.setText("absent"); 
        } 

       } 
      } 

     } 

     private JPanel getInputs() { 
      if (Inputs == null) { 
       personActivity = new JLabel(); 
       personActivity.setBounds(new Rectangle(114, 137, 77, 27)); 
       personActivity.setText(""); 
       personActivityLabel = new JLabel(); 
       personActivityLabel.setBounds(new Rectangle(7, 137, 99, 25)); 
       personActivityLabel.setText("Person Activity:"); 
       humidity = new JLabel(); 
       humidity.setBounds(new Rectangle(106, 91, 84, 27)); 
       humidity.setText(""); 
       humidityLabel = new JLabel(); 
       humidityLabel.setBounds(new Rectangle(6, 92, 88, 26)); 
       humidityLabel.setText("Humidity:"); 
       Temperature = new JLabel(); 
       Temperature.setBounds(new Rectangle(101, 50, 89, 30)); 
       Temperature.setText(""); 
       TemperatureLabel = new JLabel(); 
       TemperatureLabel.setBounds(new Rectangle(4, 50, 91, 30)); 
       TemperatureLabel.setText("Temperature:"); 
       LightIntensity = new JLabel(); 
       LightIntensity.setBounds(new Rectangle(110, 6, 84, 34)); 
       lightLabel = new JLabel(); 
       lightLabel.setBounds(new Rectangle(5, 5, 97, 34)); 
       lightLabel.setText("Light Intensity:"); 
       Inputs = new JPanel(); 
       Inputs.setLayout(null); 
       Inputs.setBounds(new Rectangle(14, 63, 200, 183)); 
       Inputs.add(lightLabel, null); 
       Inputs.add(LightIntensity, null); 
       Inputs.add(TemperatureLabel, null); 
       Inputs.add(Temperature, null); 
       Inputs.add(humidityLabel, null); 
       Inputs.add(humidity, null); 
       Inputs.add(personActivityLabel, null); 
       Inputs.add(personActivity, null); 
       th.start(); 
      } 
      return Inputs; 
     } 

     class autopilotthread implements Runnable{ 

      /** The temp. */ 
      private String[] temp; 

      /** The lightintensty. */ 
      private double lightintensty ; 

      /** The temperature. */ 
      private double temperature ; 

      /** The hum. */ 
      private double hum ; 

      /** The sens. */ 
      private String sens = null; 

      /** The humanact. */ 
      private double humanact; 

      /** The result. */ 
      private boolean [] result = {false , false}; 

      /** The fan. */ 
      private boolean fan =false; 

      /** The light. */ 
      private boolean light = false; 

      /** The pstop. */ 
      boolean pstop = false; 

      /* (non-Javadoc) 
      * @see java.lang.Runnable#run() 
      */ 
      @Override 
      public void run() { 
       System.out.println("thread start!"); 
       while(true){ 
        System.out.println("thread loop!"); 
         try { 
          if(vd.gethumanActivity()){ 
           humanact = 250; 
          }else{ 
           humanact = 0; 
          } 
         } catch (RemoteException e) { 
          // TODO Auto-generated catch block 
          e.printStackTrace(); 
         } 
         sens = arduino.getSensor(); 
         temp = sens.split(","); 
         lightintensty = Double.parseDouble(temp[1]); 
         temperature = Double.parseDouble(temp[0]); 
         hum = Double.parseDouble(temp[2]); 
         double [] out ={humanact ,lightintensty , hum, Time.now(),temperature }; 
         System.out.println(""+out[0]+" "+out[1]+" "+out[2]+" "+out[3]+" "+out[4]); 
         result = Matlab.output(out); 
         light = result[1]; 
         fan = result[0]; 
         System.out.println("light:" + light); 
         System.out.println("fan:" + fan); 
         if(light){ 
          try {X10.lightsOn();} 
          catch (IOException e) {e.printStackTrace();} 

         }else{ 
          try {X10.lightsOff();} 
          catch (IOException e) {e.printStackTrace();} 

         } 

         if(fan){ 
          try {X10.fanOn();} 
          catch (IOException e) {e.printStackTrace();} 

         }else{ 
          try {X10.fanOff();} 
          catch (IOException e) {e.printStackTrace();} 

         } 



        try {TimeUnit.SECONDS.sleep(10);} 
        catch (InterruptedException e) {e.printStackTrace();} 

        if (pstop){ 
         break; 
        } 
       } 
       System.out.println("thread stop!"); 

      } 

     } 

     class Pilotmouse implements MouseListener{ 

      /** The p thread. */ 
      autopilotthread pThread = null; 

      /** The pt. */ 
      Thread pt = null; 

      /** 
      * Instantiates a new pilotmouse. 
      */ 
      Pilotmouse(){ 

      } 

      /* (non-Javadoc) 
      * @see java.awt.event.MouseListener#mouseClicked(java.awt.event.MouseEvent) 
      */ 
      @Override 
      public void mouseClicked(MouseEvent arg0) { 
       // TODO Auto-generated method stub 

      } 

      /* (non-Javadoc) 
      * @see java.awt.event.MouseListener#mouseEntered(java.awt.event.MouseEvent) 
      */ 
      @Override 
      public void mouseEntered(MouseEvent arg0) { 
       // TODO Auto-generated method stub 

      } 

      /* (non-Javadoc) 
      * @see java.awt.event.MouseListener#mouseExited(java.awt.event.MouseEvent) 
      */ 
      @Override 
      public void mouseExited(MouseEvent arg0) { 
       // TODO Auto-generated method stub 

      } 

      /* (non-Javadoc) 
      * @see java.awt.event.MouseListener#mousePressed(java.awt.event.MouseEvent) 
      */ 
      @Override 
      public void mousePressed(MouseEvent arg0) { 
       // TODO Auto-generated method stub 

      } 

      /* (non-Javadoc) 
      * @see java.awt.event.MouseListener#mouseReleased(java.awt.event.MouseEvent) 
      */ 
      @Override 
      public void mouseReleased(java.awt.event.MouseEvent e) { 
       if ((autopilotlable.getText().equalsIgnoreCase("off"))){ 
        autopilotlable.setText("on"); 
        pThread = new autopilotthread(); 
        pt = new Thread(pThread); 
        pt.start(); 

       } else if ((autopilotlable.getText().equalsIgnoreCase("on"))){ 
        autopilotlable.setText("off"); 
        pThread.pstop = true; 
       } 
      } 

     } 


     private JButton getAutopilot() { 


      if (autopilot == null) { 
       autopilot = new JButton(); 
       autopilot.setBounds(new Rectangle(18, 14, 112, 28)); 
       autopilot.setText("Auto Pilot"); 

       autopilot.addMouseListener(new Pilotmouse()); 
      } 
      return autopilot; 
     } 


     public static void main(String[] args) { 
      SwingUtilities.invokeLater(new Runnable() { 
       public void run() { 
        GUI application = new GUI(); 
        application.getJFrame().setVisible(true); 
       } 
      }); 

     } 

    } 
+3

당신이 예를 보여 주시겠습니까? –

답변

2

Jochen이 Eclipse Refactoring Tools를 사용할 수 있다고 언급했듯이. 그래도 디자인 문제는 해결되지 않습니다.

일반적으로 :

  • GUI는 약 14 종류를 알고 약간 높은 수준 클래스로 ... 로 큰 중재자 역할이 그룹 로직 가능하며 GUI를 사용할습니까?
  • 코드는 논리와 함께 객체 구성 을 혼합합니다. 해당 작업을 다른 곳으로 이동하십시오.
  • 생성자 안에 스레드를 시작하지 마십시오!
  • 논리 문 사용을 수정합니다. 예 : : while (true) {... if (pstop) {break; }}
  • X10 타사 클래스 또는 나만의 클래스입니까? 그것은 당신이 세계 상태를 제거하려고 있다면 (예 : 상태 변화 X10 및 인스턴스 변수의 단일 인스턴스를 사용하지 않습니다 정적들.)

가장 중요한 조언 :이 기능에 대한 단위 테스트를 작성하려고하세요 처음으로 예를 들어 junit과 mockito를 사용하면 좋은 디자인이 자연스럽게 나옵니다. 나를 믿어!

편집 :

Good talk about JMM

Good book about concurrency.

편집 :

The Clean Code Talks - "Global State and Singletons"

+0

감사합니다. petro는 왜 생성자 내부에서 스레드를 시작하지 않을 수 있습니까? 나는 그것을 메인에서 시작해야합니까? 그러나 스레드가 GUI를 닫은 후에 중지해야합니다 .... – wizztjh

+2

우선 미안 해요. 코드에서 생성자 내부에서 스레드를 시작하지 않습니다. 이미이 주제를 다루었 기 때문에 Brian Goetz의 기사입니다 : http://www.ibm.com/developerworks/java/library/j-jtp0618.html –

+0

안녕하십니까, 제 프로그램에는 Listing 1에있는 문제점이 있습니다. Brian Goetz의 기사,하지만 어떻게 해결할 수 있을까요? 휘발성을 사용하고 있습니까? 또는 동기화 "? – wizztjh

1

당신은 자신의 파일에 내부 클래스의 일부를 이동 ... 새 파일을 이클립스의 Refactor-> 이동 유형을 사용할 수를 표시하는 청소 버전입니다.

관련 문제