gpt4 book ai didi

java - 在 JFrame 中使用 ComponentListener() 的 componentResized() 时,JFrame 执行无限次

转载 作者:行者123 更新时间:2023-12-01 12:32:18 28 4
gpt4 key购买 nike

我在 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(); 

关于java - 在 JFrame 中使用 ComponentListener() 的 componentResized() 时,JFrame 执行无限次,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25844814/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com