我正在尝试将我添加到面板中的JButton数组添加到我的JFrame中,但没有任何内容出现 [英] I am trying to add a JButton array that I added to a panel onto my JFrame, yet nothing is showing up

查看:116
本文介绍了我正在尝试将我添加到面板中的JButton数组添加到我的JFrame中,但没有任何内容出现的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我的代码:
由于某种原因,我的屏幕上不会显示任何内容,但我不知道为什么,我相信我正确地初始化并添加它。救命?

Here is my code: For some reason nothing will appear on my screen, yet I don't know why, I believe I am initializing it correctly and adding it. Help?

import java.awt.BorderLayout;
import java.awt.GridLayout;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;


public class main implements MouseListener{

final int WIDTH = 800, HEIGHT = 500, BOARD_WIDTH = 10, BOARD_HEIGHT = 10;
private JButton [][]buttons = new JButton[BOARD_WIDTH][BOARD_HEIGHT];

public static void main(String[] args) {
    // TODO Auto-generated method stub
    new main();
}

public main()
{
    Start();
}

private void Start()
{

    JFrame mainFrame = new JFrame("MineSweeper");
    mainFrame.setVisible(true);
    mainFrame.setSize(WIDTH,HEIGHT);
    mainFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    mainFrame.setLocationRelativeTo(null);
    mainFrame.setResizable(false);
    mainFrame.setLayout(new BorderLayout());

    JPanel p1 = new JPanel();
    p1.setLayout(new GridLayout(BOARD_WIDTH, BOARD_HEIGHT));

    for(int x = 0; x < BOARD_WIDTH; x++)
        for(int y = 0; y < BOARD_HEIGHT; y++)
        {
            buttons[x][y] = new JButton("01");
            buttons[x][y].addMouseListener(this);
            p1.add(buttons[x][y]);
        }

    mainFrame.add(p1, BorderLayout.CENTER); 
}

@Override
public void mouseClicked(MouseEvent arg0) {
    // TODO Auto-generated method stub

}

@Override
public void mouseEntered(MouseEvent arg0) {
    // TODO Auto-generated method stub

}

@Override
public void mouseExited(MouseEvent arg0) {
    // TODO Auto-generated method stub

}

@Override
public void mousePressed(MouseEvent arg0) {
    // TODO Auto-generated method stub

}

@Override
public void mouseReleased(MouseEvent arg0) {
    // TODO Auto-generated method stub

}

}

感谢您的帮助!
也很抱歉,我的按钮不会出现在屏幕上而不会出现框架。

Thanks for any help! Also sorry for any confusion it is that my buttons wont appear on the screen not that the frame will no appear.

推荐答案

致电 mainFrame.setVisible(true); last

private void Start()
{

    JFrame mainFrame = new JFrame("MineSweeper");
    // Move this...
    //mainFrame.setVisible(true);
    //...
    mainFrame.add(p1, BorderLayout.CENTER); 
    // To here
    mainFrame.setVisible(true);
}

您还应该使用EDT的上下文启动您的应用程序。请查看初始主题以获取更多详细信息

You should also launch you application with the context of the EDT. Take a look at Initial Threads for more details

您还应该避免在按钮上使用 MouseListener ,它们有 ActionListener API,包括用户点击按钮或活动键时的通知(通常输入空格

You should also avoid using a MouseListener on buttons, they have a ActionListener API which includes notification when the use clicks the button or "active" key (usually Enter or Space)

这篇关于我正在尝试将我添加到面板中的JButton数组添加到我的JFrame中,但没有任何内容出现的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆