Processing中如何关闭子窗口而不终止整个Sketch程序
解决Processing子窗口关闭导致整个程序终止的问题
要实现关闭子窗口时不终止整个Processing程序,核心是修改子窗口的默认关闭行为,将其从“退出整个程序”改为“仅销毁当前窗口”。具体操作如下:
关键修改步骤
- 获取子窗口的JFrame实例,修改其默认关闭操作
- 在
ChildApplet类的setup()方法中添加代码,获取窗口的JFrame,设置DefaultCloseOperation为JFrame.DISPOSE_ON_CLOSE,替代默认的EXIT_ON_CLOSE - 如果需要通过代码关闭子窗口,调用
dispose()方法而非exit()
修改后的完整代码
import controlP5.*; import javax.swing.JFrame; // 引入JFrame类 ControlP5 cp5; PFont f_bebasNeueBold; int resW = 1600; int resH = 900; int numOfLayouts = 0; StringList layoutsFileData; StringList testingArr; String currentSpreadTitle = "null"; String parentPath; ChildApplet newLayoutWindow; //basic ui initialization ui_window programHeader = new ui_window(0,0,resW,75,(#1D4077)); ui_window toolBar = new ui_window(0,75,resW,35,(#12284B)); ui_window infoPanel = new ui_window(15,programHeader.windowHeight + toolBar.windowHeight + 15,400,resH - 30 - programHeader.windowHeight - toolBar.windowHeight,(#12284B)); ui_window infoPanelHeader = new ui_window(15,infoPanel.windowYPos,400,60,(#1D4077)); //basic ui initialization void setup() { size(1600,900); surface.setTitle("WarBoard - V.0.1"); background(#F2F4F3); parentPath = sketchPath(); //controlP5 elements cp5 = new ControlP5(this); ControlFont cf_buttonFont = new ControlFont(createFont("Bebas Neue Pro Bold.otf",14)); cp5.addButton("buttonNewLayout") .setValue(0) .setPosition(10,toolBar.windowYPos + (toolBar.windowHeight / 2) - 10) .setSize(100,20) .setCaptionLabel("New Layout") .setColorBackground(color(#F2F4F3)) .setColorForeground(color(#BFBFBF)) .setColorActive(color(#999999)) .setColorCaptionLabel(color(#12284B)) .setFont(cf_buttonFont) .setBroadcast(false) .addCallback(new CallbackListener() { public void controlEvent(CallbackEvent event) { if (event.getAction() == ControlP5.ACTION_RELEASED) { launchNewLayoutWindow(); } } } ); //controlP5 elements layoutsFileData = new StringList(); testingArr = new StringList(); f_bebasNeueBold = createFont("Bebas Neue Pro Bold.otf", 1000); if (checkFileStructure()) { String tempFileData[] = loadStrings("data/layouts.txt"); for (int i = 0; i < tempFileData.length; i++) { layoutsFileData.append(tempFileData[i]); } } else { String setupFileStruct = "0|"; String[] fileStruct = split(setupFileStruct, '|'); saveStrings("data/layouts.txt", fileStruct); String tempFileData[] = loadStrings("data/layouts.txt"); for (int i = 0; i < tempFileData.length; i++) { layoutsFileData.append(tempFileData[i]); } numOfLayouts = 0; } numOfLayouts = int(layoutsFileData.get(0)); testingArr.append("1"); testingArr.append("2"); newLayout("Test Layout", testingArr); } void draw() { programHeader.update(); toolBar.update(); infoPanel.update(); infoPanelHeader.update(); easyText("WARBOARD", width/2, (programHeader.windowHeight / 2) - 15, 32, (#F2F4F3), CENTER, CENTER); easyText("VERSION 0.1", width/2, (programHeader.windowHeight / 2) + 15, 24, (#F2F4F3), CENTER, CENTER); easyText(currentSpreadTitle, infoPanelHeader.windowXPos + (infoPanelHeader.windowWidth / 2), infoPanelHeader.windowYPos + (infoPanelHeader.windowHeight / 2), 32, (#F2F4F3), CENTER, CENTER); } class ui_window { color fillColor; float windowWidth, windowHeight; float windowXPos, windowYPos; ui_window (float x, float y, float inputWidth, float inputHeight, color inputColor) { windowXPos = x; windowYPos = y; windowWidth = inputWidth; windowHeight = inputHeight; fillColor = inputColor; } void update() { noStroke(); fill(fillColor); rect(windowXPos, windowYPos, windowWidth, windowHeight); } } void easyText(String textDisplay, float textXPos, float textYPos, float textFontSize, color textColor, int textFontAlignX, int textFontAlignY) { noStroke(); fill(textColor); textFont(f_bebasNeueBold); textSize(textFontSize); textAlign(textFontAlignX, textFontAlignY); text(textDisplay, textXPos, textYPos); } void newLayout(String layoutName, StringList layoutPositions) { layoutsFileData.append("#" + str(numOfLayouts)); layoutsFileData.append(layoutName); for (int i = 0; i < layoutPositions.size(); i++) { layoutsFileData.append(layoutPositions.get(i)); } layoutsFileData.append(""); numOfLayouts += 1; layoutsFileData.set(0, str(numOfLayouts)); saveAndUpdate(); } void saveAndUpdate() { saveStrings("data/layouts.txt", layoutsFileData.toArray()); } boolean checkFileStructure() { InputStream input = createInput("layouts.txt"); if (input == null) { return false; } else { return true; } } public void launchNewLayoutWindow() { newLayoutWindow = new ChildApplet(2, displayWidth / 2 - 400, displayHeight / 2 - 225, 800, 450); } // New Layouts Window -------------------------------------------------------- class ChildApplet extends PApplet { int id, vx, vy, vw, vh; int bckColor; PFont f_bebasNeueBoldNewLayout; void easyTextNewLayout(String textDisplay, float textXPos, float textYPos, float textFontSize, color textColor, int textFontAlignX, int textFontAlignY) { noStroke(); fill(textColor); textFont(f_bebasNeueBoldNewLayout); textSize(textFontSize); textAlign(textFontAlignX, textFontAlignY); text(textDisplay, textXPos, textYPos); } ChildApplet(int id, int vx, int vy, int vw, int vh) { super(); this.id = id; this.vx = vx; this.vy = vy; this.vw = vw; this.vh = vh; PApplet.runSketch(new String[] {this.getClass().getName()}, this); } void settings() { size(vw, vh, P2D); smooth(0); println("Creating window "+ id); } void setup() { windowMove(vx, vy); windowResizable(true); // *关键修改:设置子窗口关闭行为* JFrame frame = (JFrame)this.getSurface().getNative(); frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE); cp5 = new ControlP5(this); f_bebasNeueBoldNewLayout = createFont(parentPath + "/data/" + "Bebas Neue Pro Bold.otf", 20); ControlFont cf_inputFont = new ControlFont(createFont(parentPath + "/data/" + "Bebas Neue Pro Bold.otf",20)); cp5.addTextfield("input") .setCaptionLabel("") .setColorCaptionLabel(color(#12284B)) .setPosition(15,40) .setSize(400,40) .setFocus(true) .setColor(color(#F2F4F3)) .setFont(cf_inputFont) ; } void draw() { background(#F2F4F3); windowTitle("WarBoard - Create New Layout"); easyTextNewLayout("Layout Name:", 15,15,20,color(#12284B),LEFT,TOP); } // 可选:添加代码关闭子窗口的方法 void closeWindow() { this.getSurface().getNative().dispose(); } } // New Layouts Window --------------------------------------------------------
说明
- 首先需要引入
javax.swing.JFrame类,用于操作窗口的关闭行为 - 在
ChildApplet的setup()中,通过getSurface().getNative()获取窗口的原生JFrame实例 JFrame.DISPOSE_ON_CLOSE会在关闭窗口时释放该窗口占用的资源,但不会终止整个Processing程序- 如果需要通过代码触发子窗口关闭,调用
closeWindow()方法即可,避免使用exit()(会终止整个程序)
内容的提问来源于stack exchange,提问作者ColtTheNark
相关产品推荐
相关产品推荐

