我对Java Swing/AWT对话框还很陌生(这也解释了下面这个对话框看起来多么业余),希望有人能帮我更好地排列它 :) 当任意一个JButton被点击时,我都无法关闭这个popUp窗口。
我已经尝试过像frame.dispose()
、frame.setVisible(false)
甚至是 SwingUtilities.getWindowAncestor(this).dispose();
的选项。
再次说明,这是由另一个正在运行的主进程调用的二级弹出窗口,所以我只想关闭这个特定的popUp窗口而不影响主进程。否则我可以使用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
方法?传递给构造函数的Object ft
是什么?我猜测了一些东西,并尝试了一下,frame.dispose
似乎工作正常。 - Lauri Lehtinen