首页 > 解决方案 > 我怎样才能让 Gui 像这张照片一样

问题描述

package matchgame;
import java.awt.BorderLayout;
import javax.swing.*;
import javax.swing.border.Border;
public class Start_Manu extends JFrame {
    Start_Manu()
    {
        JLabel welcome=new JLabel("Welcome to Match Card Game ☺");
        JButton start=new JButton("Start");
        JPanel manustart=new JPanel(new BorderLayout());
        manustart.add(welcome,BorderLayout.NORTH);
        manustart.add(start,BorderLayout.CENTER);
        add(manustart);
        //set Frame
        setSize(800, 600);
        setTitle("Match Card Game");
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setLocationRelativeTo(null);
        setVisible(true);
    }

    public static void main(String[] args) {
        Start_Manu a=new Start_Manu();
    }

}

我想设置喜欢这张照片。 在此处输入图像描述

当点击开始按钮进入游戏状态

但是当我写代码时。

在此处输入图像描述

你能告诉我如何编写代码,就像我的第一张照片一样吗?

标签: java

解决方案


这只是一个粗略的设计。您想嵌套不同的布局方法。这应该是您设计的准确框架,您可以根据自己的喜好调整一些数字(如空格等)。

        //Main Panel to contain everything
        JPanel main = new JPanel();
        main.setLayout(new BoxLayout(main,BoxLayout.Y_AXIS));

        JLabel welcome=new JLabel("Welcome to Match Card Game ☺");
        //Setting prefered Font-Size
        welcome.setFont(new Font(welcome.getName(), Font.PLAIN, 24));


        JButton start=new JButton("Start Game");

        //Change button size and apparence
        start.setPreferredSize(new Dimension(150,50));
        start.setContentAreaFilled(false);


        //Add two sub-panels for advanced positioning
        JPanel welcome_p=new JPanel(new FlowLayout(1,0,150));
        JPanel button_p=new JPanel();

        //Set welcomepanel size to lower space to Button
        welcome_p.setPreferredSize(new Dimension(300,150));
        welcome_p.add(welcome);

        button_p.add(start);
        main.add(welcome_p);

        main.add(button_p);

        add(main);
        setSize(800, 600);
        setTitle("Match Card Game");
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setLocationRelativeTo(null);
        setVisible(true);

推荐阅读