繁体   English   中英

Java GUI:JLabel可以保存对象/类吗?

[英]Java GUI: Can the JLabel hold an object/class?

我正在构建Java国际象棋游戏,并且正在尝试构建它的GUI部分。 GUI板已经完成,我可以在板上放置零件。 我希望能够设置Rook类的新实例化,而不是简单地将图像设置为片段,而需要RLab类来实现颜色和图像。使车队遵守转弯和移动规则的方法。

据我了解,JLabel只能容纳图像或文本,因此我认为这可能是不可能的。 任何帮助,将不胜感激。 我的具体问题是:

  • JLabel是否可以成为带有颜色和图像的Rook对象? 如果是这样,怎么办?
  • 如果不可能的话,除了在木板上放置一块可以清晰地实例化(例如车子,典当……)并设置其图像和颜色的棋盘以外,还有哪些替代方案?

    任何帮助将不胜感激。

码:

    // Sets a piece on the board    
    JLabel piece = new JLabel("whiteRook.png"); //want this to be a new instantiation of a Rook
    JPanel panel = (JPanel) chessBoard.getComponent(0);
    panel.add(piece);

    //  Failed Attempt ---------------------------------------------------//

    //      String color = "white";
    //      ImageIcon WhiteRook = new ImageIcon("whiteRook.png");
    //      Rook firstRook = new Rook(color, WhiteRook);
    //      
    //      piece = new JLabel(firstRook);
    //      panel = (JPanel) chessBoard.getComponent(0);
    //      panel.add(piece);

我最近正在开发国际象棋应用程序。 我雇用的是64个方格中的每个方格的JLabel子类。

这是相关代码和示例屏幕截图

在此处输入图片说明

import javax.swing.*;
import javax.swing.border.Border;
import java.awt.*;

public class Cell extends JLabel
{
    Color backgroundColor;
    boolean highlight=false;
    Color highlightColor=new Color(132, 146, 255);

    Border blackBorder=BorderFactory.createLineBorder(Color.BLACK);

    public void setBackgroundImage(ImageIcon backgroundImage)
    {
        setIcon(backgroundImage);
    }

    public Cell(Color backGroundColor, ImageIcon backgroundImage)
    {
        super(backgroundImage);
        this.backgroundColor=backGroundColor;
        setBorder(blackBorder);
    }

    @Override
    protected void paintComponent(Graphics g)
    {
        if(!highlight) g.setColor(backgroundColor);
        else g.setColor(highlightColor);
        g.fillRect(0,0,getWidth(),getHeight());
        super.paintComponent(g);
    }

    public void setHighlightMode(boolean status)
    {
        if(status==highlight) return;
        highlight=status;
        repaint();
    }
}

你可以做这样的事情。 每一块都有一个自定义的JPanel 让建造者获取一个ChessPiece对象。 绘制图像并用颜色做一些事情

public class PiecePanel extends JPanel{
     private static final D_W = 50;
     private static final D_H = 50;
     BufferedImage img;
     Color color;

     public PiecePanel(ChessPiece piece) {
         color = piece.getColor();
         try {
             img = ImageIO.read(piece.getImagePath());
         } catch (IOException ex){
             ex.printStackTrace();
         }
     }

     protected void paintComponent(Graphics g){
         super.paintComponent(g);
         g.setColor(color);
         // do something with color
         g.drawImage(0,0,getWidth(), getHeight(), 0, 0, img.getWidth(), img.getHeight());
     }

     public Dimension getPreferredSize(){
         return new Dimension(D_W, D_H);
     }
}

然后,您可以像这样实例化每个ChessPiece面板

ChessPiece bRook1 = new Rook(path, color);
JPanel blackRook1 = new PiecePanel(bRook1);

您可以执行相同的子类JLabel。 但这是在JLabel上绘画的新习惯。 如果您不想做任何额外的绘画,只需Sublclass JLabel。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM