JFrame 的内容未显示

Content of JFrame not showing

当用户在一个 JFrame frame 中点击 "close" 时,我希望 JFrame credits 在程序退出前显示 2.5 秒,显示我的名字和内容。现在,credits 正在显示,但没有 textAreaButton 是空的 - 找不到问题所在。

这是我的代码: 对于frame

的关闭操作
frame.addWindowListener(new java.awt.event.WindowAdapter() {
                    @Override
                    public void windowClosing(java.awt.event.WindowEvent windowEvent) {
                        int result = JOptionPane.showConfirmDialog(null, "Sind Sie sicher?", "Schließen", JOptionPane.YES_NO_OPTION, JOptionPane.QUESTION_MESSAGE);
                        if (result == JOptionPane.YES_OPTION){
                            credits.setVisible(true);
                            try {
                                Thread.sleep(2500);
                            } catch(InterruptedException ex) {
                                Thread.currentThread().interrupt();
                            }
                                System.exit(0);
                        } else {
                            //do nothing 
                        }
                    }
                });

对于credits(只是class初始化框架):

public class CreditsFrame extends JFrame {

Positioner pos = new Positioner();

private JPanel contentPane;
ImageIcon frameIcon = new ImageIcon("files/images/frameicon.png");
/**
 * Launch the application.
 */
public static void main(String[] args) {
    EventQueue.invokeLater(new Runnable() {
        public void run() {
            try {
                CreditsFrame frame = new CreditsFrame();
                frame.setVisible(true);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    });
}

/**
 * Create the frame.
 */
public CreditsFrame() {
    setIconImage(frameIcon.getImage());
    setAlwaysOnTop(true);
    setResizable(false);
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    setBounds(pos.posX(pos.screenX, 441), pos.posY(pos.screenY, 210), 441, 210);
    contentPane = new JPanel();
    contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
    setContentPane(contentPane);

    JTextArea txtarea = new JTextArea();
    txtarea.setBounds(10, 11, 415, 125);
    txtarea.setEditable(false);
    txtarea.setBackground(Color.WHITE);
    txtarea.setWrapStyleWord(true);
    txtarea.setLineWrap(true);
    txtarea.append("created by & more here");
    contentPane.setLayout(null);
    contentPane.add(txtarea);

    JButton btnOk = new JButton("Ok");
    btnOk.setBounds(154, 147, 89, 23);
    btnOk.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent arg0) {
            dispose();
        }
    });
    contentPane.add(btnOk);
}

}

有什么快速修复的建议吗?谢谢。

Any suggestions for a quick fix?

是的,除非您想让整个 GUI 进入休眠状态,否则不要在 Swing 事件线程上调用 Thread.sleep(...)。而是使用 Swing Timer 来处理延迟。基本上,定时器的 ActionListener 将在经过毫秒延迟后被调用。

例如,

frame.addWindowListener(new java.awt.event.WindowAdapter() {
    @Override
    public void windowClosing(java.awt.event.WindowEvent windowEvent) {
        int result = JOptionPane.showConfirmDialog(null, "Sind Sie sicher?", "Schließen", JOptionPane.YES_NO_OPTION, JOptionPane.QUESTION_MESSAGE);
        if (result == JOptionPane.YES_OPTION){
            credits.setVisible(true);
            new Timer(2500, new ActionListener() {
                public void actionPerformed(ActionEvent evt) {
                    System.exit();
                }
            }).start();
        } else {
            //do nothing 
        }
    }
});

另外,看看The Use of Multiple JFrames, Good/Bad Practice?

以后会咬你的其他问题:

  • 您似乎在使用空布局和 setBounds。虽然 null 布局和 setBounds() 似乎对 Swing 新手来说是创建复杂 GUI 的最简单和最好的方法,但您创建的 Swing GUI 越多,使用它们时就会 运行 遇到更严重的困难。当 GUI 调整大小时,它们不会调整组件的大小,它们是皇家 来增强或维护的,当放置在滚动窗格中时它们完全失败,当在与原始平台不同的所有平台上查看时它们看起来很糟糕。
  • 永远不要设置 JTextAreas 边界,因为如果它被放置在 JScrollPane 中并且添加的文本超过可以显示的数量,这将导致它完全失败——滚动条似乎不起作用,因为您人为地限制了文本组件的大小。