首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Java如何在JFrame上安装两个JPanels

Java如何在JFrame上安装两个JPanels
EN

Stack Overflow用户
提问于 2013-03-14 05:06:27
回答 1查看 232关注 0票数 0

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

我已经正确地完成了上面的所有工作,它工作得很完美,我的问题是我需要放入一个计时器,这个计时器从0开始,在我玩游戏的时候不断地从0开始计数,直到我点击一个炸弹,它停止了。然后我会在我的板下面有一个地方,用户可以在那里输入他们的名字,在它下面是一个名为提交的按钮,它将标准出该人的姓名和时间。(请注意,玩家可以随时执行此操作)

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

代码语言:javascript
复制
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");
    }
}              

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

EN

回答 1

Stack Overflow用户

发布于 2013-03-14 05:45:29

尝试使用这个(未经测试):

代码语言:javascript
复制
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();
        }
    });
}

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

代码语言:javascript
复制
getContentPane().add(yourComponent, constraint);

其中constraintBorderLayout.NORTHBorderLayout.SOUTHBorderLayout.WESTBorderLayout.EAST等之一。希望这能有所帮助!

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/15396474

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档