Java如何将两个JPanel放到JFrame上

时间:2023-01-28 00:08:22

I am making a simple minesweeper game(no menus at top and other stuff) All i need to have is a 9x9 board and when each tile is clicked it either is blank if no near bombs, or says how many bombs are near it or says B for bomb.

我正在制作一个简单的扫雷游戏(顶部没有菜单和其他东西)所有我需要的是一个9x9板,当每个瓷砖被点击时,如果没有近炸弹,它或者是空白的,或者说它附近有多少炸弹或说B为炸弹。

I have all above correctly done and it works perfectly, my problem is that I need to put in a timer that starts from 0 and continually counts up from 0 while I play the game until i click on a bomb where it stops. Then I will have below my board a place where the user can put in their name and beneath it there is a button called submit which will std out the person's name and time. (Note the player can do this at anytime)

我已经正确地完成了所有这些并且它完美地工作,我的问题是我需要放入一个从0开始的计时器并且在我玩游戏时不断从0开始计数,直到我点击它停止的炸弹。然后我会在我的董事会下面有一个用户可以输入他们名字的地方,在它下面有一个叫做提交的按钮,它会显示该人的姓名和时间。 (注意玩家可以随时执行此操作)

My problem is that whenever I try to add a JPanel or JLabel or anything really to my frame it won't be put on it and that my 9x9 grid keeps scaling to size of my window. How should I add a timer display and a submitting name part so i can see it on the window!

我的问题是,每当我尝试将JPanel或JLabel或其他任何内容添加到我的框架中时,它都不会被放在它上面,并且我的9x9网格会不断缩放到我的窗口大小。我应该如何添加计时器显示和提交名称部分,以便我可以在窗口中看到它!

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;




public class MS extends JFrame implements ActionListener{

    static JFrame bframe;
    static JPanel p;
    static double[] bomb;


    public MS() {   
        p = new JPanel(new GridLayout(9,10));  

        JButton btn;
        bomb = new double[81];
        for(int i=0; i<81; i++)
            bomb[i] = Math.random();

        for (int i=0; i< 81; i++)  {
            btn = new JButton();
            btn.addActionListener(this);
            btn.setActionCommand(Integer.toString(i));
            p.add(btn);
        }
    }

    public static void main(String[] args) {
        bframe=new MS();    //CREATE me and
        bframe.add(timelabel);
        bframe.add(p);      //add the JPanel

        bframe.setLocation(32,32);              //where my upper left hand corner goes
                //bframe.setSize(64*width, 64*height);
                bframe.setSize(500,500);
        bframe.setVisible(true);                //I start out invisible
        bframe.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);  //need this for the window manager
    }


    public void actionPerformed(ActionEvent e) {

        JButton jb = (JButton) e.getSource();
        int temp = Integer.parseInt(e.getActionCommand());

        System.out.printf("%s\n", e.getActionCommand());        

        int count = 0;
        String total = "";

        if(bomb[temp] > 0.15) {
            //for top line
            if((temp!=0) && (temp!=1) && (temp!=2) && (temp!=3) && (temp!=4) &&(temp!=5) && (temp!=6) && (temp!=7) && (temp!=8)) {
                if((temp!=0) && (temp!=9) && (temp!=18) && (temp!=27) && (temp!=36) && (temp!=45) && (temp!=54) && (temp!=63) && (temp!=72))
                    if(bomb[temp-10] <= 0.15) 
                        count++;
                if(bomb[temp-9] <= 0.15)
                    count++;
                if((temp!=8) && (temp!=17) && (temp!=26) && (temp!=35) && (temp!=44) && (temp!=53) && (temp!=62) && (temp!=71) && (temp!=80))
                    if(bomb[temp-8] <= 0.15)
                        count++;
            }
            if((temp!=0) && (temp!=9) && (temp!=18) && (temp!=27) && (temp!=36) && (temp!=45) && (temp!=54) && (temp!=63) && (temp!=72))
                if(bomb[temp-1] <= 0.15)
                    count++;    
            if((temp!=8) && (temp!=17) && (temp!=26) && (temp!=35) && (temp!=44) && (temp!=53) && (temp!=62) && (temp!=71) && (temp!=80))
                if(bomb[temp+1] <= 0.15)
                    count++;

            if((temp!=72) && (temp!=73) && (temp!=74) && (temp!=75) && (temp!=76) && (temp!=77) && (temp!=78) && (temp!=79) && (temp!=80)) {

                if((temp!=0) && (temp!=9) && (temp!=18) && (temp!=27) && (temp!=36) && (temp!=45) && (temp!=54) && (temp!=63) && (temp!=72))
                    if(bomb[temp+8] <= 0.15)
                        count++;
                if(bomb[temp+9] <= 0.15)
                    count++;
                if((temp!=8) && (temp!=17) && (temp!=26) && (temp!=35) && (temp!=44) && (temp!=53) && (temp!=62) && (temp!=71) && (temp!=80))
                    if(bomb[temp+10] <= 0.15)
                        count++;
            }
            if(count==0)
                jb.setText("");

            else {
                total=Integer.toString(count);
                jb.setText(total);              
                if(count==1) 
                    jb.setForeground(Color.blue);
                if(count==2)
                    jb.setForeground(Color.green);
                if(count==3)
                    jb.setForeground(Color.red);
            }
        }
        else
            jb.setText("B");
    }
}              

For clarity: What i wish to know is why is my 9x9 grid box's scaling with the window, how I can fix it, and how can i add it another 1 or 2 jpanels so I can complete the other parts of my project.

为清楚起见:我想知道的是为什么我的9x9网格框与窗口缩放,如何修复它,以及如何添加另外1或2个jpanels以便我可以完成项目的其他部分。

1 个解决方案

#1


0  

Try using this (untested):

尝试使用此(未经测试):

public Test() {
    setContentPane(new JPanel(new BorderLayout()));
    JPanel p = new JPanel(new GridLayout(9, 9));
    JButton btn;
    bomb = new double[81];
    for (int i = 0; i < 81; i++)
        bomb[i] = Math.random();
    for (int i = 0; i < 81; i++) {
        btn = new JButton();
        btn.addActionListener(this);
        btn.setActionCommand(Integer.toString(i));
        p.add(btn);
    }
    getContentPane().add(p, BorderLayout.CENTER); // add the JPanel
    JPanel timerPanel = new JPanel();
    timerPanel.add(timelabel);
    getContentPane().add(timerPanel, BorderLayout.EAST);
    setLocation(32, 32); // where my upper left hand corner goes
    // bframe.setSize(64*width, 64*height);
    setSize(500, 500);
    setVisible(true); // I start out invisible
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); // need this for the window manager
}

public static void main(String[] args) {
    javax.swing.SwingUtilities.invokeLater(new Runnable() {
        @Override
        public void run() {
            new Test();
        }
    });
}

All Swing-related events should be put on the Event Dispatch Thread, which is why you should use the invokeLater(Runnable) method. You need to use another parent panel so you can have the grid in the center and the timerPanel on the left. BorderLayout is probably the best layout manager for this. It adds the grid panel to the center and the timerPanel to the left. Anything related with timing should be added to the timerPanel. Any other components should be added like this:

所有与Swing相关的事件都应该放在Event Dispatch Thread上,这就是你应该使用invokeLater(Runnable)方法的原因。您需要使用另一个父面板,以便可以将网格放在中间,将timerPanel放在左侧。 BorderLayout可能是最好的布局管理器。它将网格面板添加到中心,将timerPanel添加到左侧。应该将与时间相关的任何内容添加到timerPanel。应该像这样添加任何其他组件:

getContentPane().add(yourComponent, constraint);

where constraint is one of BorderLayout.NORTH, BorderLayout.SOUTH, BorderLayout.WEST, BorderLayout.EAST, etc. Hope this helped!

其中约束是BorderLayout.NORTH,BorderLayout.SOUTH,BorderLayout.WEST,BorderLayout.EAST等之一。希望这有帮助!

#1


0  

Try using this (untested):

尝试使用此(未经测试):

public Test() {
    setContentPane(new JPanel(new BorderLayout()));
    JPanel p = new JPanel(new GridLayout(9, 9));
    JButton btn;
    bomb = new double[81];
    for (int i = 0; i < 81; i++)
        bomb[i] = Math.random();
    for (int i = 0; i < 81; i++) {
        btn = new JButton();
        btn.addActionListener(this);
        btn.setActionCommand(Integer.toString(i));
        p.add(btn);
    }
    getContentPane().add(p, BorderLayout.CENTER); // add the JPanel
    JPanel timerPanel = new JPanel();
    timerPanel.add(timelabel);
    getContentPane().add(timerPanel, BorderLayout.EAST);
    setLocation(32, 32); // where my upper left hand corner goes
    // bframe.setSize(64*width, 64*height);
    setSize(500, 500);
    setVisible(true); // I start out invisible
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); // need this for the window manager
}

public static void main(String[] args) {
    javax.swing.SwingUtilities.invokeLater(new Runnable() {
        @Override
        public void run() {
            new Test();
        }
    });
}

All Swing-related events should be put on the Event Dispatch Thread, which is why you should use the invokeLater(Runnable) method. You need to use another parent panel so you can have the grid in the center and the timerPanel on the left. BorderLayout is probably the best layout manager for this. It adds the grid panel to the center and the timerPanel to the left. Anything related with timing should be added to the timerPanel. Any other components should be added like this:

所有与Swing相关的事件都应该放在Event Dispatch Thread上,这就是你应该使用invokeLater(Runnable)方法的原因。您需要使用另一个父面板,以便可以将网格放在中间,将timerPanel放在左侧。 BorderLayout可能是最好的布局管理器。它将网格面板添加到中心,将timerPanel添加到左侧。应该将与时间相关的任何内容添加到timerPanel。应该像这样添加任何其他组件:

getContentPane().add(yourComponent, constraint);

where constraint is one of BorderLayout.NORTH, BorderLayout.SOUTH, BorderLayout.WEST, BorderLayout.EAST, etc. Hope this helped!

其中约束是BorderLayout.NORTH,BorderLayout.SOUTH,BorderLayout.WEST,BorderLayout.EAST等之一。希望这有帮助!