नीचे दिया गया उदाहरण कोड दिखाता है कि शतरंज बोर्ड के चारों ओर एक शतरंज के टुकड़े को कैसे खींचें। यह ग्लास फलक के बजाय जेलेयरपेन का उपयोग करता है, लेकिन मुझे यकीन है कि अवधारणाएं वही होंगी। यही कारण है:
क) जड़ फलक
ख) के लिए कांच फलक जोड़ने गिलास फलक दिखाई
ग) यकीन है कि सीमा से मान्य होते हैं
घ बनाने कांच फलक पर घटक जोड़ने) का उपयोग setLocation बनाने () घटक
संपादित की खींचने चेतन करने के लिए: जोड़ा कोड लेआउट प्रबंधकों का उपयोग जब आप setSize() या setBounds() तरीकों का उपयोग कभी नहीं 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);
ठीक करने के लिए। आपके मामले में आप केवल पसंदीदा आकार (0, 0) पर सेट करते हैं क्योंकि यह सभी घटकों का डिफ़ॉल्ट आकार है।
यह तब काम करता है जब आप फ्रेम में लेबल जोड़ते हैं क्योंकि फ्रेम के सामग्री फलक के लिए डिफ़ॉल्ट लेआउट प्रबंधक एक सीमा लेआउट है, इसलिए लेबल के पसंदीदा आकार को अनदेखा किया जाता है और लेबल को फ्रेम का आकार बनाया जाता है ।
हालांकि, डिफ़ॉल्ट रूप से एक जेपीनल फ़्लोलाउट का उपयोग करता है जो घटक के पसंदीदा आकार का सम्मान करता है। चूंकि पसंदीदा आकार 0 है, इसलिए पेंट करने के लिए कुछ भी नहीं है।
इसके अलावा, ग्लास फलक को चित्रित करने के लिए दृश्यमान बनाने की आवश्यकता है।
मेरा सुझाव है कि आप Swing tutorial पढ़ लें। लेआउट प्रबंधक कैसे काम करते हैं और ग्लास पैन कैसे काम करते हैं और प्रत्येक अनुभाग में काम करने वाले उदाहरण हैं इस पर अनुभाग हैं।
संपादित करें: उदाहरण कोड के नीचे कहा:
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);
}
}
आप glasspane करने के लिए इसे जोड़ने के बजाय glasspane के रूप में अपने घटक स्थापित करने की कोशिश की है? – willcodejavaforfood
यह घटक एक शब्द गेम में एक टाइल का प्रतिनिधित्व करता है, इसलिए यह केवल 37 * 37 है और प्रभाव के लिए सीमाओं का उपयोग करता है। मैं नहीं देखता कि मैं इसे एक ग्लास फलक में कैसे बदल सकता हूं क्योंकि यह टाइल के आयामों के साथ गड़बड़ कर देगा। –
क्या हम कुछ कोड देख सकते हैं? विशेष रूप से आप ग्लास फलक में घटक कैसे जोड़ते हैं और इसे दृश्यमान सेट करते हैं। –