2014-03-27 29 views
0

你好我試圖讓我在哪裏試圖使JButton的左下方我沒有下面的代碼我不知道我錯了,我的代碼備註工作如何設置流量佈局?

這裏Java桌面應用程序是我的代碼

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

new complete code 

    public class ApplicationCloseExample 
    { 
     private JButton[] buttons; 
     private void displayGUI() 
     { 
      final JFrame frame = new JFrame("Application Close Example"); 





      JPanel bottomPanel = new JPanel(); 
      bottomPanel.setLayout(new FlowLayout(FlowLayout.LEFT, 5, 5)); 
       for (int i = 5; i < 8; i++) { 
       buttons[i] = new JButton(Integer.toString(i)); 
        bottomPanel.add(buttons[i]); 
       } 

      // JButton button = new JButton("Comment"); 
      // bottomPanel.add(button); 

     // frame.getContentPane().add(contentPane, BorderLayout.CENTER); 
      frame.getContentPane().add(bottomPanel, BorderLayout.SOUTH); 

      frame.pack(); 
      frame.setVisible(true); 
     } 

     public static void main(String... args) 
     { 
      SwingUtilities.invokeLater(new Runnable() 
      { 
       public void run() 
       { 
        new ApplicationCloseExample().displayGUI(); 
       } 
      }); 
     } 
    } 

我該如何做到這一點

+0

究竟是不是你的代碼工作?我唯一能猜到的是按鈕數組沒有正確初始化。 – Jack

+0

拋出異常空指針 – user3456343

+0

和我initilised數組像這個私人jButton []按鈕; – user3456343

回答

2

我打算假設空指針異常與您的按鈕數組有關。檢查您是否已正確初始化它。

private JButton[] buttons = new JButton[8]; 

我複製你的代碼到測試項目,並進行一些修改後,運行它:

public static void main(String[] args) { 

    final JFrame frame = new JFrame("Application Close Example"); 

    JPanel bottomPanel = new JPanel(); 
    bottomPanel.setLayout(new FlowLayout(FlowLayout.LEFT, 5, 5)); 
     for (int i = 5; i < 8; i++) { 
     buttons[i] = new JButton(Integer.toString(i)); 
      bottomPanel.add(buttons[i]); 
     } 

    frame.getContentPane().add(bottomPanel, BorderLayout.SOUTH); 

    frame.pack(); 
    frame.setVisible(true); 
} 

這產生對齊到窗口的左下角三個按鈕的框架。

+0

我上傳了我的完整代碼 – user3456343

+0

更新了我的答案。我相信這是你的JButton陣列。 – Jack

+0

感謝它的工作 – user3456343

0

有一個優雅的解決方案,我會給你,但也許它服務。使用WindowsBuilder並添加幾個按鈕,然後您看起來像放置代碼。因此,讓自己瞭解一個使用Flow的setLayout之後的模式。