java - 为什么长方形没有显示在GUI里?
大家讲道理
大家讲道理 2017-04-18 09:23:41
0
1
308

求大神帮个忙,为什么长方形没有显示在GUI里?哪里出错了?

public class SelectSeat {
    
    static JFrame frame;

    public JPanel createContentPane() throws IOException
    {
        
        JPanel totalGUI = new JPanel();
        RectDraw rect= new RectDraw();
        rect.setPreferredSize(new Dimension(30,25)); 
        totalGUI.setLayout(null);
        totalGUI.setBackground(Color.WHITE);
        totalGUI.add(rect);
        
        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.drawRect(29,550,300,250);  
             g.setColor(Color.GRAY);  
             g.fillRect(20,5,330,25); 
             g.setColor(Color.BLUE);
             g.drawString("Movie Sceen", 150, 20);   
            }
        

    }

}

大家讲道理
大家讲道理

光阴似箭催人老,日月如移越少年。

모든 응답(1)
PHPzhong

이유는 totalGUI.setLayout(null);으로 인한 문제라 처음에는 이상하다고 생각했습니다

나중에 http://stackoverflow.com/questions/12308764/java-setlayoutnull이라는 질문을 언급했습니다

해결책은 setLayout(null);을 사용하지 않고 레이아웃 관리자

를 사용하는 것입니다.

또는 위 질문에 대한 답변 중 하나의 아이디어를 사용하여 표시 범위가 육안으로 보이지 않도록 표시하려는 개체에 경계를 추가합니다

으아악
최신 다운로드
더>
웹 효과
웹사이트 소스 코드
웹사이트 자료
프론트엔드 템플릿