java - 为什么 BorderLayout 不能正确显示多个图层?
问题描述
我正在为二十一点助手程序设置 UI,虽然我知道它不是最漂亮的做事方式,但它是有道理的。
似乎是上层的分层工作不正常。有什么建议么?
左侧应该有四层,中间和右侧应该在键盘和输入按钮之间有两层。图片附在下面。
import java.util.*;
import java.lang.*;
import java.io.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.text.*;
// main method, runs the program
class BlackjackHelper
{
public static void main(String\[\] args)
{
Frame21 game = new Frame21();
//pop window with rules of game
}
}
// JFrame Construction
class Frame21 extends JFrame
{
// create needed components of program
JLabel questionDisplay = new JLabel("What is your first card?");
JLabel actionDisplay = new JLabel("Enter your first card");
JLabel dealerCardText = new JLabel("Dealer's Card:");
JLabel dealerCardDisplay = new JLabel("N/A");
JLabel handOneText = new JLabel("Hand One:");
JLabel handOneDisplay = new JLabel("N/A");
JLabel handTwoText = new JLabel("Hand Two:");
JLabel handTwoDisplay = new JLabel("N/A");
JLabel statsText = new JLabel("Win %:");
JLabel statsDisplay = new JLabel("N/A");
JButton aceButton = new JButton("A");
JButton twoButton = new JButton("2");
JButton threeButton = new JButton("3");
JButton fourButton = new JButton("4");
JButton fiveButton = new JButton("5");
JButton sixButton = new JButton("6");
JButton sevenButton = new JButton("7");
JButton eightButton = new JButton("8");
JButton nineButton = new JButton("9");
JButton tenButton = new JButton("10");
JButton faceButton = new JButton("F");
JButton clearButton = new JButton("C");
JButton standButton = new JButton("Stand");
JButton hitButton = new JButton("Hit");
JButton doubleButton = new JButton("Double");
JButton splitButton = new JButton("Split");
JButton winButton = new JButton("Win");
JButton loseButton = new JButton("Lose");
JButton resetButton = new JButton("Reset All");
JButton enterButton = new JButton("Enter");
public Frame21()
{
// JFrame - the main area of the program
JFrame frame = new JFrame("Blackjack Helper");
// JPanel right - the rightside of the program
JPanel rightSide = new JPanel();
JPanel rightNorthSide = new JPanel();
rightNorthSide.setLayout(new GridLayout(3,4));
rightNorthSide.add(aceButton);
rightNorthSide.add(twoButton);
rightNorthSide.add(threeButton);
rightNorthSide.add(fourButton);
rightNorthSide.add(fiveButton);
rightNorthSide.add(sixButton);
rightNorthSide.add(sevenButton);
rightNorthSide.add(eightButton);
rightNorthSide.add(nineButton);
rightNorthSide.add(tenButton);
rightNorthSide.add(faceButton);
rightNorthSide.add(clearButton);
JPanel rightSouthSide = new JPanel();
rightSouthSide.add(resetButton, BorderLayout.WEST);
rightSouthSide.add(enterButton, BorderLayout.EAST);
rightSide.add(rightNorthSide, BorderLayout.NORTH);
rightSide.add(rightSouthSide, BorderLayout.SOUTH);
frame.add(rightSide, BorderLayout.EAST);
// JPanel Center - the center of the program
JPanel center = new JPanel();
JPanel centerNorth = new JPanel();
centerNorth.add(questionDisplay, BorderLayout.NORTH);
centerNorth.add(actionDisplay, BorderLayout.SOUTH);
JPanel centerSouth = new JPanel();
JPanel centerSouthNorth = new JPanel();
centerSouthNorth.add(dealerCardText, BorderLayout.WEST);
centerSouthNorth.add(dealerCardDisplay, BorderLayout.EAST);
JPanel centerSouthSouth = new JPanel();
JPanel centerSouthSouthWest = new JPanel();
centerSouthSouthWest.add(handOneText, BorderLayout.NORTH);
centerSouthSouthWest.add(handOneDisplay, BorderLayout.SOUTH);
JPanel centerSouthSouthEast = new JPanel();
centerSouthSouthEast.add(handTwoText, BorderLayout.NORTH);
centerSouthSouthEast.add(handTwoDisplay, BorderLayout.SOUTH);
centerSouthSouth.add(centerSouthSouthWest, BorderLayout.WEST);
centerSouthSouth.add(centerSouthSouthEast, BorderLayout.EAST);
centerSouth.add(centerSouthNorth, BorderLayout.NORTH);
centerSouth.add(centerSouthSouth, BorderLayout.SOUTH);
center.add(centerNorth, BorderLayout.NORTH);
center.add(centerSouth, BorderLayout.SOUTH);
frame.add(center, BorderLayout.CENTER);
// JPanel left - the center of the program
JPanel left = new JPanel();
JPanel leftNorth = new JPanel();
JPanel leftNorthNorth = new JPanel();
JPanel leftNorthSouth = new JPanel();
JPanel leftSouth = new JPanel();
JPanel leftSouthNorth = new JPanel();
JPanel leftSouthSouth = new JPanel();
leftNorthNorth.add(standButton, BorderLayout.WEST);
leftNorthNorth.add(hitButton, BorderLayout.EAST);
leftNorthSouth.add(doubleButton, BorderLayout.WEST);
leftNorthSouth.add(splitButton, BorderLayout.EAST);
leftNorth.add(leftNorthNorth, BorderLayout.NORTH);
leftNorth.add(leftNorthSouth, BorderLayout.SOUTH);
leftSouthNorth.add(statsText, BorderLayout.WEST);
leftSouthNorth.add(statsDisplay, BorderLayout.EAST);
leftSouthSouth.add(winButton, BorderLayout.WEST);
leftSouthSouth.add(loseButton, BorderLayout.EAST);
leftSouth.add(leftSouthNorth, BorderLayout.NORTH);
leftSouth.add(leftSouthSouth, BorderLayout.SOUTH);
left.add(leftNorth, BorderLayout.NORTH);
left.add(leftSouth, BorderLayout.SOUTH);
frame.add(left, BorderLayout.WEST);
frame.setSize(1600, 200);
frame.setVisible(true);
frame.setResizable(true);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
}
解决方案
似乎是上层的分层工作不正常。有什么建议么?左边应该有四层。
BorderLayout 不会“分层”。
BorderLayout 的 5 个区域中的每一个都只能添加一个组件。
所以,是的,您可以创建一个 JPanel 并将多个组件添加到该面板,然后将该面板添加到 BorderLayout 的一个区域。
JPanel rightSouthSide = new JPanel();
rightSouthSide.add(resetButton, BorderLayout.WEST);
rightSouthSide.add(enterButton, BorderLayout.EAST);
但是,上面的代码是不正确的。JPanel 的默认布局管理器是FlowLayout
. 所以指定 BorderLayout 约束什么都不做(而且非常混乱)。
如果“层”是指“行”,那么您需要使用具有不同布局管理的面板。也许您可以使用 aBoxLayout
添加面板行。
总的来说,您创建具有不同组件的面板的方法是有效的,问题是您还需要为每个子面板使用适当的布局管理器。
阅读有关布局管理器的 Swing 教程,了解有关每个布局管理器如何工作的更多信息。
推荐阅读
- deep-learning - 为什么 Detectron2 在迁移学习后没有检测到任何其他实例?
- c++ - 如何使用向量擦除结构中的相同元素
- java - 将 .class 作为参数传递给方法 (JAVA) (BUKKIT/SPIGOT) (CUSTOM EVENT SYSTEM)
- jqxgrid - jqwidgets jqxGrid 转到特定行的页面
- javascript - 如何根据给定值在Javascript中分配动态范围值?
- java - Session.getAsyncRemote().sendText("") 是否保证消息的顺序/连续性?
- graphql - 放大 GraphQL 将嵌套输入添加到连接
- json - 无法绑定来自可观察 IONIC 的值
- python - 如何从事件函数返回值并将其设置为 LineEdit?
- javascript - Next.js:错误:React.Children.only 预期接收单个 React 元素子项