gpt4 book ai didi

java - 如何使用 JButton 单击事件关闭基于 JFrame 的窗口

转载 作者:塔克拉玛干 更新时间:2023-11-02 08:28:34 26 4
gpt4 key购买 nike

我是整个 Java Swing/AWT 对话框的新手(这解释了下面这个对话框看起来多么业余,欢迎任何帮助更好地安排它:),当两个 JButtons 中的任何一个时,我都在努力关闭这个弹出窗口被点击。

我已经尝试过像 frame.dispose()frame.setVisible(false) 甚至 SwingUtilities.getWindowAncestor(this).dispose() 这样的选项;

同样,这是一个由另一个正在运行的主进程调用的辅助弹出窗口,所以我只想关闭这个特定的弹出窗口而不影响主进程。否则我可以使用 System.exit

正如我提到的,任何其他改进对话框整体外观的建议都将受到赞赏。

我的全部代码如下:

import java.awt.Color;
import java.awt.Component;
import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextArea;
import javax.swing.SwingUtilities;

public class UpgradePopupWindow extends JPanel implements ActionListener {
public static UpgradePopupWindow mainWindow;

static final long serialVersionUID = 0;

final String upgrade = "Continue Upgrade";
final String restore = "Restore";

JPanel panels;
JButton flashMe;
JButton helpMe;
JTextArea Message;
JFrame frame;

protected JTextArea addText(String text, boolean visible, int fontStyle) {
JTextArea textArea = new JTextArea(text);

textArea.setFont(new Font("SansSerif", fontStyle, 12)); //$NON-NLS-1$

textArea.setLineWrap(true);
textArea.setWrapStyleWord(true);
textArea.setEditable(false);
textArea.setForeground(Color.WHITE);
textArea.setOpaque(false);
textArea.setVisible(visible);
textArea.setAlignmentX(Component.CENTER_ALIGNMENT);

add(textArea);

return textArea;
}

public UpgradePopupWindow(Object ft) {
String text = "This is the random text for now. I will bother about the actual content later";
addLabel(text, Font.PLAIN, 12);

flashMe = new JButton(upgrade);
flashMe.setActionCommand("upgrade");
flashMe.addActionListener(this);
flashMe.setEnabled(true);
add(flashMe);


helpMe = new JButton(restore);
helpMe.setActionCommand("restore");
helpMe.addActionListener(this);
helpMe.setEnabled(true);
add(helpMe);
}

protected JLabel addLabel(String text, int fontStyle, int size) {
JLabel label = new JLabel(text);
label.setFont(new Font("SansSerif", fontStyle, size));
label.setAlignmentX(Component.CENTER_ALIGNMENT);
label.setOpaque(false);
label.setVisible(true);
label.setForeground(Color.BLUE);

add(label);
return label;
}

public void createGUI(Object obj) {
//Create and set up the frame.
frame = new JFrame("PopUp Dialog");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
//create and setup the content pane
UpgradePopupWindow popUpContentPane = new UpgradePopupWindow(obj);

popUpContentPane.setOpaque(true);
frame.setContentPane(popUpContentPane);

frame.pack();
frame.setVisible(true);
}

public void actionPerformed(ActionEvent e) {
if("restore".equals(e.getActionCommand())) {
System.out.println("restore button selected");
frame.dispose();
SwingUtilities.getWindowAncestor(this).dispose();
} else if ("upgrade".equals(e.getActionCommand())) {
System.out.println("upgrade button selected");
frame.dispose();
}
}
}

最佳答案

问题是您的 createGUI 方法不是静态的。所以我想您首先创建一个 UpgradePopupWindow,在其上调用 createGUI,这又创建一个 enw UpgradePopupWindow。

试试这个:

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

public class TempTest
{
public static void main(String[] args)
{
UpgradePopupWindow.createGUI(null);
}
}


class UpgradePopupWindow extends JPanel implements ActionListener {
public static UpgradePopupWindow mainWindow;

static final long serialVersionUID = 0;

final String upgrade = "Continue Upgrade";
final String restore = "Restore";

JPanel panels;
JButton flashMe;
JButton helpMe;
JTextArea Message;
JFrame frame;


protected JTextArea addText(String text, boolean visible, int fontStyle) {
JTextArea textArea = new JTextArea(text);

textArea.setFont(new Font("SansSerif", fontStyle, 12)); //$NON-NLS-1$

textArea.setLineWrap(true);
textArea.setWrapStyleWord(true);
textArea.setEditable(false);
textArea.setForeground(Color.WHITE);
textArea.setOpaque(false);
textArea.setVisible(visible);
textArea.setAlignmentX(Component.CENTER_ALIGNMENT);

add(textArea);

return textArea;
}

public UpgradePopupWindow(JFrame frm, Object ft2) {
String text = "This is the random text for now. I will bother about the actual content later";
addLabel(text, Font.PLAIN, 12);
frame = frm;
flashMe = new JButton(upgrade);
flashMe.setActionCommand("upgrade");
flashMe.addActionListener(this);
flashMe.setEnabled(true);
add(flashMe);


helpMe = new JButton(restore);
helpMe.setActionCommand("restore");
helpMe.addActionListener(this);
helpMe.setEnabled(true);
add(helpMe);
}

protected JLabel addLabel(String text, int fontStyle, int size) {
JLabel label = new JLabel(text);
label.setFont(new Font("SansSerif", fontStyle, size));
label.setAlignmentX(Component.CENTER_ALIGNMENT);
label.setOpaque(false);
label.setVisible(true);
label.setForeground(Color.BLUE);

add(label);
return label;
}

public static void createGUI(Object obj) {
//Create and set up the frame.
JFrame frame = new JFrame("PopUp Dialog");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
//create and setup the content pane
UpgradePopupWindow popUpContentPane = new UpgradePopupWindow(frame, obj);

popUpContentPane.setOpaque(true);
frame.setContentPane(popUpContentPane);

frame.pack();
frame.setVisible(true);
}

public void actionPerformed(ActionEvent e) {
if("restore".equals(e.getActionCommand())) {
System.out.println("restore button selected");
frame.dispose();
SwingUtilities.getWindowAncestor(this).dispose();
} else if ("upgrade".equals(e.getActionCommand())) {
System.out.println("upgrade button selected");
frame.dispose();
}
}

}

主要变化是createUI是静态的,UpgradePopupWindow在构造函数中取frame。

关于java - 如何使用 JButton 单击事件关闭基于 JFrame 的窗口,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3114162/

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