有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

Java Swing按钮未出现在窗体上

我的表单显示的按钮网格有问题。表单应该显示一个按钮网格,作为一个简单战舰游戏的平台

这是播放器网格面板的代码

package view;

import java.util.ArrayList;
import java.util.List;

import javax.swing.JPanel;

public class PlayerPanel extends JPanel {

    private List<PositionButton> ButtonList;


    public PlayerPanel()
    {

        ButtonList = new ArrayList<PositionButton>();
        for(int i = 0;i < 10;i++)
            for(int j = 0;j < 10;j++)
            {
                ButtonList.add(new PositionButton(i,j));
            }
        while(ButtonList.iterator().hasNext())
        {
            this.add(ButtonList.iterator().next());
        }
        while(ButtonList.iterator().hasNext())
        {
            ButtonList.iterator().next().setVisible(true);
        }
    }

}

这是实际表单的类:

package view;

import java.awt.Dimension;
import java.awt.GridLayout;

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

public class GameForm {


    private JFrame theFrame;
    private JPanel BoardPanel;
    private PlayerPanel p1Panel;
    private PlayerPanel p2Panel;



    public GameForm()
    {
        //set up the Frame
        theFrame = new JFrame();
        theFrame.setSize(new Dimension(800,500));
        theFrame.setVisible(true);
        theFrame.setResizable(false);
        theFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        //set up the board panel
        BoardPanel = new JPanel();
        theFrame.add(this.BoardPanel);
        //set up the first players board
        p1Panel = new PlayerPanel();
        p1Panel.setLayout(new GridLayout(0,0));
        this.BoardPanel.add(p1Panel);
        p1Panel.setSize(new Dimension(400,250));
        //set up the second players boards
        p2Panel = new PlayerPanel();
        p2Panel.setLayout(new GridLayout(0,0));
        this.BoardPanel.add(p2Panel);
        p2Panel.setSize(new Dimension(400,250));

        BoardPanel.setVisible(true);
        p1Panel.setVisible(true);
        p2Panel.setVisible(true);
    }
}

这是单个按钮的代码

package view;

import javax.swing.JButton;

public class PositionButton extends JButton {    

    private int x;
    private int y;      

    public PositionButton(int x, int y)
    {
        this.x = x;
        this.y = y;
        this.setVisible(true);
    }


    public int getX()
    {
        return this.x;
    }

    public int getY()
    {
        return this.y;  
    }               
}

我正在努力找出为什么我的按钮没有出现。提前谢谢你的帮助


共 (2) 个答案

  1. # 2 楼答案

    代码的一个主要问题是PositionButton重写了getX()getY()。似乎您希望使用这些方法来确定GridLayout中按钮的坐标,但是您将给它们起个别的名字。例如getXCoord()

    尝试查看此SSCCE以获取提示

    import java.awt.*;
    import javax.swing.*;
    import java.util.ArrayList;
    
    public class GameForm {
    
        private JFrame theFrame;
        private JPanel BoardPanel;
        private PlayerPanel p1Panel;
        private PlayerPanel p2Panel;
    
        public static void main(String[] args) {
            new GameForm();
        }
    
        public GameForm()
        {
            //set up the Frame
            theFrame = new JFrame();
            //set up the board panel
            BoardPanel = new JPanel(new BorderLayout());
            BoardPanel.setBackground(Color.BLUE);
            theFrame.add(this.BoardPanel);
            //set up the first players board
            p1Panel = new PlayerPanel();
            p1Panel.setBackground(Color.RED);
            this.BoardPanel.add(p1Panel);
    
            theFrame.setSize(new Dimension(800,500));
            theFrame.setVisible(true);
            theFrame.setResizable(false);
            theFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        }
    }
    
    class PlayerPanel extends JPanel {
    
        private java.util.List<PositionButton> ButtonList;
    
        public PlayerPanel()
        {
    
            ButtonList = new ArrayList<PositionButton>();
            for(int i = 0;i < 10;i++)
                for(int j = 0;j < 10;j++)
                {
                    PositionButton pb = new PositionButton(i,j);
                    ButtonList.add(pb);
                    add(pb);
                }
        }
    }
    
    class PositionButton extends JButton {
    
        private int x;
        private int y;
    
        public PositionButton(int x, int y)
        {
            this.x = x;
            this.y = y;
            this.setVisible(true);
        }
    
    
        public int getXCoord()
        {
            return this.x;
        }
    
        public int getYCoord()
        {
            return this.y;
        }
    }