尝试移至底部时,JPanel被压缩并消失

时间:2019-02-25 12:50:18

标签: java swing jpanel layout-manager null-layout-manager

我必须用Java做一个项目,并且认为GUI Text Adventure会很棒。我的问题是,当我创建一个JPanel并将其在屏幕上向下移动时,面板首先会更改其大小,然后在某一点完全消失。

GameScreen上应该有一个面板供您选择选项,但该面板拒绝下降得比屏幕大一半左右。

代码如下:

import java.awt.Color;
import java.awt.Container;
import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextArea;

public class Yeet {

    JFrame epicOfYeet;
    Container con;
    JPanel titleNamePanel, startButtonPanel, mainTextPanel, choiceButtonPanel;
    JLabel titleNameLabel;
    Font titleFont = new Font("Times New Roman", Font.PLAIN, 90);
    Font normalFont = new Font ("Times New Roman", Font.PLAIN, 55);
    JButton startButton;
    JButton choice1;
    JButton choice2;
    JButton choice3;
    JButton choice4;
    JTextArea mainTextArea;
    TitleScreenHandler tsHandler = new TitleScreenHandler();

    public static void main(String[] args) {

        new Yeet();
    }

    public Yeet() {

        epicOfYeet = new JFrame();
        epicOfYeet.setSize(1200, 1000);
        epicOfYeet.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        epicOfYeet.getContentPane().setBackground(Color.black);
        epicOfYeet.setLayout(null);
        con = epicOfYeet.getContentPane();

        titleNamePanel = new JPanel();
        titleNamePanel.setBounds(190, 100, 800, 230);
        titleNamePanel.setBackground(Color.black);
        titleNameLabel = new JLabel("EPIC OF YEET");
        titleNameLabel.setForeground(Color.red);
        titleNameLabel.setFont(titleFont);

        startButtonPanel = new JPanel();
        startButtonPanel.setBounds(400, 500, 400, 100);
        startButtonPanel.setBackground(Color.black);

        startButton = new JButton("START");
        startButton.setBackground(Color.black);
        startButton.setForeground(Color.white);
        startButton.setFont(normalFont);
        startButton.addActionListener(tsHandler);
        startButton.setFocusPainted(false);

        titleNamePanel.add(titleNameLabel);
        startButtonPanel.add(startButton);
        con.add(titleNamePanel);
        con.add(startButtonPanel);

        epicOfYeet.setVisible(true);
    }

    public void createGameScreen(){

        titleNamePanel.setVisible(false);
        startButtonPanel.setVisible(false);
        mainTextPanel = new JPanel();
        mainTextPanel.setBounds(100, 100, 1000, 400);
        mainTextPanel.setBackground(Color.green);
        con.add(mainTextPanel);

        mainTextArea = new JTextArea("You come to your senses again.\nThe dewy grass you're laying on is gleaming with moonlight.\nYour body aches as you get up and catch a \nglimpse of your surroundings.\n");
        mainTextArea.setBounds(100, 100, 1000, 250);
        mainTextArea.setBackground(Color.blue);
        mainTextArea.setForeground(Color.white);
        mainTextArea.setFont(normalFont);
        mainTextArea.setLineWrap(true);
        mainTextPanel.add(mainTextArea);

        choiceButtonPanel = new JPanel();
        choiceButtonPanel.setBounds(300, 500, 600, 550);
        choiceButtonPanel.setBackground(Color.red);
        con.add(choiceButtonPanel);
    }

    public class TitleScreenHandler implements ActionListener{

        @Override
        public void actionPerformed(ActionEvent event) {

            createGameScreen();
        }
    }
}

1 个答案:

答案 0 :(得分:1)

这是使用布局管理器的基本实现,避免了空布局管理器的不良做法。
这不是最佳选择,但应该可以帮助您入门:

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Container;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextArea;

public class Yeet {

    JFrame epicOfYeet;
    Container con;
    JPanel titleNamePanel, startButtonPanel, mainTextPanel, choiceButtonPanel;
    JLabel titleNameLabel;
    Font titleFont = new Font("Times New Roman", Font.PLAIN, 90);
    Font normalFont = new Font ("Times New Roman", Font.PLAIN, 55);
    JButton startButton, coice1, choice2, choice3, choice4;
    JTextArea mainTextArea;
    TitleScreenHandler tsHandler = new TitleScreenHandler();

    public static void main(String[] args) {

        SwingUtilities.invokeLater(()->new Yeet());
    }

    public Yeet() {

        epicOfYeet = new JFrame();
        //epicOfYeet.setSize(1200, 1000); // do not set size. let layout manager calcualte it
        epicOfYeet.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        epicOfYeet.getContentPane().setBackground(Color.black);
        //epicOfYeet.setLayout(null); //aviod null layouts Jframe uses BorderLayout by default
        con = epicOfYeet.getContentPane();

        titleNamePanel = new JPanel();
        //titleNamePanel.setBounds(190, 100, 800, 230);
        titleNamePanel.setPreferredSize(new Dimension(800, 230));//set preferred size to be used by layout manager
        titleNamePanel.setBackground(Color.black);
        titleNameLabel = new JLabel("EPIC OF YEET");
        titleNameLabel.setForeground(Color.red);
        titleNameLabel.setFont(titleFont);

        startButtonPanel = new JPanel();
        //startButtonPanel.setBounds(400, 500, 400, 100);
        startButtonPanel.setPreferredSize(new Dimension(400, 100));
        startButtonPanel.setBackground(Color.black);

        startButton = new JButton("START");
        startButton.setBackground(Color.black);
        startButton.setForeground(Color.white);
        startButton.setFont(normalFont);
        startButton.addActionListener(tsHandler);
        startButton.setFocusPainted(false);

        titleNamePanel.add(titleNameLabel);
        startButtonPanel.add(startButton);
        con.add(titleNamePanel, BorderLayout.PAGE_START);  //set to top
        con.add(startButtonPanel, BorderLayout.PAGE_END); //set to bottom
        epicOfYeet.pack();
        epicOfYeet.setVisible(true);
    }

    public void createGameScreen(){

        //titleNamePanel.setVisible(false);
        //startButtonPanel.setVisible(false);
        con.remove(titleNamePanel);
        con.remove(startButtonPanel);
        mainTextPanel = new JPanel();
        //mainTextPanel.setBounds(100, 100, 1000, 400);
        mainTextPanel.setBackground(Color.green);
        con.add(mainTextPanel); // added to center position of the BorderLayout (the default)

        mainTextArea = new JTextArea(10, 20); //size in rows, cols
        mainTextArea.setText("You come to your senses again.\nThe dewy grass you're laying on is gleaming with moonlight.\nYour body aches as you get up and catch a \nglimpse of your surroundings.\n");
        //mainTextArea.setBounds(100, 100, 1000, 250);
        mainTextArea.setBackground(Color.blue);
        mainTextArea.setForeground(Color.white);
        mainTextArea.setFont(normalFont);
        mainTextArea.setLineWrap(true);
        mainTextPanel.add(mainTextArea);

        choiceButtonPanel = new JPanel();
        //choiceButtonPanel.setBounds(300, 500, 600, 550);
        choiceButtonPanel.setPreferredSize(new Dimension(600, 550));
        choiceButtonPanel.setBackground(Color.red);
        con.add(choiceButtonPanel, BorderLayout.PAGE_END);//add to bottom
        epicOfYeet.pack();
    }

    public class TitleScreenHandler implements ActionListener{

        @Override
        public void actionPerformed(ActionEvent event) {
            createGameScreen();
        }
    }
}