10

我有一个 JLabel 的子类,它构成了我的 GUI 的一个组件。我已经实现了将组件从一个容器拖放到另一个容器的能力,但没有任何视觉效果。在将项目从一个容器拖动到另一个容器期间,我想让这个 JLabel 跟随光标。我想我可以创建一个玻璃窗格并将其绘制在上面。但是,即使我将组件添加到玻璃窗格,将组件设置为可见,并将玻璃窗格设置为可见,并将玻璃窗格设置为不透明,我仍然看不到该组件。我知道该组件可以工作,因为我可以将它添加到内容窗格并让它显示出来。

如何将组件添加到玻璃窗格?


终于想出了如何让这个简单的例子工作。谢谢,@akf。我能够使这个解决方案适应我最初的问题,允许我删除大约 60 行手动呈现 JLabel 表示的 Java2D 代码。

package test;

import java.awt.Color;

import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.border.LineBorder;

public class MainFrame extends JFrame {

    /**
     * @param args
     */
    public static void main(String[] args) {
        MainFrame mf = new MainFrame();
        mf.setSize(400, 400);
        mf.setLocationRelativeTo(null);
        mf.setDefaultCloseOperation(DISPOSE_ON_CLOSE);
        mf.setGlassPane(new JPanel());

        JLabel l = new JLabel();
        l.setText("Hello");
        l.setBorder(new LineBorder(Color.BLACK, 1));
        l.setBounds(10, 10, 50, 20);
        l.setBackground(Color.RED);
        l.setOpaque(true);
        l.setPreferredSize(l.getSize());

        //mf.add(l);
        ((JPanel)mf.getGlassPane()).add(l);
        mf.getGlassPane().setVisible(true);

        mf.setVisible(true);
    }
}
4

4 回答 4

13

下面的示例代码显示了如何在棋盘周围拖动棋子。它使用 JLayeredPane 而不是玻璃窗格,但我确信概念是相同的。那是:

a) 将玻璃窗格添加到根窗格
b) 使玻璃窗格可见
c) 将组件添加到玻璃窗格,确保边界有效
d) 使用 setLocation() 为组件的拖动设置动画

编辑:添加代码来修复 SSCCE

JLabel l = new JLabel();
l.setText("Hello");
l.setBorder(new LineBorder(Color.BLACK, 1));
// l.setPreferredSize(l.getSize());
// l.setBounds(10, 10, 50, 20);
((JPanel)mf.getGlassPane()).add(l);

mf.setVisible(true);
mf.getGlassPane().setVisible(true);

使用布局管理器时,您永远不会使用 setSize() 或 setBounds() 方法。在您的情况下,您只需将首选大小设置为 (0, 0),因为这是所有组件的默认大小。

当您将标签添加到框架时,它会起作用,因为框架的内容窗格的默认布局管理器是边框布局,因此标签的首选大小被忽略,标签成为框架的大小。

但是,默认情况下,JPanel 使用 FlowLayout,它确实尊重组件的首选大小。由于首选大小为 0,因此无需绘制任何内容。

此外,需要使玻璃窗格可见才能对其进行涂漆。

我建议您阅读Swing 教程。有关于布局管理器如何工作以及玻璃窗格如何工作的部分,每个部分都有工作示例。

编辑:下面添加的示例代码:

import java.awt.*;
import java.awt.event.*;
import java.util.*;
import javax.swing.*;

public class ChessBoard extends JFrame implements MouseListener, MouseMotionListener
{
    JLayeredPane layeredPane;
    JPanel chessBoard;
    JLabel chessPiece;
    int xAdjustment;
    int yAdjustment;

    public ChessBoard()
    {
        Dimension boardSize = new Dimension(600, 600);

        //  Use a Layered Pane for this this application

        layeredPane = new JLayeredPane();
        layeredPane.setPreferredSize( boardSize );
        layeredPane.addMouseListener( this );
        layeredPane.addMouseMotionListener( this );
        getContentPane().add(layeredPane);

        //  Add a chess board to the Layered Pane

        chessBoard = new JPanel();
        chessBoard.setLayout( new GridLayout(8, 8) );
        chessBoard.setPreferredSize( boardSize );
        chessBoard.setBounds(0, 0, boardSize.width, boardSize.height);
        layeredPane.add(chessBoard, JLayeredPane.DEFAULT_LAYER);

        //  Build the Chess Board squares

        for (int i = 0; i < 8; i++)
        {
            for (int j = 0; j < 8; j++)
            {
                JPanel square = new JPanel( new BorderLayout() );
                square.setBackground( (i + j) % 2 == 0 ? Color.red : Color.white );
                chessBoard.add( square );
            }
        }

        // Add a few pieces to the board

        ImageIcon duke = new ImageIcon("dukewavered.gif"); // add an image here

        JLabel piece = new JLabel( duke );
        JPanel panel = (JPanel)chessBoard.getComponent( 0 );
        panel.add( piece );
        piece = new JLabel( duke );
        panel = (JPanel)chessBoard.getComponent( 15 );
        panel.add( piece );
    }

    /*
    **  Add the selected chess piece to the dragging layer so it can be moved
    */
    public void mousePressed(MouseEvent e)
    {
        chessPiece = null;
        Component c =  chessBoard.findComponentAt(e.getX(), e.getY());

        if (c instanceof JPanel) return;

        Point parentLocation = c.getParent().getLocation();
        xAdjustment = parentLocation.x - e.getX();
        yAdjustment = parentLocation.y - e.getY();
        chessPiece = (JLabel)c;
        chessPiece.setLocation(e.getX() + xAdjustment, e.getY() + yAdjustment);

        layeredPane.add(chessPiece, JLayeredPane.DRAG_LAYER);
        layeredPane.setCursor(Cursor.getPredefinedCursor(Cursor.MOVE_CURSOR));
    }

    /*
    **  Move the chess piece around
    */
    public void mouseDragged(MouseEvent me)
    {
        if (chessPiece == null) return;

        //  The drag location should be within the bounds of the chess board

        int x = me.getX() + xAdjustment;
        int xMax = layeredPane.getWidth() - chessPiece.getWidth();
        x = Math.min(x, xMax);
        x = Math.max(x, 0);

        int y = me.getY() + yAdjustment;
        int yMax = layeredPane.getHeight() - chessPiece.getHeight();
        y = Math.min(y, yMax);
        y = Math.max(y, 0);

        chessPiece.setLocation(x, y);
     }

    /*
    **  Drop the chess piece back onto the chess board
    */
    public void mouseReleased(MouseEvent e)
    {
        layeredPane.setCursor(null);

        if (chessPiece == null) return;

        //  Make sure the chess piece is no longer painted on the layered pane

        chessPiece.setVisible(false);
        layeredPane.remove(chessPiece);
        chessPiece.setVisible(true);

        //  The drop location should be within the bounds of the chess board

        int xMax = layeredPane.getWidth() - chessPiece.getWidth();
        int x = Math.min(e.getX(), xMax);
        x = Math.max(x, 0);

        int yMax = layeredPane.getHeight() - chessPiece.getHeight();
        int y = Math.min(e.getY(), yMax);
        y = Math.max(y, 0);

        Component c =  chessBoard.findComponentAt(x, y);

        if (c instanceof JLabel)
        {
            Container parent = c.getParent();
            parent.remove(0);
            parent.add( chessPiece );
            parent.validate();
        }
        else
        {
            Container parent = (Container)c;
            parent.add( chessPiece );
            parent.validate();
        }
    }

    public void mouseClicked(MouseEvent e) {}
    public void mouseMoved(MouseEvent e) {}
    public void mouseEntered(MouseEvent e) {}
    public void mouseExited(MouseEvent e) {}

    public static void main(String[] args)
    {
        JFrame frame = new ChessBoard();
        frame.setDefaultCloseOperation( DISPOSE_ON_CLOSE );
        frame.setResizable( false );
        frame.pack();
        frame.setLocationRelativeTo( null );
        frame.setVisible(true);
     }
}
于 2010-04-01T18:29:46.637 回答
12

尽管与问题相切, @camickr 引用的JLayeredPane 示例承认以下改编,这突出mouseReleased()了现有组件的效果。

public ChessBoard() {
    ...
    // Add a few pieces to the board
    addPiece(3, 0, "♛"); 
    addPiece(4, 0, "♚");
    addPiece(3, 7, "♕");
    addPiece(4, 7, "♔");
}

static Font font = new Font("Sans", Font.PLAIN, 72);

private void addPiece(int col, int row, String glyph) {
    JLabel piece = new JLabel(glyph, JLabel.CENTER);
    piece.setFont(font);
    JPanel panel = (JPanel) chessBoard.getComponent(col + row * 8);
    panel.add(piece);
}
于 2010-04-01T20:12:44.520 回答
3

因为我很长时间以来一直在关注 Romain Guy 在 Swing 上的博客。我有一个你可能会感兴趣的链接。他发布了源代码——它使用 GlassPane 来实现 DnD 效果。

http://jroller.com/gfx/entry/drag_and_drop_effects_the

我自己从来没有在 DnD 上使用过泡沫动画/效果,所以不能进一步评论:-|

于 2010-04-01T18:55:41.823 回答
3

除了已经提供的指向 LayerPane 示例的指针之外,原始代码的问题集中在标签首选大小的设置上。您在 JLabel 调整大小之前设置它,因此您的:

l.setPreferredSize(l.getSize());

是无效的。另一方面,如果您在拨打电话后拨打电话setBounds,您将看到您想要的结果。考虑到这一点,重新排序:

l.setPreferredSize(l.getSize());
l.setBounds(10, 10, 50, 20);

看起来像这样:

l.setBounds(10, 10, 50, 20);
l.setPreferredSize(l.getSize());
于 2010-04-03T03:06:01.163 回答