java - GUI需要帮忙
阿神
阿神 2017-04-18 09:25:16
0
2
268

请问我该怎样写才能让长方形变长并且将它移下,摆在中间 ?谢谢!

public class SelectSeat extends JPanel {
    
    int a;
    Object source;
    int counter= 0;
    
    static JFrame frame = new JFrame("Select Seat");
    
     public static void main(String[] args) {
            SwingUtilities.invokeLater(() -> {
                try {
                    createAndShowGui();
                } catch (Exception e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
            });
        }
     
     static void createAndShowGui() throws Exception {
            frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            frame.getContentPane().add(new SelectSeat());
            frame.pack();
            frame.setLocationRelativeTo(null);
            frame.setVisible(true);
        }

    
    public SelectSeat() throws Exception
    {
        JPanel topPanel= new JPanel(new GridLayout(1, 15));
        RectDraw rect= new RectDraw();
        rect.setPreferredSize(new Dimension(30,25)); 
        topPanel.add(rect);
        
          JToggleButton[] ButtonList = new JToggleButton[30];
            
            JPanel ButtonPanel= new JPanel(new GridLayout(5,15,45,25)); // row,col,hgap,vgap
            for(int i = 0; i < 30; i++) {
                a=i+1;
                ButtonList[i]= new JToggleButton(""+a);
                ButtonPanel.add(ButtonList[i]); 
                
            }
                        
            JPanel bottomPanel = new JPanel(new GridLayout(1,5,40,20));
            JButton cancel= new JButton("Back");
            JButton confirm= new JButton("Next");
            bottomPanel.add(cancel);
            bottomPanel.add(confirm);
            
           setLayout(new BorderLayout(0, 45));
           add(topPanel, BorderLayout.PAGE_START);
           add(ButtonPanel, BorderLayout.CENTER);
           add(bottomPanel, BorderLayout.PAGE_END);
           ButtonPanel.setBorder(BorderFactory.createEmptyBorder(20, 20, 20, 20));
           bottomPanel.setBorder(BorderFactory.createEmptyBorder(0, 10, 20, 20)); //top,left,bottom,right
                  
             }

    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);   
            }
    }

}
 
阿神
阿神

闭关修行中......

모든 응답(2)
Ty80

이 코드는 직접 작성하신 건가요?
회색 직사각형이어야 합니다. 회색 직사각형과 파란색 문자열을 각각 제어하려면 마지막 함수에서 drawRect의 매개변수를 변경하면 됩니다.

洪涛

Eciplise라면 플러그인을 사용해 레이아웃을 직접 구축할 수 있습니다. (스윙만 사용한지 일주일 됐어요)

최신 다운로드
더>
웹 효과
웹사이트 소스 코드
웹사이트 자료
프론트엔드 템플릿
회사 소개 부인 성명 Sitemap
PHP 중국어 웹사이트:공공복지 온라인 PHP 교육,PHP 학습자의 빠른 성장을 도와주세요!