2016-07-01 3 views
-1

사각형의 색상을 변경하는 방법은 무엇입니까? 나는 그것을 노란 색으로 바꾸고 싶다. 안에 g.setColor(Color.YELLOW);을 추가했지만 사각형의 색상은 여전히 ​​동일하게 유지됩니다. 누군가 내가 뭘 잘못했는지 말해 줄 수 있니?Java GUI에서 직사각형 색상 변경

public class SelectSeat { 

    static JFrame frame; 

    public JPanel createContentPane() throws IOException 
    { 

     JPanel totalGUI = new JPanel(); 
     RectDraw rect= new RectDraw(); 
     rect.setPreferredSize(new Dimension(330,35)); //for size 
     totalGUI.setLayout(null); 
     totalGUI.setBackground(Color.WHITE); 
     totalGUI.add(rect); 
     Dimension d = rect.getPreferredSize(); 
     rect.setBounds(100, 20, d.width, d.height); // for location 
     return totalGUI; 
    } 

     void setVisible(boolean b) { 
     // TODO Auto-generated method stub 

    } 

    static void createAndShowGUI() throws IOException 
    { 

     JFrame.setDefaultLookAndFeelDecorated(true); 
     frame = new JFrame("Seat Selection"); 
     //Create and set up the content pane. 
     SelectSeat demo = new SelectSeat(); 
     frame.setContentPane(demo.createContentPane()); 
     frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
     frame.setSize(535, 520); 
     frame.setLocation(500,220); 
     frame.setVisible(true); 
    } 

    private static class RectDraw extends JPanel 
    { 
     protected void paintComponent(Graphics g) { 
      super.paintComponent(g); 
      g.setColor(Color.BLUE); 
      g.drawString("Movie Sceen", 130, 20); 
      } 


    } 

} 

enter image description here

+0

'g.setColor (Color.YELLOW);를 추가했습니다. '게시 된 코드에서 해당 호출이 표시되지 않습니다. 통화 후 직사각형을 채우시겠습니까? – copeg

답변

2

어떻게 사각형의 색상을 변경하려면? 나는 그것을 노란 색으로 바꾸고 싶다.

색상을 노란색으로 설정 한 다음 구성 요소의 크기로 직사각형을 채워야합니다.

protected void paintComponent(Graphics g) { 
    super.paintComponent(g); 
    g.setColor(Color.YELLOW); 
    g.fillRect(0,0,getWidth(), getHeight()); 
    g.setColor(Color.BLUE); 
    g.drawString("Movie Sceen", 130, 20); 
} 

그리고 무엇을 그 가치에 대한

는 :

totalGUI.setLayout(null); 

나는 널 (null) 레이아웃을 사용하여에 추천 할 것입니다. 작업에 적합한 LayoutManager을 사용하고 구성 요소 계층 내에서 레이아웃을 중첩 할 수 있음을 기억하십시오.

+0

고마워, 그것을 작동합니다 :) –

+0

null 레이아웃 대신 LayoutManager를 사용합니까? –

+0

예. null 레이아웃에서 벗어나고 싶은 이유에 대한 설명은 [이 응답] (http://stackoverflow.com/questions/17122338/swing-gridbaglayout-component-resizing/17122843#17122843)을 참조하십시오. – copeg