gpt4 book ai didi

Java Swing 面板位于顶部,文本居中

转载 作者:行者123 更新时间:2023-12-02 06:34:27 24 4
gpt4 key购买 nike

我正在制作一个简单的 Jeopardy 式游戏:

enter image description here

使用 Java Swing。显然,它是一个 JFrame,其中有一个 JPanel,并且有成行的按钮。

现在我需要添加一个分层面板,其中包含居中且换行的文本:

enter image description here

我可以稍后删除。我已经尝试使用 JTextPane 和 JTextArea 和 JPanel,但这些都不想显示。我使用 AWT Panel 实现的最佳效果是,它确实可以显示,但我无法将文本居中或换行。

这是一些我表示歉意的代码,我通常会尝试使其简短易读,但由于它不起作用,我不知道如何处理它才能使 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 中的一些组合,然后根据需要在它们之间进行更改。

最佳答案

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

我建议将上述网格封装在另一个 Pane (可能是新的内容 Pane )的 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...
}

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

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

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

关于Java Swing 面板位于顶部,文本居中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19798507/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com