Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/352.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 仅显示最后添加的组件的BoxLayout_Java_Swing_Layout Manager_Boxlayout - Fatal编程技术网

Java 仅显示最后添加的组件的BoxLayout

Java 仅显示最后添加的组件的BoxLayout,java,swing,layout-manager,boxlayout,Java,Swing,Layout Manager,Boxlayout,看了一些以前关于我的主题的帖子,但也没用 尝试使用对齐组件,但无法使其工作。我已经修补了一段时间了,结果不一样,但我想不出来。我使用默认设置没有任何问题,我正在努力学习和扩展我的知识,BoxLayout将更适合我的程序。如果用户调整应用程序窗口的大小,我希望所有内容都保持一致。我用这种方式调整了所有的尺寸,只是为了让它工作,但失败了 package GUI; import javax.swing.*; import java.awt.*; /** * Created by Thunderf

看了一些以前关于我的主题的帖子,但也没用

尝试使用对齐组件,但无法使其工作。我已经修补了一段时间了,结果不一样,但我想不出来。我使用默认设置没有任何问题,我正在努力学习和扩展我的知识,BoxLayout将更适合我的程序。如果用户调整应用程序窗口的大小,我希望所有内容都保持一致。我用这种方式调整了所有的尺寸,只是为了让它工作,但失败了

package GUI;
import javax.swing.*;
import java.awt.*;

 /**
 * Created by Thunderfoot on 7/31/2016. Keep Growing!
 * Graphical User Interface
 * Needs 3 JPanels(Text area + scroll pane)(2 Buttons) (1 Button), a    JTextArea, JScrollPane, and 3 JButtons
 */
public class PrimaryFrame extends JFrame {

    //Class variables
    private static JPanel panel1, panel2, panel3;
    public static JTextArea output;

    //Constructor
    public PrimaryFrame() {
        //Frame component attributes
        final Dimension FRAME_SIZE = new Dimension(400, 400);
        final Dimension PANEL1_SIZE = new Dimension(400, 250);
        final Dimension PANEL2_SIZE = new Dimension(400, 40);
        final Dimension PANEL3_SIZE = new Dimension(400, 40);

        //JFrame is PrimaryFrame
        setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
        setPreferredSize(FRAME_SIZE);
        setMaximumSize(FRAME_SIZE);
        setTitle("Fighting Game");

        //JPanel for Text
        panel1 = new JPanel();
        panel1.setLayout(new BoxLayout(panel1, BoxLayout.PAGE_AXIS));
        panel1.setMinimumSize(PANEL1_SIZE);
        panel1.setPreferredSize(PANEL1_SIZE);
        panel1.setMaximumSize(PANEL1_SIZE);
        panel1.setBackground(Color.BLACK);

        //JPanel for Attack and Kick Buttons
        panel2 = new JPanel();
        panel2.setLayout(new BoxLayout(panel2, BoxLayout.LINE_AXIS));
        panel2.setMinimumSize(PANEL2_SIZE);
        panel2.setPreferredSize(PANEL2_SIZE);
        panel2.setMaximumSize(PANEL2_SIZE);
        panel2.setBackground(Color.BLUE);

        //JPanel for Power Attack Button
        panel3 = new JPanel();
        panel3.setLayout(new BoxLayout(panel3, BoxLayout.PAGE_AXIS));
        panel3.setMinimumSize(PANEL3_SIZE);
        panel3.setPreferredSize(PANEL3_SIZE);
        panel3.setMaximumSize(PANEL3_SIZE);
        panel3.setBackground(Color.ORANGE);
        panel3.add(Box.createHorizontalGlue());
        panel3.add(Box.createVerticalGlue());

        //JTextArea & JScrollPane
        output = new JTextArea();
        output.setEditable(false);
        JScrollPane outputScroller = new JScrollPane(output, ScrollPaneConstants.VERTICAL_SCROLLBAR_AS_NEEDED, ScrollPaneConstants.HORIZONTAL_SCROLLBAR_NEVER);
        outputScroller.setMaximumSize(new Dimension(375, 250));
        outputScroller.setBorder(BorderFactory.createLineBorder(Color.RED));
        panel1.add(outputScroller);
        panel1.add(Box.createHorizontalGlue());
        panel1.add(Box.createVerticalGlue());

        //Attack Button
        JButton attackButton = new JButton(" ATTACK ");
        attackButton.setMaximumSize(new Dimension(75, 30));
        attackButton.setBorderPainted(true);
        //Kick Button
        JButton kickButton = new JButton(" KICK ");
        kickButton.setMaximumSize(new Dimension(75, 30));
        kickButton.setBorderPainted(true);
        //Add components
        panel2.add(attackButton);
        panel2.add(Box.createHorizontalGlue());
        panel2.add(Box.createVerticalGlue());
        panel2.add(kickButton);
        panel2.add(Box.createHorizontalGlue());
        panel2.add(Box.createVerticalGlue());

        //Power Attack Button
        JButton powAttButton = new JButton(" POWER ATTACK ");
        powAttButton.setMaximumSize(new Dimension(150, 30));
        powAttButton.setBorderPainted(true);
        panel3.add(powAttButton);
        panel3.add(Box.createHorizontalGlue());

    }

    public void buildGUI() {
        //Add components and build GUI Frame
        this.add(panel3);
        this.add(panel2);
        this.add(panel1);

        //Set attributes
        //Pack components together inside of frame
        pack();
        //Center of screen
        setLocationRelativeTo(null);
        //Make frame visible
        setVisible(true);
    }

}

您必须设置主框架的布局。 我建议您在
buildGUI()
方法中添加一行:

public void buildGUI() {

        //defines the Layout for the main Frame
        this.setLayout(new GridLayout(3,1)) //its up to you wich Layout you use

        //Add components and build GUI Frame
        this.add(panel3);
        this.add(panel2);
        this.add(panel1);

        //Set attributes
        //Pack components together inside of frame
        pack();
        //Center of screen
        setLocationRelativeTo(null);
        //Make frame visible
        setVisible(true);
    }

请注意,
GridLayout(3,1)
将生成一个三行一列的布局

您必须设置PrimaryFrame的布局。 我建议您在
buildGUI()
方法中添加一行:

public void buildGUI() {

        //defines the Layout for the main Frame
        this.setLayout(new GridLayout(3,1)) //its up to you wich Layout you use

        //Add components and build GUI Frame
        this.add(panel3);
        this.add(panel2);
        this.add(panel1);

        //Set attributes
        //Pack components together inside of frame
        pack();
        //Center of screen
        setLocationRelativeTo(null);
        //Make frame visible
        setVisible(true);
    }

请注意,
GridLayout(3,1)
将生成一个三行一列的布局

您忘了设置JFrame的布局。您忘了设置JFrame的布局。谢谢,我猜我的眼睛和大脑都累了XD谢谢,我猜我的眼睛和大脑都累了XD