当试图移动到底部时,JPanel会被压缩并消失

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

我必须用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();
        }
    }
}
java swing jpanel layout-manager null-layout-manager
1个回答
1
投票

这是使用布局管理器的基本实现,避免了null布局管理器的不良做法。 它不是最佳的,但应该让你开始:

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();
        }
    }
}
© www.soinside.com 2019 - 2024. All rights reserved.