- Java 双重比较
- java - 比较器与 Apache BeanComparator
- Objective-C 完成 block 导致额外的方法调用?
- database - RESTful URI 是否应该公开数据库主键?
我正在使用 Swing 和 AWT(供听众使用)制作一个小程序。我在获取 JPanel(名为 Chess 的类)的大小方面遇到了问题。我的布局:
public class Main extends JFrame implements MouseListener, ActionListener{
Chess chessPanel = new Chess ();
JButton newGameButton = new JButton ("New Game");
JButton loadGameButton = new JButton ("Load Game");
JButton saveGameButton = new JButton ("Save Game");
JButton exitButton = new JButton ("Exit");
public static void main (String [] args) {
new Main();
}
Main () {
super ("Chess");
Dimension dim = Toolkit.getDefaultToolkit().getScreenSize();
setSize(dim);
setLocation(0,0);
setUndecorated(true);
chessPanel.addMouseListener(this);
add(chessPanel, BorderLayout.CENTER);
JPanel buttonPanel = new JPanel();
buttonPanel.setLayout(new FlowLayout());
newGameButton.addActionListener(this);
loadGameButton.addActionListener(this);
saveGameButton.addActionListener(this);
exitButton.addActionListener(this);
buttonPanel.add(newGameButton);
buttonPanel.add(loadGameButton);
buttonPanel.add(saveGameButton);
buttonPanel.add(exitButton);
add(buttonPanel, BorderLayout.SOUTH);
setVisible(true);
}
// ... Code ...
}
正如您从代码中看到的那样,我在 CENTER 中有一个 JPanel,它几乎占据了整个屏幕。在底部我有另一个 JPanel (SOUTH),它有一排按钮。
我需要的是 CENTER 中的 JPanel 所占的大小。当我调用从 JPanel 继承的 getWidth()、getHeight() 或 getBounds() 方法时,由于 BorderLayout,它们都返回 0。知道如何获得真正的值(value)吗?
PS:屏幕总是占据整个屏幕,并且永远不会调整大小,如果有帮助的话。
最佳答案
您可能在渲染 JPanel 之前调用 getWidth,因此它将为 0。解决方案是在 渲染之后获取尺寸,例如在 pack() 或 setVisible(true ) 已在包含此 JPanel 的根容器上调用。
此外,我建议不要在任何事情上调用 setSize(),因为大多数标准布局管理器会观察组件的首选大小,而不是大小,并且当您调用 pack() 告诉布局管理器做他们的事情时,设置大小通常被忽略。如果位于中心的 JPanel 需要特定大小,您可能希望通过重写其 setPreferredSize 方法来设置自己的大小。然后让 JFrame 及其持有的容器在您调用 pack 时根据它们的布局管理器设置适合大小。
例如,
import java.awt.*;
import javax.swing.*;
public class Main extends JFrame {
Chess chessPanel = new Chess();
JButton newGameButton = new JButton("New Game");
JButton loadGameButton = new JButton("Load Game");
JButton saveGameButton = new JButton("Save Game");
JButton exitButton = new JButton("Exit");
public static void main(String[] args) {
new Main();
}
Main() {
super("Chess");
add(chessPanel, BorderLayout.CENTER);
JPanel buttonPanel = new JPanel();
buttonPanel.setLayout(new FlowLayout());
buttonPanel.add(newGameButton);
buttonPanel.add(loadGameButton);
buttonPanel.add(saveGameButton);
buttonPanel.add(exitButton);
System.out.printf("chessPanel Size before rendering: %s%n", chessPanel.getSize());
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
add(buttonPanel, BorderLayout.SOUTH);
pack();
System.out.printf("chessPanel Size after rendering: %s%n", chessPanel.getSize());
setLocationRelativeTo(null);
setVisible(true);
}
// ... Code ...
}
@SuppressWarnings("serial")
class Chess extends JPanel {
private static final int CHESS_WIDTH = 600;
private static final int CHESS_HEIGHT = CHESS_WIDTH;
private static final int MAX_ROW = 8;
private static final int MAX_COL = 8;
private static final Color LIGHT_COLOR = new Color(240, 190, 40);
private static final Color DARK_COLOR = new Color(180, 50, 0);
@Override
public Dimension getPreferredSize() {
return new Dimension(CHESS_WIDTH, CHESS_HEIGHT);
}
@Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
int panelWidth = getWidth();
int panelHeight = getHeight();
int sqrWidth = panelWidth / MAX_ROW;
int sqrHeight = panelHeight / MAX_COL;
for (int row = 0; row < MAX_ROW; row++) {
for (int col = 0; col < MAX_COL; col++) {
Color c = (row % 2 == col % 2) ? LIGHT_COLOR : DARK_COLOR;
g.setColor(c);
int x = (row * panelWidth) / MAX_ROW;
int y = (col * panelHeight) / MAX_COL;
g.fillRect(x, y, sqrWidth, sqrHeight);
}
}
}
}
关于Java,BorderLayout.CENTER,获取JPanel的宽高,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6403738/
来自 BorderLayout javadoc: A border layout lays out a container, arranging and resizing its components
我试图在 BorderLayout 的中心添加一个 GridBagLayout,但是当我将它添加到中心时,它没有出现。 尽管如此,如果我将它放在 BorderLayout 中的另一个位置,它会正确显示
我想制作一个带有 BorderLayout 的程序,其中中心和东部在图形上是分开的。我尝试了以下操作,但不幸的是分隔符位于 JFrame 的左侧,而不是在 centerPanel 和 rightPan
我试图将两个 JButton 彼此相邻放置在 JFrame 的中心,当 JFrame 已重新调整大小。 为此,我将两个按钮放在一个带有 FlowLayout 的面板中,然后将其放置在一个带有中心 Bo
我正在尝试设置披萨订单表单的框架,但在使用 orderPanel 和 ButtonPanel 时遇到问题。我可以让其中之一出现,但不能同时出现?在我发布的当前代码中,显示了按钮,但未显示文本框/ord
我正在从事一个计算机科学项目,我遇到了一些边框布局问题:这是我的配置(简化) public class MainPanel extends JPanel{ JSplitPane Pcenter
我决定编写一个小型 Java 程序来试验 BorderLayout,因为我正在开发一个 Java 游戏,我需要同时将 2 个对象放置在一个 JFrame 中,我问的每个人都说我需要 BorderLay
我一直在尝试在这个的死点制作一个 2x2 网格,但我做不到。网格要么会覆盖其余部分,要么会适合,但我只能看到最后一个按钮。 import javax.swing.*; import java.awt.
我正在编写一个程序,该程序旨在使用在窗口的西侧和东侧有两个按钮的 BorderLayout。不知怎的,中心有一个很大的差距。有什么方法可以消除这个间隙并使两个按钮彼此相切吗?下面我附上了我的代码。如有
我正在制作一个标题栏,并将标题放在左侧,将两个按钮放在右侧。但如果我对两个按钮都使用 BorderLayout.EAST,则只会显示最后一个按钮。我可以让两者都可见吗?我尝试使用 JPanel,但它会
我分别在 MouseEnter 和 MouseExit 上添加和删除 JButton。这工作正常,但是当按钮添加到面板时,它出现在右上角,而不是指定的 BorderLayout.SOUTH 位置。 框
我正在尝试创建一个可以在其中绘画的 Canvas ,以及一个将 Canvas 重置为空状态的按钮。然而,使用 BorderLayout 我的按钮会被复制,第二个按钮是第一个按钮的图像副本,但看起来仍然
我很难弄清楚为什么这个边框布局仍然像流布局一样...... 我先尝试了这个: JPanel canvas = new JPanel(new BorderLayout()); add(canvas);
我是 Java GUI 的新手,目前正在致力于创建微波 GUI。我正在尝试将面板放置在面板中,但它并没有真正按照我认为的那样工作。我正在使用 BorderLayout,基本上将一个面板放入 Borde
我尝试做以下事情: 使用 BorderLayout 将组件添加到 JFrame 向 JFrame 添加另一个组件 我希望新组件能够“覆盖”旧组件,因为我使用的是 BorderLayout。如果我在调用
我有如下代码: public static void main(String[] args) { JFrame f = new BorderTest(); JPanel p = new
据我了解,将组件添加到 BorderLayout 时,它们会占据整个隔间的大小。将 panel1 添加到 contentPane 的北部时,似乎是下面的情况。但是,当我随后将 panel2 放在 pa
我在使用 BorderLayout 时遇到问题,但首先,这是我的 GUI 设置: 如您所见,我的 JFrame 中有 3 个不同的组件。添加 JMenu 和 JList 效果很好。但是我的 JPane
我的问题是: 如何将 JPanel 添加到东南部的 BorderLayout? 这是我的按钮组: buttonsPanel = new JPanel(); buttonsPanel.setLayout
我想要一个 JFrame,在左侧和右侧有一个黑色边框,宽度为 withfOfJFrame/10。 现在,我的尝试看起来像这样: JFrame f = new JFrame(); f.setSize(8
我是一名优秀的程序员,十分优秀!