我是Java Swing / AWT的新手,我有这个代码后面的一个简单的PopUp对话框,该对话框关闭任何点击的JButtons,但显示真实不稳定。有没有人对什么以及如何修复提出建议?
import java.awt.Color;
import java.awt.Component;
import java.awt.Dimension;
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.Box.Filler;
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 newFrame;
FlasherThread flash;
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.setBackground(Color.DARK_GRAY);
textArea.setForeground(Color.WHITE);
textArea.setOpaque(false);
textArea.setVisible(visible);
textArea.setAlignmentX(Component.CENTER_ALIGNMENT);
add(textArea);
return textArea;
}
private UpgradePopupWindow(JFrame frame, Object ft) {
flash = (FlasherThread)ft;
String text = "An error occurred during the attempt to update your software. We recommend the following: (1) Restore your device to its previous version, (2) back up important data, and then (3) try updating your device again. If you continue with the current update, only your previously backed-up data will be available.";
addFiller(5);
addLabel(text, Font.PLAIN, 12);
//addText(text, true, Font.PLAIN);
addFiller(20);
newFrame = frame;
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);
setOpaque(true);
newFrame.setContentPane(this);
}
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;
}
protected void addFiller(int size) {
/*
* create some space before the progress bar
*/
Dimension diminsion = new Dimension(size, size);
Filler filler = new Filler(diminsion, diminsion, diminsion);
filler.setAlignmentX(Component.CENTER_ALIGNMENT);
add(filler);
}
public static void createGUI(Object obj) {
//Create and set up the frame.
JFrame frame = new JFrame("PopUp Dialog");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setPreferredSize(new Dimension(400, 200));
//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");
flash.setUpgradeRestoreChoice("restore");
newFrame.dispose();
} else if ("upgrade".equals(e.getActionCommand())) {
System.out.println("upgrade button selected");
flash.setUpgradeRestoreChoice("upgrade");
newFrame.dispose();
}
}
}alt text
答案 0 :(得分:4)
JOptionPane
而不是制作自己的选项对话框答案 1 :(得分:3)
不需要更改框架的contentPane
。您只需将JPanel
添加到框架即可。它将默认填充框架。
JLabel
不进行自动换行。
一种选择是手动插入换行符。 JLabel
接受HTML的一个子集:
String text = "<html>An error occurred during the attempt to update your software. <br />We recommend the following:<br />(1) Restore your device to its previous version,<br />(2) back up important data, and then<br />(3) try updating your device again.<br />If you continue with the current update, only your previously backed-up data will be available.</html>";
另一个选择是使用JTextArea
代替标签。它不接受HTML但它可以自动换行,你可以在文本中包含换行符以强制换行。
不要忘记删除边框并使背景透明(调用setOpaque(false)
仅适用于某些外观而非其他外观。)
无论哪种方式,您都需要在JPanel上设置布局。
以下是使用GridBagLayout
的示例:
import java.awt.Color;
import java.awt.Component;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
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.WindowConstants;
import javax.swing.Box.Filler;
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 newFrame;
FlasherThread flash;
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.setBackground(Color.DARK_GRAY);
textArea.setForeground(Color.WHITE);
textArea.setOpaque(false);
textArea.setVisible(visible);
textArea.setAlignmentX(Component.CENTER_ALIGNMENT);
add(textArea);
return textArea;
}
protected JTextArea addMultiLineLabel(String text, int fontStyle, int fontSize, Object constraints) {
JTextArea textArea = new JTextArea(text);
textArea.setFont(new Font("SansSerif", fontStyle, fontSize));
textArea.setLineWrap(true);
textArea.setWrapStyleWord(true);
textArea.setEditable(false);
textArea.setBackground(new Color(0, 0, 0, 0)); // Zero alpha = transparent background
textArea.setOpaque(false);
textArea.setBorder(null);
textArea.setAlignmentX(Component.CENTER_ALIGNMENT);
add(textArea, constraints);
return textArea;
}
private UpgradePopupWindow(JFrame frame, Object ft) {
super(new GridBagLayout());
GridBagConstraints gbc = new GridBagConstraints();
flash = (FlasherThread)ft;
String text = "An error occurred during the attempt to update your software.\nWe recommend the following:\n (1) Restore your device to its previous version,\n (2) back up important data, and then\n (3) try updating your device again.\nIf you continue with the current update, only your previously backed-up data will be available.";
addFiller(5);
gbc.gridy = 0;
gbc.gridx = 0;
gbc.gridwidth = 2;
gbc.weightx = 1.0;
gbc.fill = GridBagConstraints.HORIZONTAL;
addMultiLineLabel(text, Font.PLAIN, 12, gbc);
addFiller(20);
newFrame = frame;
gbc.gridy = 1;
gbc.gridwidth = 1;
gbc.fill = GridBagConstraints.NONE;
flashMe = new JButton(upgrade);
flashMe.setActionCommand("upgrade");
flashMe.addActionListener(this);
flashMe.setEnabled(true);
add(flashMe, gbc);
++ gbc.gridx;
helpMe = new JButton(restore);
helpMe.setActionCommand("restore");
helpMe.addActionListener(this);
helpMe.setEnabled(true);
add(helpMe, gbc);
setOpaque(true);
newFrame.add(this);
}
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;
}
protected void addFiller(int size) {
/*
* create some space before the progress bar
*/
Dimension diminsion = new Dimension(size, size);
Filler filler = new Filler(diminsion, diminsion, diminsion);
filler.setAlignmentX(Component.CENTER_ALIGNMENT);
add(filler);
}
public static void createGUI(Object obj) {
//Create and set up the frame.
JFrame frame = new JFrame("PopUp Dialog");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setPreferredSize(new Dimension(400, 200));
//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");
flash.setUpgradeRestoreChoice("restore");
newFrame.dispose();
} else if ("upgrade".equals(e.getActionCommand())) {
System.out.println("upgrade button selected");
flash.setUpgradeRestoreChoice("upgrade");
newFrame.dispose();
}
}
}
答案 2 :(得分:2)
我没有看到你使用任何类型的布局。 Swing设计适用于布局,然后使用这些布局中的项填充容器。我个人最喜欢的布局是MigLayout。
网页上有很多支持。一旦你开始使用布局,你的生活将变得更容易在摇摆设计。