2012-04-04 33 views
0

我想讓我的界面動態生成一個定製的按鈕,當我點擊一個按鈕。我搜索了幾個這樣的答案,但不知何故它不起作用。我目前的代碼是否有任何錯誤?如何動態添加新的自定義按鈕?

public class MainWindow { 

private JFrame frame; 
private JPanel panel; 
private JPanel panel_1; 
private JPanel panel_2; 
private JSplitPane splitPane; 
private JButton btnSearch; 

/** 
* Launch the application. 
*/ 
public static void main(String[] args) { 
    EventQueue.invokeLater(new Runnable() { 
     public void run() { 
      try { 
       MainWindow window = new MainWindow(); 
       window.frame.setVisible(true); 
      } catch (Exception e) { 
       e.printStackTrace(); 
      } 
     } 
    }); 
} 

/** 
* Create the application. 
*/ 
public MainWindow() { 
    initialize(); 
} 

/** 
* Initialize the contents of the frame. 
*/ 
private void initialize() { 
    frame = new JFrame(); 
    frame.setBounds(100, 100, 645, 438); 
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 

    panel = new JPanel(); 
    frame.getContentPane().add(panel, BorderLayout.CENTER); 
    panel.setLayout(new BoxLayout(panel, BoxLayout.X_AXIS)); 

    splitPane = new JSplitPane(); 
    panel.add(splitPane); 

    panel_1 = new JPanel(); 
    splitPane.setLeftComponent(panel_1); 

    btnSearch = new JButton("Search"); 

    GridBagConstraints gbc_btnSearch = new GridBagConstraints(); 
    gbc_btnSearch.gridx = 0; 
    gbc_btnSearch.gridy = 10; 
    panel_1.add(btnSearch, gbc_btnSearch); 

    panel_2 = new JPanel(); 
    splitPane.setRightComponent(panel_2); 

    btnSearch.addMouseListener(new MouseAdapter() { 
     @Override 
     public void mouseClicked(MouseEvent e) { 
      addButton(); 
     } 
    }); 
} 
protected void addButton() { 
    MyButton hahaButton = new MyButton("haha"); 
    panel_2.add(hahaButton); 
    panel_2.add(new JButton()); 
    panel_2.revalidate(); 
    panel_2.repaint(); 
} 

這是myButton的定義:

public class MyButton extends JButton { 

private static final long serialVersionUID = 1L; 

private Color circleColor = Color.BLACK; 

public MyButton(String label) { 
    super(label); 
} 

@Override 
protected void paintComponent(Graphics g) { 
    super.paintComponent(g); 

    Dimension originalSize = super.getPreferredSize(); 
    int gap = (int) (originalSize.height * 0.2); 
    int x = originalSize.width + gap; 
    int y = gap; 
    int diameter = originalSize.height - (gap * 2); 

    g.setColor(circleColor); 
    g.fillOval(x, y, diameter, diameter); 
} 

@Override 
public Dimension getPreferredSize() { 
    Dimension size = super.getPreferredSize(); 
    size.width += size.height; 
    return size; 
} 

}

+1

我剛剛吐在我的嘴裏。 – mre 2012-04-04 19:35:04

+0

對不起...但我不熟悉swing編程,請問如何解決這個問題請提供一些建議? – faz 2012-04-04 19:44:03

回答

1

我只是想你的源代碼,它按預期工作:每次我克利克在左側的搜索按鈕拆分面板,2個按鈕被添加到面板右側的面板(一個帶黑色填充圓圈和一個沒有標籤的按鈕)。

什麼不適合你?我在Mac OSX上使用的是Java 1.6,但是這也適用於其他平臺上的早期版本......

+0

我剛剛發現....我的壞......我誤解了WindowsBuilder中的「測試/預覽」按鈕,它正在工作......非常感謝:) – faz 2012-04-04 19:47:17

1

正如Dieter Rehbein指出的那樣,您已經編譯並運行的代碼。

然而,這是相當草率和令人費解的,就好像你複製和粘貼不同的來源。

我花了幾分鐘,清理了一些,希望它有幫助。

 

    public class MainWindow 
    { 
     public static void main(String[] args) 
     { 
      new MainWindow(); 
     } 

     public MainWindow() 
     { 
      // Create the split pane 
      JSplitPane jSplitPane = new JSplitPane(); 

      final JPanel leftPanel = new JPanel(); 
      final JPanel rightPanel = new JPanel(); 

      jSplitPane.setLeftComponent(leftPanel); 
      jSplitPane.setRightComponent(rightPanel); 

      // Create the button 
      JButton jButton = new JButton("Generate"); 
      leftPanel.add(jButton); 

      jButton.addMouseListener(new MouseAdapter() 
      { 
       @Override 
       public void mouseClicked(MouseEvent e) 
       { 
        addButtons(rightPanel); 
       } 
      }); 

      // Create the panel 
      JPanel jPanel = new JPanel(); 

      jPanel.setLayout(new BoxLayout(jPanel , BoxLayout.X_AXIS)); 
      jPanel.add(jSplitPane); 

      // Create the frame 
      JFrame jFrame = new JFrame(); 

      jFrame.setBounds(100 , 100 , 645 , 438); 
      jFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
      jFrame.getContentPane().add(jPanel , BorderLayout.CENTER); 

      // Show the frame 
      jFrame.setVisible(true); 
     } 

     private void addButtons(JPanel jPanel) 
     { 
      addButton(jPanel , "Default" , null); 
      addButton(jPanel , "Red" , Color.RED); 
      addButton(jPanel , "Yellow" , Color.YELLOW); 
      addButton(jPanel , "Blue" , Color.BLUE); 
      addButton(jPanel , "Green" , Color.GREEN); 
     } 

     protected void addButton(JPanel jPanel , String label , Color color) 
     { 
      jPanel.add(new MyButton(label , color)); 
      jPanel.revalidate(); 
      jPanel.repaint(); 
     } 

     public class MyButton extends JButton 
     { 
      private static final long serialVersionUID = 1L; 

      private Color color = null; 

      public MyButton(String label , Color color) 
      { 
       super(label); 
       this.color = color; 
      } 

      @Override 
      protected void paintComponent(Graphics graphics) 
      { 
       super.paintComponent(graphics); 

       if (color != null) 
       { 
        Dimension dimension = super.getPreferredSize(); 

        int gap = (int) (dimension.height * 0.2); 
        int diameter = dimension.height - (gap * 2); 

        graphics.setColor(color); 
        graphics.fillOval(dimension.width + gap , gap , diameter , diameter); 
       } 
      } 

      @Override 
      public Dimension getPreferredSize() 
      { 
       Dimension size = super.getPreferredSize(); 
       size.width += size.height; 
       return size; 
      } 
     } 
    }