小编use*_*877的帖子

试图了解javax.swing的细节

所以我一直在尝试获取javax.swing的句柄并且遇到了一些麻烦.我正在尝试实现'8拼图',其中在3x3网格中设置了8个拼贴和一个打开点,当点击与打开点相邻的拼块时,它与空位交易空间.我的结构包含一个JFrame,它包含一个JPanel,而JPanel包含9个tile作为JComponents,但是只有JPanel渲染,并且找不到任何图块.任何有关此问题的帮助将不胜感激.

    import javax.swing.*;



    public class MainFrame{

        public static void main(String[] args){
            JFrame frame = new JFrame("8 Puzzle");
            frame.setVisible(true);
            frame.setSize(600, 600);
            frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

            PieceManager pm = new PieceManager();
            frame.add(pm);

        }
   }

    import java.awt.Color;
    import java.awt.GridLayout;
    import java.awt.event.MouseEvent;
    import java.awt.event.MouseListener;
    import java.util.ArrayList;
    import java.util.Arrays;
    import java.util.Collections;
    import java.util.Random;

    import javax.swing.*;
Run Code Online (Sandbox Code Playgroud)

二等

    public class PieceManager extends JPanel{

        int[] possmoves;
        GameTile[] pieces;
        int openSpot;

        public PieceManager(){
            this.setSize(600,600);
            this.setBackground(new Color(255,255,255));
            this.setLayout(new GridLayout(3,3));
            pieces = new GameTile[9];
            this.init();
            this.addMouseListener(new ClickAction());
        }

        public void init(){
            ArrayList<Integer> nums = …
Run Code Online (Sandbox Code Playgroud)

java swing jcomponent jpanel jframe

1
推荐指数
1
解决办法
176
查看次数

标签 统计

java ×1

jcomponent ×1

jframe ×1

jpanel ×1

swing ×1