为什么BorderLayout无法在多层中正确显示?

问题描述 投票:0回答:1

我正在为二十一点助手程序设置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);
    }
}

“”

java swing layout layout-manager border-layout
1个回答
0
投票

似乎是顶层的分层无法正常工作。有什么建议么?左边应该有四层。

BorderLayout不会“分层”。

只能将单个组件添加到BorderLayout的5个区域中的每个区域。

所以,是的,您可以创建一个JPanel并将多个组件添加到该面板,然后将该面板添加到BorderLayout的某个区域。

    JPanel rightSouthSide = new JPanel();
    rightSouthSide.add(resetButton, BorderLayout.WEST);
    rightSouthSide.add(enterButton, BorderLayout.EAST);

但是,以上代码不正确。 JPanel的默认布局管理器是FlowLayout。因此,指定BorderLayout约束不会执行任何操作(并且非常令人困惑)。

如果用“层”表示“行”,则需要使用具有不同布局管理的面板。也许您可以使用BoxLayout添加面板行。

总的来说,使用不同组件创建面板的方法是有效的,问题在于,您还需要为每个子面板使用适当的布局管理器。

阅读有关Layout Managers的Swing教程,以获取有关每个布局管理器如何工作的更多信息。

© www.soinside.com 2019 - 2024. All rights reserved.