2012-04-25 4 views
1

JLabel을 빨간색 사각형 위에 녹색 직사각형으로 시뮬레이션 한 상태 막대에 추가하려고합니다. JLabel을 선언하고이를 rectangleComponent에 첨부해도 결코 나타나지 않습니다. 어떤 아이디어? 여기에 내 rectangleComponent와 그 프레임이 초기화됩니다.자바에서는 직사각형에 JLabel을 어떻게 추가합니까?

import java.awt.Color; 
import java.awt.Dimension; 
import java.awt.Graphics; 
import java.awt.Graphics2D; 
import java.awt.geom.Rectangle2D; 

import javax.swing.JComponent; 
import javax.swing.JLabel; 

public class RectangleComponent extends JComponent 
{ 
    private Color color; 
    private int width; 
    private int height; 
    private int origWidth; 
    private JLabel label; 
    private Rectangle2D rectangle; 
    private boolean wantLabel; 
    private int xCoord; 
    private int yCoord; 

    public RectangleComponent(int x, int y, Color color, int width, int height, boolean wantLabel) 
    { 
     this.width = width; 
     this.height = height; 
     this.color = color; 
     origWidth = width; 
     xCoord = x; 
     yCoord = y; 
     this.wantLabel = wantLabel; 
     if(wantLabel) 
     { 
      label = new JLabel(this.width + "/" + origWidth); 
      label.setLabelFor(this); 
     } 
     setBounds(xCoord, yCoord, width, height); 
     rectangle = new Rectangle2D.Float(xCoord, yCoord, width, height); 
    } 

    public RectangleComponent(int x, int y, Color color, boolean wantLabel) 
    { 
     width = 125; 
     height = 18; 
     xCoord = x; 
     yCoord = y; 
     this.color = color; 
     origWidth = width; 
     this.wantLabel = wantLabel; 
     if(wantLabel) 
     { 
      label = new JLabel(this.width + "/" + origWidth); 
      label.setLabelFor(this); 
     } 
     setBounds(xCoord, yCoord, width, height); 
     rectangle = new Rectangle2D.Float(xCoord, yCoord, width, height); 
    } 

    public void paintComponent(Graphics g) 
    { 
     Graphics2D graphics2 = (Graphics2D) g; 
     rectangle = new Rectangle2D.Float(xCoord, yCoord, width, height); 
     graphics2.setPaint(color); 
     graphics2.fill(rectangle); 
     graphics2.draw(rectangle); 
     if(wantLabel) 
      label.setText(this.width + "/" + origWidth); 
    } 

    public Dimension getPreferredSize() 
    { 
     return (new Dimension(width, height)); 
    } 

    public void subtractLife(int amount) 
    { 
     width -= amount; 
     if(width > 0) 
     { 
      rectangle.setRect(xCoord, yCoord, width, height); 
      repaint(); 
     } 
     else 
      width = 0; 
    } 

    public void addLife(int amount) 
    { 
     width += amount; 
     if(width < origWidth) 
     { 
      rectangle.setRect(xCoord, yCoord, width, height); 
      repaint(); 
     } 
     else width = origWidth; 
    } 
} 


import java.awt.BorderLayout; 
import java.awt.Color; 
import java.awt.FlowLayout; 
import java.awt.GridLayout; 

import javax.swing.JFrame; 
import javax.swing.JPanel; 

public class GameFrame extends JFrame 
{ 
    private SpellBarComponent bar; 
    private JPanel mainPanel = new JPanel(); 
    private JPanel buttonPanel = new JPanel(); 
    Color green = new Color(29, 180, 29); 
    Color red = new Color(255, 0, 0); 
    private RectangleComponent life; 
    private RectangleComponent death; 

    public GameFrame(char x) 
    { 
     setSize(1024, 768); 
     setTitle("Game"); 
     setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE); 
     mainPanel.setLayout(null); 
     createPanels(x); 
     buttonPanel.setLayout(new FlowLayout(FlowLayout.LEFT, 5, 5)); 
     repaint(); 
     getContentPane().add(mainPanel, BorderLayout.CENTER); 
     getContentPane().add(buttonPanel, BorderLayout.PAGE_END); 
     setExtendedState(getExtendedState() | JFrame.MAXIMIZED_BOTH); 
     setLocationByPlatform(true); 
     life.subtractLife(10); 
     setVisible(true); 

    } 

    public RectangleComponent getLife() 
    { 
     return life; 
    } 

    private void createHealth() 
    { 
     life = new RectangleComponent(0, 0, green, true); 
     death = new RectangleComponent(0, 0, red, true); 
    } 

    private void createPanels(char x) 
    { 
     createBar(x); 
     createHealth(); 
     mainPanel.add(buttonPanel); 
     mainPanel.add(life); 
     mainPanel.add(death); 
     buttonPanel.add(bar.getSpell1()); 
     buttonPanel.add(bar.getSpell2()); 
     buttonPanel.add(bar.getSpell3()); 
    } 

    private void createBar(char x) 
    { 
     bar = new SpellBarComponent(x); 
    } 
} 

답변

4

귀하의 구성 요소에 add(label) 전화가 없습니다. 따라서 라벨은 라벨에 붙어 있지 않습니다. Btw, 나는 당신의 paintComponent에서 라벨을 수정하는 것이 정말로 금지되지 않는다고 생각한다. widh 및/또는 origWidth가 수정되면 해당 호출을 수행해야합니다.

+0

나는 그것을 추가했다고 생각한 label.setLabelFor (this)를 호출합니다. RectangleComponent에 대한 생성자에서 add (label)를 호출해도 문제가 해결되지 않습니다. – keyert

+1

@keyert 레이블의 경계를 설정하거나 LayoutManager를 설정하지 않고 JComponent를 확장하기 때문입니다. 레이블의 너비와 높이는 아마도 0입니다. RectangleComponent는이를 위해 JPanel을 확장 할 수 있습니다. 또한 setLabelFor를 설정해도 레이블이 구성 요소에 추가되지는 않습니다. 이는 간단히 니모닉에 사용됩니다. http://docs.oracle.com/javase/7/docs/api/javax/swing/JLabel.html#setLabelFor(java). awt.Component). 마지막으로 부울 플래그를 사용하여 레이블을 표시하거나 표시하지 않고 대신 레이블에서 setVisible을 사용하십시오. –

0

paintComponent에서 label.paint()에 대한 호출이 누락되었습니다. 또는 생성자에서 add (label)를 호출하여 레이블을이 컨테이너의 자식으로 추가하고 paintChildren이 처리하도록 할 수 있다고 생각합니다.

원하는 것은 모두 사용자 정의 배경의 레이블이므로 JLabel을 확장하고 사용자 정의 배경을 그리기 위해 paintComponent를 대체 한 다음 super.paintComponent를 호출하여 포 그라운드를 페인트하는 것이 좋습니다.

+0

내 paintComponent에 label.paint (g)를 추가했는데 문제가 해결되지 않았습니다. 나도 생성자에서 add (label)를 호출하지 않았다. – keyert

관련 문제