我的 Swing 类中有太多嵌套类

发布于 2024-10-13 01:27:10 字数 10549 浏览 5 评论 0原文

我需要使用这些嵌套类,因为嵌套类可以使用嵌套类中的变量。如何将这些类移动到 some.java 以简化我的代码,并且该类仍然具有 gui 类的控制权,例如 Jlabel?

这是清理后的版本,用于显示重要部分

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);
                }
            });

        }

    }

I needed to use these nested class because nested class can use the variable from the class been nested. How do I move these class to a something.java to simplify my code and the class still have the control of the gui class , such as Jlabel?

this is the cleaned version to show the important part

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);
                }
            });

        }

    }

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

为人所爱 2024-10-20 01:27:11

您可以使用 Eclipse 的 Refactor->Move Type to New File... 将某些内部类移动到它们自己的文件中。

You could use Eclipse's Refactor->Move Type to New File... to move some of the inner classes to their own file.

吃不饱 2024-10-20 01:27:10

正如 Jochen 提到的,您可以使用 Eclipse 重构工具。但这并不能解决设计问题。

一般来说:

  • GUI 知道 14 个类和行为
    作为大调解人...有可能吗
    将逻辑分组到更高的层次
    类并让 GUI 使用它们?
  • 您的代码混合了对象构造
    与逻辑。尝试移动这些动作
    到不同的地方
  • 不要在里面启动线程
    构造函数!
  • 修改逻辑的用法
    声明。例如。 :
    while(true){ ...如果
    (pstop){ 中断; X10
  • 是第 3 方类还是您自己的类?
    如果是你的尝试删除全局
    状态(例如使用 X10 的单个实例
    和状态的实例变量
    改变。非静态的)

最重要的建议:请尝试使用(例如)junit 和mockito首先为此功能编写单元测试,好的设计就会自然而然地出现。相信我!

编辑:

关于 JMM 的好谈

关于并发的好书

编辑:

清洁代码会谈 - “全局状态和单例”

As Jochen mentioned you could use Eclipse Refactoring Tools. That won't solve design issues though.

Generally:

  • GUI knows about 14 classes and acts
    as big mediator... Is it possible to
    group logic into some higher level
    classes and have GUI use them?
  • Your code mixes object construction
    with logic. Try to move those actions
    to different places
  • Don't start a thread inside
    constructor!
  • Revise usage of logical
    statements. Eg. :
    while(true){ ... if
    (pstop){ break; }}
  • Is X10 3rd party class or your own?
    If it's yours try to remove global
    state(eg use single instance of X10
    and instance variables for state
    change. Not static ones)

The most important advise: Please try to write unit tests for this functionality first using (for example) junit and mockito, and good design will come naturally. Believe me!

EDIT:

Good talk about JMM

Good book about concurrency.

EDIT:

The Clean Code Talks - "Global State and Singletons"

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文