2013-09-29 214 views
-1

爲什麼「繪圖」不出現在這裏?我將它添加到不同的JPanel,然後將所有內容添加到另一個JPanel並返回。但是,我看到的是軌跡球將元素添加到JPanel

public class Draw extends JFrame 
{ 
    private JSplitPane itemPane; 

    private Point position = null; 

     public Draw() 
     { 
      // Set the layout to a grid 
      setLayout (new BorderLayout (5,5)); 
      // Set the properties of the window 
      setTitle ("Emulator"); 
      setSize (900, 700); 
      setDefaultCloseOperation (EXIT_ON_CLOSE); 
      setBackground (new Color (15, 255, 10)); 

      // Add the components 
      addComponents(); 
     } 


     public static void startWindowsGui() 
     { 
      // We are in the static main, set the form to invoke later 
      SwingUtilities.invokeLater (new Runnable() 
      { 
       public void run() 
       { 
        // Create a new instance of server and set it to visible 
        Draw gui = new Draw(); 
        gui.setVisible (true); 
       } 
      }); 
     } 

     private void addComponents() 
     { 
      // Create the main and an itemPane 
      JSplitPane mainPane = new JSplitPane (JSplitPane.HORIZONTAL_SPLIT); 
      setItemPane(new JSplitPane (JSplitPane.VERTICAL_SPLIT)); 

      mainPane.add (createPane (), JSplitPane.LEFT); 
      mainPane.add (getItemPane(), JSplitPane.RIGHT); 
      mainPane.setOneTouchExpandable (true); 

      getItemPane().setOpaque(true); 
      getItemPane().setBackground(new Color(0xffffffc0)); 

      JPanel p = new JPanel(); 
      this.getItemPane().add(p); 

      add (mainPane, BorderLayout.CENTER); 
     } 

     public static void main(final String[] args) 
     { 
      Runnable gui = new Runnable() 
      { 
       @Override 
       public void run() 
       { 
        new Draw().setVisible(true); 
       } 
      }; 
      SwingUtilities.invokeLater(gui); 
     } 

     private class Drawing extends JPanel { 

      private static final long serialVersionUID = 1L; 
      private final Point position; 

      public Drawing(Point position) { 
       this.position = position; 
      } 

      @Override 
      protected void paintComponent(Graphics g) { 
       super.paintComponent(g); 
       Graphics2D g2 = (Graphics2D) g; 
       g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, 
         RenderingHints.VALUE_ANTIALIAS_ON); 
       g2.setColor(Color.RED); 
       g2.fillOval(160 + position.x, 160 - position.y, 15, 15); 
      } 
     } 

     private JPanel createPane() 
     { 
      // Create the feedPanel 
      JPanel panel = new JPanel (); 
      JPanel panel2 = new JPanel (); 
      JPanel panel3 = new JPanel (); 
      this.position = new Point(); 

      TrackBall myJoystick = new TrackBall(150, position, 100); 
      panel.add(myJoystick, BorderLayout.PAGE_END); 
      Drawing drawing = new Drawing(position); 

      panel2.add(drawing, BorderLayout.PAGE_START); 

      panel3.add(panel2); 
      panel3.add(panel); 
      return panel3; 
     } 
     public JSplitPane getItemPane() {return itemPane;} 
     public void setItemPane(JSplitPane itemPane) {this.itemPane = itemPane;} 
} 

回答

2

3個問題

  • panel2需要BorderLayout而不是默認的FlowLayoutBorderLayout.PAGE_START約束使用
  • Drawing需要重寫getPreferredSize
  • getPreferredSize指定的Dimension應該足夠大以容納t他position在構造函數中指定Drawing

閱讀:Changing preferred size of a Swing component