Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/381.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
在帧的正确位置显示图像(JLables)的问题[Java]_Java_Swing - Fatal编程技术网

在帧的正确位置显示图像(JLables)的问题[Java]

在帧的正确位置显示图像(JLables)的问题[Java],java,swing,Java,Swing,我已经用java创建了一个基本程序,它创建了一张五张牌的扑克牌,然后在JFrame中显示它们。我基本上是在一个帧中显示五个图像,但不是在我认为它们应该在的位置上显示,它们看起来像 任何帮助都将不胜感激,谢谢 import java.awt.BorderLayout; import javax.swing.JFrame; import javax.swing.*; import java.awt.Color; import javax.swing.border.SoftBevelBorder;

我已经用java创建了一个基本程序,它创建了一张五张牌的扑克牌,然后在JFrame中显示它们。我基本上是在一个帧中显示五个图像,但不是在我认为它们应该在的位置上显示,它们看起来像

任何帮助都将不胜感激,谢谢

import java.awt.BorderLayout;

import javax.swing.JFrame;
import javax.swing.*;
import java.awt.Color;
import javax.swing.border.SoftBevelBorder;
import javax.swing.border.BevelBorder;
import java.awt.Point;
import java.net.URL;
import javax.smartcardio.Card;

public class PokerGUI extends JFrame {

    private static JPanel contentPane;
    private PokerBoard board;
    private static JLabel[] displayCards;
    private static Point[] cardCoords;
    private static final int LAYOUT_WIDTH_INC = 100;
    private static final int LAYOUT_HEIGHT_INC = 125;
    private static final int LAYOUT_TOP = 30;
    private static final int LAYOUT_LEFT = 30;
    private static boolean[] selections;

    public PokerGUI(PokerBoard gameBoard) {
        board = gameBoard;
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setBounds(100, 100, 530, 252);
        contentPane = new JPanel();
        setResizable(false);
        setTitle("SUPER COOL POKER GAME!!!!");
        contentPane.setBackground(Color.DARK_GRAY);
        contentPane.setBorder(new SoftBevelBorder(BevelBorder.LOWERED, new Color(0, 255, 0), new Color(0, 255, 0), new Color(0, 128, 0), new Color(0, 128, 0)));
        contentPane.setToolTipText("Super Cool Poker!!");
        contentPane.setLayout(new BorderLayout(0, 0));

        selections = new boolean[5];
        displayCards = new JLabel[5];
        Point one = new Point(30, 30);
        Point two = new Point(130, 30);
        Point three = new Point(230, 30);
        Point four = new Point(330, 30);
        Point five = new Point(430, 30);

        cardCoords = new Point[]{one, two, three, four, five};
        init();

        for (int k = 0; k < 5; k++) {
            String cardImageFileName = imageFileName(board.cardAt(k), selections[k]);
            URL imageURL = getClass().getResource(cardImageFileName);
            if (imageURL != null) {
                ImageIcon icon = new ImageIcon(imageURL);
                displayCards[k].setIcon(icon);
                displayCards[k].setVisible(true);
            } else {
                throw new RuntimeException("Card image not found: \"" + cardImageFileName + "\"");
            }
        }

        setContentPane(contentPane);
    }

    private static void init() {

        for (int k = 0; k < 5; k++) {
            displayCards[k] = new JLabel();
            contentPane.add(displayCards[k]);
            displayCards[k].setBounds(cardCoords[k].x, cardCoords[k].y, 73, 97);
            //displayCards[k].addMouseListener(new MyMouseListener());
            selections[k] = false;
        }
    }

    private String imageFileName(Card c, boolean isSelected) {
        String str = "cards/";
        if (c == null) {
            return "cards/back1.GIF";
        }
        str += c.rank() + c.suit();
        if (isSelected) {
            str += "S";
        }
        str += ".GIF";
        return str;
    }

}
导入java.awt.BorderLayout;
导入javax.swing.JFrame;
导入javax.swing.*;
导入java.awt.Color;
导入javax.swing.border.SoftBevelOrder;
导入javax.swing.border.BevelBorder;
导入java.awt.Point;
导入java.net.URL;
导入javax.smartcardio.Card;
公共类GUI扩展了JFrame{
私有静态JPanel内容窗格;
私人扑克局;
专用静态JLabel[]显示卡;
专用静态点[]cardCoords;
专用静态最终内部布局宽度INC=100;
专用静态最终内部布局高度INC=125;
专用静态最终int布局_TOP=30;
专用静态最终int布局_左=30;
私有静态布尔[]选择;
公共扑克GUI(扑克游戏板){
棋盘=棋盘;
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
立根(100100530252);
contentPane=newjpanel();
可设置大小(假);
setTitle(“超级酷扑克游戏!!!!”);
contentPane.setBackground(颜色:深灰色);
contentPane.setboorder(新的SoftBevelOrder(BevelOrder.LOWERED,新颜色(0,255,0),新颜色(0,255,0),新颜色(0,128,0),新颜色(0,128,0));
setToolTipText(“超级酷扑克!!”);
setLayout(新的BorderLayout(0,0));
选择=新布尔值[5];
显示卡=新的JLabel[5];
点1=新点(30,30);
第二点=新点(130,30);
第三点=新点(230,30);
第四点=新点(330,30);
第五点=新的点(430,30);
cardCoords=新点[]{1,2,3,4,5};
init();
对于(int k=0;k<5;k++){
字符串cardImageFileName=imageFileName(board.cardAt(k),选项[k]);
URL imageURL=getClass().getResource(文件名);
if(imageURL!=null){
ImageIcon图标=新的ImageIcon(imageURL);
显示卡[k]。设置图标(图标);
显示卡[k].setVisible(真);
}否则{
抛出新的运行时异常(“未找到卡片图像:\”“+cardImageFileName+”\”);
}
}
setContentPane(contentPane);
}
私有静态void init(){
对于(int k=0;k<5;k++){
displayCards[k]=新的JLabel();
contentPane.add(displayCards[k]);
显示卡[k].setBounds(cardCoords[k].x,cardCoords[k].y,73,97);
//displayCards[k].addMouseListener(新的MyMouseListener());
选择[k]=假;
}
}
私有字符串imageFileName(卡c,选择布尔值){
String str=“cards/”;
如果(c==null){
返回“cards/back1.GIF”;
}
str+=c.rank()+c.suit();
如果(当选){
str+=“S”;
}
str+=“.GIF”;
返回str;
}
}
其他的类不应该很重要,因为我的主要问题是让这五个图像显示在正确的位置

contentPane.setLayout(new BorderLayout(0, 0));
首先,将布局设置为BorderLayout,该布局仅接受中心的单个构件

contentPane.add(displayCards[k]);
displayCards[k].setBounds(cardCoords[k].x, cardCoords[k].y, 73, 97);
然后将所有卡片添加到中心

contentPane.add(displayCards[k]);
displayCards[k].setBounds(cardCoords[k].x, cardCoords[k].y, 73, 97);
然后完全忽略布局管理器,并尝试设置每张卡的边界

但是,当显示边框时,最后添加的卡片将由布局管理器设置其边界,这将覆盖您的边界

查看我在这篇帖子中的答案:更多关于为什么会发生这种情况的信息


我试着把它们排成一行


因此,解决方案是使用
流程布局
,让布局管理器确定每张卡的位置。不要试图手动设置每张卡的边界。

卡的布局方式有什么“错误”吗?是否要将它们放在网格中?我正在尝试在cardCoords[]中指定的点将它们显示在一行中。它们都在正确的位置,除了钻石杰克(jack of diamonds),它显示在左下角而不是最右下角。您的
contentPane
使用的是
BorderLayout
,因此它(试图)控制您添加到其中的最后一个组件的布局(通常忽略其余组件)。你可以考虑使用<代码> GridBagLayout < />代码,或者甚至是<代码> jaReleDePAN< <代码>啊,谢谢。我想我现在就评论一下。我对JFrames非常陌生,甚至不知道什么是边框布局。