Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/329.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 为什么BorderLayout不能正确显示多个图层?_Java_Swing_Layout_Layout Manager_Border Layout - Fatal编程技术网

Java 为什么BorderLayout不能正确显示多个图层?

Java 为什么BorderLayout不能正确显示多个图层?,java,swing,layout,layout-manager,border-layout,Java,Swing,Layout,Layout Manager,Border Layout,我正在为blackjack助手程序设置一个UI,虽然我知道这不是最漂亮的方式,但它是有意义的。 似乎是上层的分层工作不正常。有什么建议吗? 左边应该有四层,中间和右边在键盘和回车按钮之间应该有两层。图片附在下面 import java.util.*; import java.lang.*; import java.io.*; import java.awt.*; import java.awt.event.*; import javax.swing.*; import java.text.*;

我正在为blackjack助手程序设置一个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的5个区域中的每个区域添加一个组件

    JPanel rightSouthSide = new JPanel();
    rightSouthSide.add(resetButton, BorderLayout.WEST);
    rightSouthSide.add(enterButton, BorderLayout.EAST);
因此,是的,您可以创建一个JPanel并向该面板添加多个组件,然后将面板添加到BorderLayout的某个区域

    JPanel rightSouthSide = new JPanel();
    rightSouthSide.add(resetButton, BorderLayout.WEST);
    rightSouthSide.add(enterButton, BorderLayout.EAST);
但是,上述代码不正确。JPanel的默认布局管理器是FlowLayout。因此,指定BorderLayout约束没有任何作用,而且非常令人困惑

如果“层”是指行,则需要使用具有不同布局的面板。也许您可以使用BoxLayout来添加多行面板

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


阅读上的Swing教程,了解有关每个布局管理器如何工作的更多信息。

以最小尺寸提供GUI预期布局的ASCII艺术或简单绘图,如果可以调整大小,有更多的宽度和高度-显示如何使用额外的空间。左边应该有四层对不起,不清楚你想要什么布局。这个评论有我的解决方案。我的印象是JPanel也有一个默认的BorderLayout布局,就像JFrame一样。在JPanel声明中添加新的BorderLayout解决了我的问题。谢谢你的帮助!