2013-06-23 44 views
2

我的代碼以前工作,但現在一個JButton佔用整個JFrame,任何幫助將不勝感激!爲什麼我的按鈕佔用整個JFrame?

我使用的FlowLayout稱爲FL和一個叫肯

我有十個按鈕調用(oneButton,twoButton,threeButton,等..)

我的代碼類:

import java.awt.ComponentOrientation; 
import java.awt.Dimension; 
import java.awt.FlowLayout; 


import javax.swing.JButton; 
import javax.swing.JFrame; 



public class Ken { 

public static void frame(){ 
    JFrame frame = new JFrame("Pow"); 
    frame.setSize(500, 600); 
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
    frame.setVisible(true); 
    frame.setResizable(false); 
    FlowLayout fl = new FlowLayout(0, 50, 40); 


    JButton oneButton = new JButton("   1   "); 
    oneButton.setPreferredSize(new Dimension(100, 90)); 
    frame.add(oneButton); 
    JButton twoButton = new JButton("   2   "); 
    twoButton.setPreferredSize(new Dimension(100, 90)); 
    frame.add(twoButton); 
    JButton threeButton = new JButton("   3   "); 
    threeButton.setPreferredSize(new Dimension(100, 90)); 
    frame.add(threeButton); 
    JButton fourButton = new JButton("   4   "); 
    fourButton.setPreferredSize(new Dimension(100, 90)); 
    frame.add(fourButton); 
    JButton fiveButton = new JButton("   5   "); 
    fiveButton.setPreferredSize(new Dimension(100, 90)); 
    frame.add(fiveButton); 
    JButton sixButton = new JButton("   6   "); 
    sixButton.setPreferredSize(new Dimension(100, 90)); 
    frame.add(sixButton); 
    JButton sevenButton = new JButton("   7   "); 
    sevenButton.setPreferredSize(new Dimension(100, 90)); 
    frame.add(sevenButton); 
    JButton eightButton = new JButton("   8   "); 
    eightButton.setPreferredSize(new Dimension(100, 90)); 
    frame.add(eightButton); 
    JButton nineButton = new JButton("   9   "); 
    nineButton.setPreferredSize(new Dimension(100, 90)); 
    frame.add(nineButton); 
    JButton zeroButton = new JButton("                  
            0        "); 
    zeroButton.setPreferredSize(new Dimension(400, 90)); 
    frame.add(zeroButton); 
    frame.setComponentOrientation(
      ComponentOrientation.LEFT_TO_RIGHT); 
} 



public static void main(String[] args){ 
    frame(); 
} 
} 

任何我可以嘗試?

回答

4

我使用的FlowLayout稱爲FL ...

你是不是在實際上使用的FlowLayout,因爲你永遠不會調用setLayout(...)的容器上。相反,您的Frame的contentPane使用contentPanes的默認佈局BorderLayout,這將導致以默認方式添加的最後一個組件填充容器的BorderLayout.CENTER位置。

那麼一個解決方案是使用你的佈局:

FlowLayout fl = new FlowLayout(0, 50, 40);  
frame.getContentPane().setLayout(fl); 

不過話說回來,你可能會最好不要設置你的組件的首選尺寸,而是使用嵌套的容器的組合各自使用其自己的佈局,以實現一個令人愉快和複雜的圖形用戶界面,將更容易維護和擴展。

或者,您可以使用更復雜的佈局,如GridBagLayout。例如:您void frame()方法

import java.awt.Component; 
import java.awt.Font; 
import java.awt.GridBagConstraints; 
import java.awt.GridBagLayout; 
import java.awt.Insets; 

import javax.swing.*; 

public class Ken2 { 
    private static final String[][] NUMBERS = { 
     {"1", "2", "3"}, 
     {"4", "5", "6"}, 
     {"7", "8", "9"}, 
     {"0"} 
    }; 
    private static final float BUTTON_FONT_PTS = 45f; 
    private static final int INSETS = 20; 
    private static final Insets BUTTON_INSETS = new Insets(INSETS, INSETS, 
      INSETS, INSETS); 
    private static final int IPAD = 20; 
    private JPanel mainPanel = new JPanel(); 

    public Ken2() { 
     mainPanel.setLayout(new GridBagLayout()); 
     for (int row = 0; row < NUMBERS.length; row++) { 
     addRowToPanel(row, NUMBERS[row]); 
     } 
    } 

    private void addRowToPanel(int row, String[] numbersRow) { 
     int columns = numbersRow.length; 
     for (int col = 0; col < numbersRow.length; col++) { 
     addNumberButton(row, col, columns, numbersRow[col]); 
     } 
    } 

    private void addNumberButton(int row, int col, int columns, 
      String numberText) { 
     JButton button = new JButton(numberText); 
     button.setFont(button.getFont().deriveFont(Font.BOLD, BUTTON_FONT_PTS)); 
     GridBagConstraints gbc = new GridBagConstraints(); 
     gbc.gridx = col; 
     gbc.gridy = row; 
     gbc.gridheight = 1; 
     gbc.gridwidth = 3/columns; 
     gbc.weightx = 1.0; 
     gbc.weighty = 1.0; 
     gbc.anchor = GridBagConstraints.CENTER; 
     gbc.fill = GridBagConstraints.BOTH; 
     gbc.insets = BUTTON_INSETS; 
     gbc.ipadx = IPAD; 
     gbc.ipady = IPAD; 

     mainPanel.add(button, gbc); 
    } 

    private static void createAndShowGui() { 
     Ken2 ken = new Ken2(); 

     JFrame frame = new JFrame("Ken2"); 
     frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
     frame.getContentPane().add(ken.getMainPanel()); 
     frame.pack(); 
     frame.setLocationByPlatform(true); 
     frame.setVisible(true); 
    } 

    private Component getMainPanel() { 
     return mainPanel; 
    } 

    public static void main(String[] args) { 
     SwingUtilities.invokeLater(new Runnable() { 
     public void run() { 
      createAndShowGui(); 
     } 
     }); 
    } 
} 
+0

非常感謝你 – mmdfasd

+0

@ user2498848:不客氣。 –