Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/331.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java JPanel以额外宽度绘制_Java_Swing_Jpanel - Fatal编程技术网

Java JPanel以额外宽度绘制

Java JPanel以额外宽度绘制,java,swing,jpanel,Java,Swing,Jpanel,我有一个Stratego板画在一个JPanel上,用来设置球员的棋子。玩家正确排列两个不同的面板实例后,将显示主板(具有相同的格式和布局,只是具有不同的块外观和逻辑行为) 我的问题是,第二个输入面板和主板(目前没有任何功能或部件)有时会在底部和右侧设置额外的宽度,导致10x10网格不会像应该的那样占用整个板空间 播放器1的初始输入面板似乎工作正常,从未出现过此问题。第二个面板和主面板只是有时会有这个问题,所以我不完全确定这是从哪里来的 这里是设置面板和其他东西的主要方法 public class

我有一个Stratego板画在一个JPanel上,用来设置球员的棋子。玩家正确排列两个不同的面板实例后,将显示主板(具有相同的格式和布局,只是具有不同的块外观和逻辑行为)

我的问题是,第二个输入面板和主板(目前没有任何功能或部件)有时会在底部和右侧设置额外的宽度,导致10x10网格不会像应该的那样占用整个板空间

播放器1的初始输入面板似乎工作正常,从未出现过此问题。第二个面板和主面板只是有时会有这个问题,所以我不完全确定这是从哪里来的

这里是设置面板和其他东西的主要方法

public class Core {

    public static void main(String[] args) {

        LogicInterpreter logic = new LogicInterpreter();

        Dimension dim = Toolkit.getDefaultToolkit().getScreenSize();

        InputFrame inputPlayer1 = new InputFrame(logic, 1, "red", 600, 600);
        inputPlayer1.setLocation(dim.width / 2 - inputPlayer1.getSize().width/2,
            dim.height / 2 - inputPlayer1.getSize().height / 2);

        while(!logic.isSetUp1()){
            //Just to make it work
            try {
                Thread.sleep(100);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }

        //Now bring up board 2

        InputFrame inputPlayer2 = new InputFrame(logic, 2, "blue", 600, 600);
        inputPlayer2.setLocation(dim.width / 2 - inputPlayer2.getSize().width/2,
                dim.height / 2 - inputPlayer2.getSize().height / 2);

        while(!logic.isSetUp2()){
            try {
                Thread.sleep(100);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
        openBoards(logic);
    }

    public static void openBoards(LogicInterpreter logic) {
        try {
            Dimension dim = Toolkit.getDefaultToolkit().getScreenSize();
            MainBoard board = new MainBoard(logic);
            board.setLocation(dim.width / 2 - board.getSize().width / 2, dim.height / 2 - board.getSize().height / 2);
        } catch (Exception e) {
            System.exit(1);
        }
    }
}
此外,这里是输入面板中的代码。我不确定什么是相关的,所以我不能把东西拿出来。很抱歉我将保留主板设置代码,因为它实际上是相同的

public class InputFrame extends JFrame {
    private static final long serialVersionUID = 1L;
    private LogicInterpreter holder;
    private Panel2 jp;
    private int height, width;
    private Map<Integer, ArrayList<Integer>> lakeCoords = new HashMap<>();
    private List<Piece> pieces = new ArrayList<>();
    private int playernumber;
    private String playerColor;
    Piece selectedPiece;
    Piece secondSelectedPiece;
    boolean hidePieces = false;

    JButton submit = new JButton("SUBMIT");

    public void addCoords() {
        lakeCoords.put(3, new ArrayList<Integer>(Arrays.asList(6, 5)));
        lakeCoords.put(4, new ArrayList<Integer>(Arrays.asList(6, 5)));
        lakeCoords.put(7, new ArrayList<Integer>(Arrays.asList(6, 5)));
        lakeCoords.put(8, new ArrayList<Integer>(Arrays.asList(6, 5)));
    }

    public void createPieces() {
        int y = 1;

        if (playernumber == 2) {
            y = 7;
        }

        List<Integer> openValues = new ArrayList<>();

        openValues.add(1);
        openValues.add(2);
        openValues.add(11);
        openValues.add(12);
        for (int x = 0; x < 2; x++) {
            openValues.add(3);
        }
        for (int x = 0; x < 3; x++) {
            openValues.add(4);
        }
        for (int x = 0; x < 4; x++) {
            openValues.add(5);
            openValues.add(6);
            openValues.add(7);
        }
        for (int x = 0; x < 5; x++) {
            openValues.add(8);
        }
        for (int x = 0; x < 8; x++) {
            openValues.add(9);
        }
        for (int x = 0; x < 6; x++) {
            openValues.add(10);
        }

        Collections.sort(openValues);

        System.out.println(openValues.size());
        System.out.println(pieces.size());

        for (int x = 1; x <= 10; x++) {
            for (int z = y; z <= y + 3; z++) {  

                // 1x1 Marshal
                // 2x1 General
                // 3x2 Colonel
                // 4x3 Major
                // 5x4 Captain
                // 6x4 Lieutenant
                // 7x4 Sergeant
                // 8x5 Miner
                // 9x8 Scout
                // 10x6 Bomb
                // 11x1 Flag
                // 12x1 Spy 

                Piece piece = new Piece(new Coords(x, z), openValues.get(0), playerColor);

                openValues.remove(0);
                pieces.add(piece);
            }
        }
    }

    public InputFrame(LogicInterpreter holder, int playerNumber, String playerColor, int height, int width) {
        this.height = height;
        this.width = width;
        playernumber = playerNumber;
        this.playerColor = playerColor;
        setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
        addCoords();
        this.holder = holder;
        createPieces();
        jp = new Panel2(height, width);
        setResizable(false);
        jp.setBackground(new Color(235, 202, 158));
        setTitle("Player " + playerNumber + " Arrangement GUI     ||     Click Submit When Ready");
        jp.setPreferredSize(new Dimension(600, 600));
        jp.setLayout(null);
        jp.addMouseListener(new HandleMouse());
        if(playernumber == 1)
            submit.setBounds(width / 10 * 4, height / 10 * 7, width / 10 * 2, height / 10 * 2);
        else
            submit.setBounds(width / 10 * 4, height / 10, width / 10 * 2, height / 10 * 2);
        submit.setFont(new Font("Arial", Font.BOLD, width * 20 / 600));
        submit.setBackground(Color.LIGHT_GRAY);
        submit.addActionListener(new CloseListener(this));
        jp.add(submit);
        getContentPane().add(jp);
        pack();
        setVisible(true);
    }

    class CloseListener implements ActionListener {
        private InputFrame frame;

        public CloseListener(InputFrame frame) {
            this.frame = frame;
        }

        public void actionPerformed(ActionEvent event) {
            // Do the stuff here before closing
            hidePieces = true;
            repaint();
            if (playernumber == 1) {
                holder.setP1Pieces(pieces);
                JOptionPane.showMessageDialog(null, "Press When Ready for Next Player");
                holder.setSetUp1(true);
            } else {
                holder.setP2Pieces(pieces);
                JOptionPane.showMessageDialog(null, "Press When Player 1 is Ready");
                holder.setSetUp2(true);
            }
            frame.dispose();
        }
    }

    public class Panel2 extends JPanel {

        private static final long serialVersionUID = 1L;
        int height = 0;
        int width = 0;

        public Panel2(int height, int width) {
            this.height = height;
            this.width = width;
        }

        @Override
        public void paintComponent(Graphics g) {
            super.paintComponent(g);
            for (int x = 0; x < width; x += width / 10) {
                for (int y = 0; y < height; y += height / 10) {
                    boolean fill = false;
                    for (Entry<Integer, ArrayList<Integer>> coords : lakeCoords.entrySet()) {
                        if ((coords.getKey() - 1 == x / 60 && coords.getValue().get(0) - 1 == y / 60)
                                || (coords.getKey() - 1 == x / 60 && coords.getValue().get(1) - 1 == y / 60)) {
                            fill = true;
                            break;
                        }
                    }
                    if (fill) {
                        g.setColor(Color.BLUE);
                        g.fillRect(x, y, width / 10, height / 10);
                        g.setColor(Color.BLACK);
                        g.drawRect(x, y, width / 10, height / 10);
                    } else {
                        g.setColor(Color.BLACK);
                        g.drawRect(x, y, width / 10, height / 10);
                    }
                }
            }

            if(hidePieces){
                for (Piece piece : pieces) {
                    try {
                        g.drawImage(ImageIO.read(new File(playerColor + "_pieces/" + (playerColor.equals("blue") ? "Blue" : "Red") + "_Strat_Piece"
                                + ".png")), piece.getX() * width / 10 - width / 10,
                                piece.getY() * height / 10 - height / 10, width / 10, height / 10, null);
                    } catch(Exception e){}
                }
            } else {
                for (Piece piece : pieces) {
                    g.drawImage(piece.getImage(), piece.getX() * width / 10 - width / 10,
                            piece.getY() * height / 10 - height / 10, width / 10, height / 10, null);
                }

                if (selectedPiece != null) {
                    g.setColor(Color.BLUE);
                    g.drawImage(selectedPiece.getImage(), selectedPiece.getX() * width / 10 - width / 10,
                            selectedPiece.getY() * height / 10 - height / 10, width / 10, height / 10, null);
                    g.drawRect(selectedPiece.getX() * width / 10 - width / 10,
                            selectedPiece.getY() * height / 10 - height / 10, width / 10, height / 10);
                }
            }
        }
    }

    private class HandleMouse extends MouseAdapter {
        public void mousePressed(MouseEvent e) {
            int x = e.getX();
            int y = e.getY();
            Coords coordinates = holder.getClickedBox(x, y, width, height);
            boolean found = false;
            boolean move = false;

            for (Piece piece : pieces) {
                if (piece.getX() == coordinates.getX() && piece.getY() == coordinates.getY()) {
                    found = true;
                    if (selectedPiece == null) {
                        selectedPiece = piece;
                    } else {
                        move = true;
                        secondSelectedPiece = piece;
                    }
                }
            }
            if (move) {
                pieces.remove(selectedPiece);
                pieces.remove(secondSelectedPiece);
                Coords storage = selectedPiece.getCoords();
                selectedPiece.setCoords(secondSelectedPiece.getCoords());
                secondSelectedPiece.setCoords(storage);
                pieces.add(selectedPiece);
                pieces.add(secondSelectedPiece);
                selectedPiece = null;
                secondSelectedPiece = null;
            } else if (!found) {
                if (selectedPiece != null) {
                    selectedPiece = null;
                }
            }
            repaint();
        }
    }
}
公共类InputFrame扩展了JFrame{
私有静态最终长serialVersionUID=1L;
私人口译持有者;
私人小组2 jp;
私家车的高度、宽度;
private Map lakeCoords=新HashMap();
私有列表项=新的ArrayList();
私人int玩家编号;
私人弦乐演奏者彩色;
单件选择单件;
第二件选择第二件;
布尔hidePieces=false;
JButton submit=新JButton(“提交”);
public void addCoords(){
lakeCoords.put(3,新数组列表(Arrays.asList(6,5));
lakeCoords.put(4,新数组列表(Arrays.asList(6,5));
lakeCoords.put(7,新数组列表(Arrays.asList(6,5));
lakeCoords.put(8,新数组列表(Arrays.asList(6,5));
}
公共void createPieces(){
int y=1;
如果(playernumber==2){
y=7;
}
List openValues=new ArrayList();
增加(1);
增加(2);
openValues.add(11);
增加(12);
对于(int x=0;x<2;x++){
增加(3);
}
对于(int x=0;x<3;x++){
增加(4);
}
对于(int x=0;x<4;x++){
增加(5);
增加(6);
增加(7);
}
对于(int x=0;x<5;x++){
增加(8);
}
对于(int x=0;x<8;x++){
增加(9);
}
对于(int x=0;x<6;x++){
增加(10);
}
Collections.sort(openValues);
System.out.println(openValues.size());
System.out.println(partes.size());

对于(int x=1;x您是否检查了“有时”添加了多少像素

当我在jframe上调用setResizeable(true)时,我注意到“有时”宽度和高度都比我预期的多10个像素

如果这是你的情况,我回答了一个类似的问题。 我用一个“技巧”解决了这个问题,多次给pack打电话


在回答中,我还链接了另一个类似的问题(),在那里我无法找到另一个解决方案,但有一些用户描述了该错误。

您是否检查了“有时”添加了多少像素

当我在jframe上调用setResizeable(true)时,我注意到“有时”宽度和高度都比我预期的多10个像素

如果这是你的情况,我回答了一个类似的问题。 我用一个“技巧”解决了这个问题,多次给pack打电话


在回答中,我还联系了另一个类似的问题(),在那里我无法找到其他解决方案,但有一些用户描述了该错误。

很难弄清楚到底发生了什么,特别是因为你说这是一个随机的问题。你在调试方面做了哪些尝试?你是否尝试过删除/设置任何东西以查看问题是否仍然存在?“有时在底部和右侧设置额外的宽度"偶尔出现的问题通常与未在EDT上启动GUI有关。为了更快地获得更好的帮助,请发布或。很难弄清楚发生了什么,特别是因为您说这是一个随机问题。您在调试方面做了哪些尝试?您是否尝试过删除/设置任何内容以查看问题是否存在“有时在底部和右侧设置额外宽度”偶尔出现的问题通常与没有在EDT上启动GUI有关。为了更快地获得更好的帮助,请发布or。您完全理解了!非常感谢!我们不会很快解决这个问题…您完全理解了!非常感谢!我们不会很快解决这个问题。。。