Java – 当 JFrame 关闭时如何打印消息?

huangapple go评论78阅读模式
英文:

Java - How to print message when a JFrame is closed?

问题

ColorChanger类(位于主方法的末尾,在要求用户输入颜色后):

JFrame application = new JFrame();
application.setLocation(250, 250);
application.setSize(400, 150);
application.setTitle("颜色变换器");
application.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
application.setLocationRelativeTo(null);
ColorPanel panel = new ColorPanel(colorAmountRed, colorAmountGreen, colorAmountBlue);
application.add(panel);

application.setVisible(true);
System.out.println("GUI已关闭。");

panel的构造方法位于名为ColorPanel的类内部:

import javax.swing.JPanel;
import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics;

public class ColorPanel extends JPanel {
    private int red;
    private int green;
    private int blue;

    public ColorPanel(int red, int green, int blue) {
        this.red = red;
        this.green = green;
        this.blue = blue;
        this.setBackground(new Color(this.red, this.green, this.blue));
    }

    public void paintComponent(Graphics g) {
        super.paintComponent(g);    // 调用父类的paintComponent方法(即JPanel本身)

        int fontSize = 26;    // 作为参数传递的字体大小
        g.setFont(new Font("TimesRoman", Font.PLAIN, fontSize));

        int redContrast = 0;
        if (red >= 0 && red <= 123) {
            redContrast = 255;
        }

        int greenContrast = 0;
        if (green >= 0 && green <= 123) {
            greenContrast = 255;
        }

        int blueContrast = 0;
        if (blue >= 0 && blue <= 123) {
            blueContrast = 255;
        }
        this.setForeground(new Color(redContrast, greenContrast, blueContrast));
        g.drawString("Java GUI One", getWidth() / 2 - 75, getHeight() / 2 + 10);
    }
}

我相当确定使用println是正确的方法,但我不确定它应该放在何处以便在GUI关闭后显示。这是我第一次使用Java(或StackOverflow)。对于任何格式问题等,我表示歉意。

英文:

I'm working through an assignment for a beginner Java class. We're only about halfway through the semester and just learned about JFrames this week. Our assignment had us create a GUI for a program to change colors on screen from a previous week and asks us to print a message to the console saying that the GUI has closed when the user exits the GUI. My current code prints this message as the GUI is opened instead of when it is closed. This creation of a JFrame takes place inside a public class called ColorChanger where javax.swing.JFrame is imported.

ColorChanger Class (at the bottom of main under user prompts for colors):

JFrame application = new JFrame();
application.setLocation(250, 250);
application.setSize(400, 150);
application.setTitle(&quot;Color Changer&quot;);
application.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
application.setLocationRelativeTo(null);
ColorPanel panel = new ColorPanel(colorAmountRed, colorAmountGreen, colorAmountBlue);
application.add(panel);
application.setVisible(true);
System.out.println(&quot;The GUI has closed.&quot;);

The constructor for panel is located inside a class called ColorPanel:

import javax.swing.JPanel;
import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics;
public class ColorPanel extends JPanel {
private int red;	
private int green;
private int blue;
public ColorPanel(int red, int green, int blue) {
this.red = red;
this.green = green;
this.blue = blue;
this.setBackground(new Color (this.red, this.green, this.blue));	
}
public void paintComponent(Graphics g) {
super.paintComponent(g);	// Calls parent paintComponent (JPanel itself)
int fontSize = 26;	// Font Size sent as a parameter
g.setFont(new Font(&quot;TimesRoman&quot;, Font.PLAIN, fontSize));
int redContrast = 0;
if (red &gt;= 0 &amp;&amp; red &lt;= 123) {
redContrast = 255;
}
int greenContrast = 0;
if (green &gt;= 0 &amp;&amp; green &lt;= 123) {
greenContrast = 255;
}
int blueContrast = 0;
if (blue &gt;= 0 &amp;&amp; blue &lt;= 123) {
blueContrast = 255;
}
this.setForeground(new Color (redContrast, greenContrast, blueContrast));
g.drawString(&quot;Java GUI One&quot;, getWidth() / 2 - 75, getHeight() / 2 + 10 );
}
}	

I'm fairly certain that a println is the way to go, but I'm unsure of where it needs to go in order to appear AFTER the GUI is closed. This is my first time ever working with Java (or StackOverflow, for that matter). Apologies for any formatting issues, etc.

答案1

得分: 1

使用以下代码行:

application.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

你告诉Java在窗口关闭时关闭自身。如果要在GUI关闭后打印消息,你不应该在GUI关闭时关闭Java:

application.setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);

然后添加一个“退出监听器”:

application.addWindowListener(new java.awt.event.WindowAdapter() {
    @Override
    public void windowClosing(java.awt.event.WindowEvent windowEvent) {
        System.out.println("GUI已关闭");
        //然后你可以退出程序
        System.exit(0);
    }
});
英文:

With the line

application.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

you tell to Java to close itself when the frame closes. To print a messsage after the GUI closes, you should not close Java when your GUI closes:

application.setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);

and then add an "exit listener":

application.addWindowListener(new java.awt.event.WindowAdapter() {
@Override
public void windowClosing(java.awt.event.WindowEvent windowEvent) {
System.out.println(&quot;GUI has been closed&quot;);
//THEN you can exit the program
System.exit(0);
}
});

huangapple
  • 本文由 发表于 2020年3月4日 06:55:31
  • 转载请务必保留本文链接:https://go.coder-hub.com/60516720.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定