我在JFrame中添加了ComponentListener,并在其componentResized()方法中保留了一些代码,这些代码使应用程序执行无限窗口。这是因为我在componentResized()方法内创建了该类的对象。在这里我想做的是访问其内部类BottomPanel()的方法。我在这里附加了我的可执行代码。

  package nonResponsiveButtons;

    import java.awt.Color;
    import java.awt.event.ComponentEvent;
    import java.awt.event.ComponentListener;
    import javafx.embed.swing.JFXPanel;
    import javafx.event.ActionEvent;
    import javafx.event.EventHandler;
    import javafx.scene.Scene;
    import javafx.scene.control.Button;
    import javafx.scene.layout.HBox;
    import javafx.scene.layout.Pane;
    import javafx.application.Platform;

    import javax.swing.JFrame;
    import javax.swing.JPanel;
    import javax.swing.SwingUtilities;

    public class NonResponsiveButtons extends JFrame {
    private int applicationWidth_600 = 600;
    private int applicationHeight_600 = 600;

    private int upperPanelHeight_500 = 500;
    private int bottomPanelHeight_100 = (applicationWidth_600-upperPanelHeight_500);

    /**
     *
     * @return the height of the jframe
     */
    public int getJframeHeight(){
        return this.getHeight();
    }

    /**
     *
     * @return the width of the jframe
     */
    public int getJframeWidth(){
        return this.getWidth();
    }

    public static void main(String[] args) {
        new NonResponsiveButtons();
    }



    public NonResponsiveButtons(){

        SwingUtilities.invokeLater(new Runnable() {

            @Override
            public void run() {
                // TODO Auto-generated method stub
                try{
                new JFXPanel();
                BottomPanel bottomPanel = new BottomPanel();
                add(bottomPanel);
                }
                catch(Exception e){
                    System.out.println("Error in swing utilities thread :" + e.getMessage());
                }

            }
        });

        this.setSize(applicationWidth_600, applicationHeight_600);
        this.addComponentListener(new ComponentListener() {

            @Override
            public void componentShown(ComponentEvent arg0) {
                // TODO Auto-generated method stub


            }

            //gets executed every time
            @Override
            public void componentResized(ComponentEvent arg0) {
                // TODO Auto-generated method stub
                System.out.println("Component resized");
                NonResponsiveButtons nrb = new NonResponsiveButtons();  //this code creates a new window

                NonResponsiveButtons.BottomPanel nonResponsiveBottmPanel = nrb.new BottomPanel();  //this code always creates a new window

                nonResponsiveBottmPanel.centeriseBottomPanelComponents();

            }

            @Override
            public void componentMoved(ComponentEvent arg0) {
                // TODO Auto-generated method stub

            }

            @Override
            public void componentHidden(ComponentEvent arg0) {
                // TODO Auto-generated method stub

            }
        });
        getContentPane().setLayout(null);
        add(jPanel());
        setVisible(true);
    }



    private JPanel jPanel(){
        JPanel panel = new JPanel();
        panel.setSize(applicationWidth_600,upperPanelHeight_500);
        panel.setBackground(Color.blue);
        return panel;
    }

    private class BottomPanel extends JPanel{
        private JFXPanel jfxPanel;
        private HBox scenePane;
        private Button btn1;
        private Button btn2;
        private Button btn3;

        private BottomPanel(){
            setSize(applicationWidth_600, bottomPanelHeight_100);
            setLocation(0, upperPanelHeight_500);
            setLayout(null);



            Platform.runLater(new Runnable(){
                @Override
                public void run(){
                    /// putting all the buttons inside the scenePane
                    getScenePane().getChildren().addAll(getBtn1(),getBtn2(),getBtn3());
                    ///adding scenePane to the fxPanel
                    getjfxPanel().setScene(new Scene(getScenePane()));
                }

            });

            add(getjfxPanel());
        }

        public void centeriseBottomPanelComponents() {
            getJframeHeight();
            getJframeWidth();
            System.out.println("Jframe's height is : " + getJframeHeight() + " & " + "Jframe's width is : " + getJframeWidth());
        }

    /**
     * Disposing the components of the bottom panel
     */
        private void disposeBottomPanel(){
            getScenePane().getChildren().removeAll();
        }

        private JFXPanel getjfxPanel(){
            if(jfxPanel == null){
                jfxPanel = new JFXPanel();
                jfxPanel.setSize(applicationWidth_600,bottomPanelHeight_100);

            }
            return jfxPanel;
        }

        private HBox getScenePane(){
            if(scenePane == null){
                scenePane = new HBox();
                scenePane.setStyle("-fx-background-color:#666666");
                scenePane.setLayoutX(100);
                scenePane.setSpacing(50);
            }
            return scenePane;
        }

        /*
         * using getters will avoid :-
         * 1. null pointer exceptions
         * 2. standard coding format
         * 3. makex programming felxible
         */
        private Button getBtn1(){
            if(btn1 == null){
                btn1 = new Button("Button 1");
                btn1.setPrefSize(100, 50);
                btn1.setLayoutX(80);

                btn1.setOnAction(new EventHandler<ActionEvent>() {

                    @Override
                    public void handle(ActionEvent arg0) {
                        // TODO Auto-generated method stub
                        try{
                        disposeBottomPanel();
                        } catch(Exception e){
                            System.out.println("Error disposing the components :" + e.getMessage());
                        }
                        System.out.println("Btn1 event handler");
                    }
                });
            }
            return btn1;
        }
        private Button getBtn2(){
            if(btn2 == null){
                btn2 = new Button("Button 2");
                btn2.setPrefSize(100, 50);
                btn2.setLayoutX(80);
            }
            return btn2;
        }
        private Button getBtn3(){
            if(btn3 == null){
                btn3 = new Button("Button 3");
                btn3.setPrefSize(100, 50);
                btn3.setLayoutX(80);
            }
            return btn3;
        }
    }


}

最佳答案

声明类字段BottomPanel bottomPanel = new BottomPanel();并在初始化时仅创建一次,而不是创建局部变量BottomPanel bottomPanelInstance;

在您的componentResized()方法中,只需访问字段bottomPanelInstance并调用

bottomPanelInstance.centeriseBottomPanelComponents();

08-03 19:52