【问题标题】:Java Swing panel layered on top with centered textJava Swing 面板位于顶部,文本居中
【发布时间】:2013-11-05 20:39:21
【问题描述】:

我正在制作一个简单的危险游戏:

使用 Java Swing。这显然是一个 JFrame,里面有一个 JPanel 和成行的按钮。

现在我需要添加一个带有居中和环绕文本的分层面板:

我可以稍后删除。我已经尝试过使用 JTextPane 和 JTextArea 和 JPanel,这些都不想显示。我使用 AWT 面板达到的最佳效果,它确实显示,但我无法在其中居中或换行。

以下是一些我要道歉的代码,我通常会尽量使其简短易读,但由于它不起作用,我不知道如何处理它以使 ti 看起来更好:

    JLabel questionLabel = new JLabel(questionList.get(randomNumber).getQuestion(), SwingConstants.CENTER);
    Font font = new Font("Arial", Font.PLAIN, 20);

    //------------------JTextPane--------------------

    JTextPane questionPane = new JTextPane();
    questionPane.setForeground(Color.WHITE);
    questionPane.setSize(gameWidth, gameHeight);
    questionPane.setText(questionList.get(randomNumber).getQuestion());
    questionPane.setFont(font);
    questionPane.setEditable(false);

    //------------------AWT panel--------------------
    Panel awtPanel = new Panel();
    awtPanel.setBackground(Color.blue);
    awtPanel.setSize(game.getWidth(),game.getHeight());
    Label labelQuestion = new Label("<html>" + questionList.get(randomNumber).getQuestion() + "</html>", Label.CENTER);
    labelQuestion.setFont(font);
    awtPanel.setForeground(Color.white);

    awtPanel.add(labelQuestion);

    //------------------JPanel-----------------------
    JPanel layeredPanel = new JPanel();
    layeredPanel.setBackground(Color.blue);
    layeredPanel.setSize(game.getWidth(),game.getHeight());
    JLabel jLabelQuestion = new JLabel("<html>" + questionList.get(randomNumber).getQuestion() + "</html>", SwingConstants.CENTER);
    jLabelQuestion.setFont(font);
    layeredPanel.setForeground(Color.WHITE);

    layeredPanel.add(jLabelQuestion, BorderLayout.CENTER);

    game.getLayeredPane().add(layeredPanel, JLayeredPane.DEFAULT_LAYER);

    try {
        Thread.sleep(3000);
    } catch (InterruptedException e1) {
        // TODO Auto-generated catch block
        e1.printStackTrace();
    }
    button.setEnabled(false);
    font = new Font("Arial", Font.PLAIN, 16);

    button.add(jLabelQuestion, BorderLayout.CENTER);
    button.setDisabledIcon(new ImageIcon(source.getScaledInstance(gameWidth/4, gameHeight/5, java.awt.Image.SCALE_SMOOTH)));

    questionList.remove(randomNumber);

    logger.info(questionList.size());

    game.getLayeredPane().remove(layeredPanel);

更新:我改用 SWT 而不是 Swing,我使用 StackLayout 和一些 Composites,并在我认为合适的时候在它们之间进行更改。

【问题讨论】:

  • 您不必求助于使用 AWT 面板。请解释一下 JTextPane 等有什么问题以及您的问题是什么。
  • 您希望分层面板显示在按钮之上吗?
  • 是的,就像图片中的“可视化”一样。 JTextPane 不显示自身。我对 Swing 还很陌生,因为我昨天开始使用它,所以可能存在某种初始化错误

标签: java swing


【解决方案1】:

您通常可以使用 JLabel 解决此类问题。

我建议将上述网格封装在另一个窗格的 BorderLayout.CENTER 中,也许是一个新的内容窗格。然后,将标题添加到 BorderLayout.NORTH。

作为一个更具体的例子,

private void createContent() {
    this.getContentPane().setLayout(new BorderLayout());

    //establish the panel currently set as center, here labeled "everythingElse"
    this.getContentPane().add(everythingElse, BorderLayout.CENTER);

    //Create a JLabel with your caption
    JLabel jlbl = new JLabel("Question");
    //format that caption, most details being rather obvious, but most importantly:
    jlbl.setHorizontalAlignment(SwingConstants.CENTER); //keeps text centered
    this.getContentPane().add(jlbl, BorderLayout.NORTH); //add it to the top of the panel

    //...other cleanup operations...
}

网格窗格的问题在于它们对其中可见组件数量的容忍度有限。如果你超载一个,它不会显示。对于 BorderLayout 窗格,您可以轻松地将新项目换入和换出。

为了提高效率,我可能会建议将此 JLabel 编译为代码中其他地方的最终版本,并在需要时保留它。这样,您还可以避免重复创建标签对象的开销。

最后,尽可能避免使用 AWT。它已被弃用超过十年,如果您使用它,您将遇到许多涉及重量级和轻量级组件不兼容的关键问题。如果您打算使用另一个窗口工具包,请考虑使用 JFXPane 实现新标准 JavaFX——它也更能容忍 HTML 语法。

【讨论】:

    猜你喜欢
    • 2023-03-09
    • 2012-06-03
    • 1970-01-01
    • 2011-01-01
    • 1970-01-01
    • 2014-05-01
    • 1970-01-01
    • 2014-09-01
    • 1970-01-01
    相关资源
    最近更新 更多