Java 如何使用Jpanel创建开始菜单屏幕

Java 如何使用Jpanel创建开始菜单屏幕,java,swing,screen,panel,frame,Java,Swing,Screen,Panel,Frame,这将创建一个包含3行4列的jframe。我试图实现一个记忆游戏,它匹配的字母。我想这样做,当程序运行时,它将创建一个退出按钮和一个开始按钮,这将进入游戏。我对Jpanel和Jframe相当陌生。另一个问题是,您将如何更改字体的大小 import java.awt.BorderLayout; import java.awt.Dimension; import java.awt.GridLayout; import java.awt.event.ActionEvent; import java

这将创建一个包含3行4列的jframe。我试图实现一个记忆游戏,它匹配的字母。我想这样做,当程序运行时,它将创建一个退出按钮和一个开始按钮,这将进入游戏。我对Jpanel和Jframe相当陌生。另一个问题是,您将如何更改字体的大小

   import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import javax.swing.border.Border;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import java.awt.Color;
import javax.swing.BorderFactory;


public class matchinggame implements ActionListener 
{
    JPanel jp = new JPanel(); //lets us use the variables jp
    JFrame jf = new JFrame(); //lets us use the variables jf

    String[] matchList = { "dank", "dank", "memes", "memes", "360", "360", "headshot", "headshot", "doge", "doge",
            "mlg", "mlg",};  
    String[] shuffledList;

    JButton[][] buttons;

    int i = 0;
    int cardOne;
    int secIndex;

    public static void main(String[] args) 
    {
        //creates app and Gui

        javax.swing.SwingUtilities.invokeLater(new Runnable() //launches a thread that will run a class later
        {
            public void run() 
            {
                matchinggame app = new matchinggame();
                app.makeGUI();
            }
        });

    }

    public void dealCards(JPanel panel) 
    {
        buttons = new JButton[3][4]; // array of buttons used to represent cards
        shuffleCards();
        for (int i = 0; i < 3 * 4; i++)  // creates 3 rows with 4 columns     
        {                       
            if (i % 4 == 0)
                buttons[i / 4] = new JButton[4];
            buttons[i / 4][i % 4] = new JButton("choose me"); // shows cards faced down
            buttons[i / 4][i % 4].addActionListener(this);
            panel.add(buttons[i / 4][i % 4]);
        }
    }

    public void shuffleCards() 
    {
        List<String> list = Arrays.asList(matchList);
        Collections.shuffle(list);
        shuffledList = list.toArray(new String[list.size()]);
    }

    public void updateMatchList(String a, String b, boolean add) 
    {
        String[] courseList;
        int oldLen = matchList.length;

        if (add) // add the new item to the list
        { 
            courseList = new String[oldLen + 2];
            courseList[0] = new String(a); // new first course
            courseList[1] = new String(b); // new first course num
            for (int item = 2; item <= oldLen; item += 2) 
            {
                courseList[item] = matchList[item - 2];
                courseList[item + 1] = matchList[item - 1];
            }
            matchList = courseList;// delete matching item
        } 
            else 
        { 
            courseList = new String[oldLen - 2];
            int matchItem = 0;
            for (int item = 0; item <= oldLen; item += 2) {
                if (a != matchList[item]) { // no match so OK to copy over
                    courseList[item] = matchList[matchItem];
                    courseList[item + 1] = matchList[matchItem + 1];
                    matchItem += 2;
                }
            }
            matchList = courseList;
        }

    }

    /**
     * Creates the JFrame and its components.
     */
    public void makeGUI() 
    {
        JFrame frame = new JFrame("matchinggame"); 
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        jp = new JPanel(new GridLayout(3, 4));
        jp.setPreferredSize(new Dimension(500, 300));
        dealCards(jp);
        frame.getContentPane().setLayout(new BorderLayout());
        frame.getContentPane().add(jp, BorderLayout.CENTER);

        // Display the window.
        frame.setSize(500, 500);   //creates frame
        frame.setVisible(true);    //shows frame


        jp.setBorder(BorderFactory.createLineBorder(Color.ORANGE,16));



    }


    public void actionPerformed(ActionEvent e) 
  {
        int r, c;

        if (i < 2) // find the card clicked on and flip it over
        { 

            for (r = 0; r < 3; r++) 
            {
                for (c = 0; c < 4; c++) 
                {
                    // if the card is not face down (showing "choose me") don't
                    // flip it
                    if ((e.getSource() == buttons[r][c])
                            && buttons[r][c].getText().equals("choose me")) 
                    {
                        // flip the card face-up to show text from matchList
                        buttons[r][c].setText(shuffledList[(r * 4 + c)]);
                        i++; // increment number of cards flipped
                        if (i == 1)
                            cardOne = (r * 4 + c); // save which pattern was
                                                    // shown first
                        else
                            secIndex = (r * 4 + c); // save the pattern
                                                    // shown second
                        return;
                    }
                }
            }
        } else { // 2 cards already flipped, put all cards face down

            for (r = 0; r < 3; r++) 
            {
                for (c = 0; c < 4; c++) 
                {
                    // first and second cards flipped
                    if (shuffledList[cardOne].equals(shuffledList[secIndex])) 
                    {
                        if (!buttons[r][c].getText().equals("choose me")) //if cards dont match set this card
                            {
                        buttons[r][c].setText("dankified"); //when matched changes card
                        buttons[r][c].setBackground(Color.GREEN);  //when matched changes color of card
                        buttons[r][c].setEnabled(false);    //matched cards will not be able to clicked 
                            }

                    } else if ((!buttons[r][c].getText().equals("dankified"))
                            && (!buttons[r][c].getText().equals("choose me"))) 
                    {
                        // if 2 face up cards didn't match, flip face down again
                        buttons[r][c].setText("choose me");
                    }
                }
                i = 0; // new turn, no cards flipped face up
            }
        }
    }
}

您只需添加一个确认对话框,该对话框将在创建框架之前进行检查

public void makeGUI() {

javax.swing.JOptionPane jOptionPane = new JOptionPane();
if(JOptionPane.showConfirmDialog(null, "start playing ?", "card memory game", 2)==0) {
JFrame frame = new JFrame("matchinggame");  

//rest of your code  ..

}
要更改按钮的字体,请在创建按钮后向dealCards方法添加一些代码

buttons[i / 4][i % 4].setFont(new Font("customFont", FontConstants.ALIGN_CENTER, 24));

您将如何更改字体的大小。你必须更准确地知道你想要什么。当您第一次启动程序时,您希望“确定”和“取消”选项出现在以下位置??