Java 为什么我的简单JFrame显示的很奇怪?

Java 为什么我的简单JFrame显示的很奇怪?,java,swing,awt,jframe,jbutton,Java,Swing,Awt,Jframe,Jbutton,我是JavaSwing/AWT的新手,下面的代码用于创建一个简单的弹出对话框,该对话框在单击任何JButton时关闭,但显示出真正的不稳定。有没有人对修复什么和如何修复有什么建议 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.Acti

我是JavaSwing/AWT的新手,下面的代码用于创建一个简单的弹出对话框,该对话框在单击任何JButton时关闭,但显示出真正的不稳定。有没有人对修复什么和如何修复有什么建议

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

我看不到您使用任何布局。Swing design处理布局,然后用这些布局中的项目填充容器。我个人最喜欢使用的布局是


网页上有很多支持。一旦你开始使用布局,在swing中进行设计时,你的生活会变得轻松得多。

我没有看到你使用任何布局。Swing design处理布局,然后用这些布局中的项目填充容器。我个人最喜欢使用的布局是


网页上有很多支持。一旦开始使用布局,在swing中进行设计时,您的生活将变得轻松得多。

无需更改框架的
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>";

无需更改框架的
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>";
  • 您应该使用比默认布局管理器更好的布局管理器
  • 您应该使用
    JOptionPane
    而不是创建自己的选项对话框
  • 您应该使用比默认布局管理器更好的布局管理器
  • 您应该使用
    JOptionPane
    而不是创建自己的选项对话框

  • 你能描述一下它是如何表现出奇怪的样子吗?附加一张图片可能是最好的。就像我刚刚附加的截图一样,你能描述一下它是如何显示的吗?附加一张图片可能是最好的。就像我刚刚附加的截图一样。我几乎不了解BoxLayout,你能详细说明我如何在这个设置中引入任何类型的布局吗?我几乎不了解BoxLayout,你能详细说明我如何在这个设置中引入任何类型的布局吗,虽然我还有一个问题。什么控制此对话框的背景色?我想要默认的灰色/浅灰色默认背景色。此外,是否有方法规范化文本,使JTextArea的左右边界与对话框边缘均匀且等距?这里有一些提示:在JTextArea中更改文本对齐稍微复杂一些。看看有没有更好的方法用更简单的布局实现这个解决方案避免GridBagLayout?这很有帮助,尽管我还有一个问题。什么控制此对话框的背景色?我想要默认的灰色/浅灰色默认背景色。此外,是否有方法规范化文本,使JTextArea的左右边界与对话框边缘均匀且等距?这里有一些提示:在JTextArea中更改文本对齐稍微复杂一些。请参阅是否有更好的方法使用更简单的布局实现此解决方案避免GridBagLayout?