2017-09-04 42 views
-1

,以創造在JFrame中多Jbuttons中我寫了這個代碼在一個JFrame創建Jbuttons中我已經創建的每一次點擊該按鈕,我發現與創建只有一個按鈕我試圖使用循環

String m=profile.jTextField6.getText(); 
    int M = Integer.parseInt(m); 

    تنشيفT f1 = new تنشيفT(); 
    f1. setVisible(true); 
    f1.setDefaultCloseOperation(DISPOSE_ON_CLOSE); 
    for (int i = 0; i < M; i++){ 
     JButton btnBoard = new JButton("month "+ +i); 
     f1.add(btnBoard); 
     btnBoard.setSize(500, 100); 
     btnBoard.setLocation((50+(15*M)), (100+(15*M))); 


    } 

月內0內 請幫我;

+0

歡迎堆棧溢出!它看起來像你需要學習使用調試器。請幫助一些[互補調試技術](https://ericlippert.com/2014/03/05/how-to-debug-small-programs/)。如果您之後仍然遇到問題,請隨時回答一個更具體的問題。 –

+0

我會在這裏猜測(對於我們來說,不必猜測發佈有效的[mcve])。 1)你創建了一個'JFrame'(我不知道'تنشيفT'是什麼意思或者這個類是怎麼樣的,但我猜它是一個'JFrame',所以它默認有一個'BorderLayout',所以這個問題是[重複](https://stackoverflow.com/questions/13881444/only-one-component-shows-up-in-jframe)。您需要學習使用[佈局管理器](https:// docs.oracle.com/javase/tutorial/uiswing/layout/visual.html)2)在向組件添加組件之前,您正在調用'setVisible(true)',這應該是程序中的最後一行 – Frakcool

+0

可能的重複[只有一個組件出現在JFrame中](https://stackoverflow.com/questions/13881444/only-one-component-shows-up-in-jframe) – Frakcool

回答

0

感謝您的幫助,代碼是確定它的佈局管理器,在那裏我是願意出口的按鈕,當我將其設置爲佈局框它創造了整個按鈕

if (profile.jComboBox2.getSelectedItem().equals("تخسيس")){ 
    تخسيسT f2 = new تخسيسT(); 
    String m=profile.jTextField6.getText(); 
    int M = Integer.parseInt(m); 
    for (int i = 1; i <= M; i++){ 

     JButton btn = new JButton("month "+ +i); 

     f2.add(btn); 
     btn.setSize(500, 100); 
     btn.setLocation(0, 0); 
     f2.setSize(85*M, 150); 
    } 
    f2. setVisible(true); 
    f2.setDefaultCloseOperation(DISPOSE_ON_CLOSE); 

    }