2016-06-09 6 views
0

저는 재미있는 작은 자바 게임을 제 GUI 프로그래밍으로 연습하고 있습니다. 내 콘텐츠 창의 테두리를 배치하고 싶습니다 .Layout이 이미지이고, 특정 위치에 이미지의 맨 위에 "보이지 않는"단추를 넣고 싶습니다 (나중에 배치 할 수 있습니다. 지금 당장 작업 할 수 있습니다). 내 문제는 실제로 보이지 않는 버튼을 얻는 것입니다. 지금은 흰색 사각형을 그대로 두는 것 같습니다. 주위를 둘러 보았지만 제안 된 것만이 .setOpaque, .setContentAreaFilled 및 .setBorderPainted입니다. (게임과 관련된 공간, 이름 설명)이미지 상단에 보이지 않는 버튼 배치하기

   galaxyButton1 = new JButton(); 
       galaxyButton1.setFont(starSystem); 
       galaxyButton1.setBorder(BorderFactory.createEmptyBorder(25,25,25,25)); 
       galaxyButton1.setOpaque(false); 
       galaxyButton1.setContentAreaFilled(false); 
       galaxyButton1.setBorderPainted(false); 
       Color invis = new Color(Color.TRANSLUCENT); 
       galaxyButton1.setForeground(invis); 
       galaxyButton1.setBackground(invis); 
       galaxyButton1.addActionListener(new ButtonHandler()); 
       JPanel centerPanel = new JPanel(); 
       centerPanel.setLayout(new BorderLayout()); 
       JPanel buttons = new JPanel(); 
       buttons.setLayout(new GridLayout(1,0,5,5)); 
       buttons.setOpaque(false); 
       buttons.add(galaxyButton1); 
       centerPanel.add(buttons,BorderLayout.CENTER); 
       centerImg.setLayout(new GridBagLayout()); 
       centerImg.add(centerPanel); 
       contentPane.add(centerImg, BorderLayout.CENTER); 
+0

가능한 중복 질문 : [SO] (http://stackoverflow.com/questions/4585867/transparent-jbutton) –

+0

내가 그 문제의 모든 솔루션을 시도, 그들은 나를 위해 작동하지 않았다 나는 때문에 버튼을 이미지 위에 놓습니다. (그게 문제의 원인이라고 생각합니다) – ExileVirtigo

+0

마우스 입력을받는 수업이 있습니까? 그렇다면 이미지 주위에 직사각형을 만들고 마우스의 [x, y] 위치가 사각형과 교차하면 어떤 일이 일어나야 하는지를 호출하십시오. –

답변

0

다음은 자신의 버튼을 만드는 방법에 대한 코드 개요입니다. 다른 클래스에서 실제 마우스를 설정해야합니다.

public class InvisibleButton implements MouseListener{ 

    private final Rectangle rectangle; 

    public InvisibleButton(int x, int y, int width, int height){ 
     rectangle = new Rectangle(x,y,width,height); 
    } 

    @Override 
    public void mouseClicked(MouseEvent e) { 
     int x = 0; // Set this to Mouse X 
     int y = 0; // Set this to Mouse Y 
     if(rectangle.contains(x,y)){ 
      //Set Something to True or do action here 
     } 
    } 

    @Override 
    public void mousePressed(MouseEvent e) { 

    } 

    @Override 
    public void mouseReleased(MouseEvent e) { 

    } 

    @Override 
    public void mouseEntered(MouseEvent e) { 

    } 

    @Override 
    public void mouseExited(MouseEvent e) { 

    } 
} 
관련 문제