프레임을 채우는 배경 이미지와 이미지 위에 드래그되는 JLabel이있는 응용 프로그램이 있습니다. 레이블을 드래그 할 때 (레이블 뒤에 흔적이 생겼을 때) 배경 이미지를 연속적으로 그리고 올바르게 다시 그리는 데 어려움을 겪고 있었고 JPanel에 배경 이미지를 놓은 다음 JLabels을 드래그 앤 드롭하는 데 사용할 투명 JPanel을 맨 위에 두었습니다. . 그러나, 내 JLabels 나타나지 않습니다. 배경 이미지 위에 레이블을 드래그 할 수 있도록 코드를 수정하려면 어떻게해야합니까?배경 위에 이미지 끌어서 놓기 구성 요소
메인 클래스 :
public class InteractionBlocks {
public static void main(String[] args) {
BlocksFrame frame = new BlocksFrame();
frame.setVisible(true);
frame.setSize(1000, 650); //full size is 8000, 5000
frame.setResizable(false);
frame.setLocationRelativeTo(null);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
}
BlocksFrame 클래스 :
@SuppressWarnings("serial")
public class BlocksFrame extends JFrame implements MouseListener, MouseMotionListener {
private JPanel panel = new JPanel(null);
private BackgroundPanel backgroundPanel = new BackgroundPanel(null);
private boolean drag = false;
//Block is a JLabel with additional information stored
private Block qa = new Block(Pattern.QUESTION_ANSWER, Actor.HUMAN);
public BlocksFrame() {
this.add(backgroundPanel);
panel.setOpaque(true);
backgroundPanel.add(panel);
addButtons();
}
private void addButtons() {
panel.add(qa);
qa.setBounds(100, 50, qa.getIcon().getIconWidth(), qa.getIcon().getIconHeight());
qa.addMouseMotionListener(this);
qa.addMouseListener(this);
}
@Override
public void mouseDragged(MouseEvent e) {
if (drag == true) {
JComponent jc = (JComponent) e.getSource();
jc.setLocation(jc.getX() + e.getX(), jc.getY() + e.getY());
}
}
@Override
public void mousePressed(MouseEvent e) {
drag = true;
}
@Override
public void mouseReleased(MouseEvent e) {
((Block) e.getSource()).setLocation(((Block) e.getSource()).getX(), 300);
drag = false;
}
//additional @Override methods that are empty
}
BackgroundPanel 클래스 :
public class BackgroundPanel extends JPanel {
public BackgroundPanel(LayoutManager mngr) {
super(mngr);
}
public void paint(Graphics g) {
super.paint(g);
ImageIcon background = new ImageIcon("./figures/ui_new.png");
g.drawImage(background.getImage(), 0, 0, null);
}
}
나는이 두 가지 제안을 모두 고맙게 생각합니다. 제 패널이 불투명하지 않았다고보고 싶었습니다. 그러나 JLabel은 여전히 나타나지 않습니다. 추가 제안 사항이 있으십니까? –
MouseListener를'Block' 컴포넌트에 추가하지 말고 드래그 패널에 추가하면 포인트가 더 잘 이해 될 것입니다 ... – MadProgrammer
죄송합니다, 의식이 흐르지는 마시고, 제거하면 두 번째 투명 패널을 만든 다음 BackgroundPanel에 버튼을 직접 추가하면 모두 작동하는 것 같습니다. 이 접근법에 단점이 있습니까? –