2013-02-18 85 views
0

我創建了移動屏幕周圍的GUI箱一個簡單的指南針計劃JPanels,我現在進展到添加NE,NW等添加多個按鈕使用

這包括使用JPanels,因爲這允許多個對象。我的問題是,我已經爲單獨的按鈕製作了9個獨立的面板,但是我不知道如何將它們添加到JFrame中,因爲我所做的每件事似乎都不起作用。

任何想法,將不勝感激。

import java.awt.*; 
import java.awt.event.*; 
import javax.swing.*; 

public class MovingCompassExtraJFrame extends JFrame implements ActionListener { 

    private JButton north; 
    private JButton east; 
    private JButton west; 
    private JButton south; 
    private JButton center; 
    private JButton northEast; 
    private JButton northWest; 
    private JButton southEast; 
    private JButton southWest; 
    int screenHeight; 
    int screenWidth; 
    int height; 
    int width; 

    public MovingCompassExtraJFrame() 
    { 
     super(); 

      width = 400; 
      height = 300; 

      setSize(width, height); 
      setLocation(200, 100); 
      setDefaultCloseOperation(EXIT_ON_CLOSE); 

      getContentPane().setBackground(Color.RED); 
      getContentPane().setLayout(new GridLayout(3,3)); 

      setupNEPanel(); 
      setupNPanel(); 
      setupNWPanel(); 
      setupWPanel(); 
      setupCPanel(); 
      setupEPanel(); 
      setupSWPanel(); 
      setupSPanel(); 
      setupSEPanel(); 

      setVisible(true); 

    } 

    private JPanel setupNEPanel() 
     { 
      northEast = new JButton("Move to North East"); 

      JPanel northEP = new JPanel(); 

      getContentPane().add(northEast); 
      east.addActionListener(this); 

      return northEP; 
     } 

    private JPanel setupNPanel() 
     { 
     north = new JButton("Move to North"); 

     JPanel northP = new JPanel(); 

     getContentPane().add(north); 
     north.addActionListener(this); 

     return northP; 
     } 

    private JPanel setupNWPanel() 
     { 
     northWest = new JButton("Move to North West"); 

     JPanel northWP = new JPanel(); 

     getContentPane().add(northWest); 
     west.addActionListener(this); 

     return northWP; 
     } 

    private JPanel setupWPanel() 
     { 

      west = new JButton("Move to West"); 

      JPanel westP = new JPanel(); 

      getContentPane().add(west); 
      west.addActionListener(this); 

      return westP; 
     } 

    private JPanel setupCPanel() 
     { 
      center = new JButton("Move to Center"); 

      JPanel centerP = new JPanel(); 

      getContentPane().add(center); 
      center.addActionListener(this); 

      return centerP; 
     } 

    private JPanel setupEPanel() 
     { 
      east = new JButton("Move to East"); 

      JPanel eastP = new JPanel(); 

      getContentPane().add(east); 
      east.addActionListener(this); 

      return eastP; 

     } 

    private JPanel setupSEPanel() 
     { 
      southEast = new JButton("Move to South East"); 

      JPanel southEP = new JPanel(); 

      getContentPane().add(southEast); 
      east.addActionListener(this); 

      return southEP; 
     } 

    private JPanel setupSPanel() 
     { 
      south = new JButton("Move to South"); 

      JPanel southP = new JPanel(); 

      getContentPane().add(south); 
      south.addActionListener(this); 

      return southP; 
     } 

    private JPanel setupSWPanel() 
     { 
      southWest = new JButton("Move to South West"); 

      JPanel southWP = new JPanel(); 

      getContentPane().add(southWest); 
      west.addActionListener(this); 

      return southWP; 
     } 

    public void actionPerformed(ActionEvent e) 
    { 
     screenHeight = Toolkit.getDefaultToolkit().getScreenSize().height; 
     screenWidth = Toolkit.getDefaultToolkit().getScreenSize().width; 


     if (e.getSource() == north) 
     { 
      this.setLocation((screenWidth - width)/ 2, 0); 
     } 
     else if (e.getSource() == south) 
     { 
      this.setLocation((screenWidth - width)/ 2, (screenHeight-height)- 30); 
     } 
     else if (e.getSource() == east) 
     { 
      this.setLocation(screenWidth - width , (screenHeight-height)/2); 
     } 
     else if (e.getSource() == west) 
     { 
      this.setLocation(0, (screenHeight-height)/2); 
     } 
     else if (e.getSource() == center) 
     { 
      this.setLocation((screenWidth-width)/2, (screenHeight - height)/2); 
     } 
    } 
} 
+1

看來你需要一個像[這]排列(http://stackoverflow.com/questions/10861852/add-a-complex-image-in-the-panel-with-buttons-圍繞它在一個定製用戶/ 10862262#10862262)(用'GridLayout'完成)。 – 2013-02-18 11:34:32

+1

首先,您要添加按鈕而不是面板,其次您可能有興趣查看網格佈局以便在框架中排列面板。 – 2013-02-18 11:34:50

回答

6
JFrame frame = new JFrame ("Compass"); 
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
frame.getContentPane().setLayout(new GridLayout(3, 3)); 
frame.add (new JButton ("NW")); 
frame.add (new JButton ("N")); 
frame.add (new JButton ("NE")); 
frame.add (new JButton ("W")); 
frame.add (new JButton (" ")); 
frame.add (new JButton ("E")); 
frame.add (new JButton ("SW")); 
frame.add (new JButton ("S")); 
frame.add (new JButton ("SE")); 
frame.pack(); 
frame.setVisible(true);