2016-01-04 56 views
0

我想與下一行中的一行,5 3個的按鈕和在最後一行3個按鈕顯示的按鈕的這種二維陣列的,但它們都出來在一行上- 我究竟做錯了什麼?顯示自定義二維陣列的

public class GUICustombuttonstwodimarray extends JFrame { 
    static final String title = "Custom Buttons"; 
    int row, col; 
    JPanel panel; 

    JButton[][] jbut = { { new JButton("0"), new JButton("1"), new JButton("2") }, 
     { new JButton("3"), new JButton("4"), new JButton("5"), new JButton("6"), new JButton("7") }, 
     { new JButton("8"), new JButton("9") }, { new JButton("10") } 
    }; 

    public GUICustombuttonstwodimarray(String title) { 
     super(title); 
     this.setDefaultCloseOperation(EXIT_ON_CLOSE); 
     this.setSize(500, 500); 
     this.setVisible(true); 

     panel = new JPanel(); 

     for (row = 0; row < jbut.length; row++) { 
      for (col = 0; col < jbut[row].length; col++) { 
       panel.add(jbut[row][col]); 
      } 
     } 
     getContentPane().add(panel, BorderLayout.CENTER); 
    } 

    public static void main(String[] args) { 
     GUICustombuttonstwodimarray g = new GUICustombuttonstwodimarray(title); 
    } 
} 
+0

退房https://docs.oracle.com/javase/tutorial/uiswing/layout/visual.html,尤其是GridBagLayout的可能是有趣的。 – LordAnomander

+0

[如何使用表(http://docs.oracle.com/javase/tutorial/uiswing/components/table.html) – MadProgrammer

+0

這tutorialspoint文章舉了一個例子,這是非常相似,你想達到什麼樣的: http://www.tutorialspoint.com/awt/awt_gridlayout.htm –

回答

2

有可能實現的幾種方法,但最簡單的可能只是只使用一個GridBagLayout,例如

GridBagLayout

import java.awt.BorderLayout; 
import java.awt.Dimension; 
import java.awt.EventQueue; 
import java.awt.Graphics; 
import java.awt.Graphics2D; 
import java.awt.GridBagConstraints; 
import java.awt.GridBagLayout; 
import javax.swing.JButton; 
import javax.swing.JFrame; 
import static javax.swing.JFrame.EXIT_ON_CLOSE; 
import javax.swing.JPanel; 
import javax.swing.UIManager; 
import javax.swing.UnsupportedLookAndFeelException; 

public class GUICustombuttonstwodimarray extends JPanel { 

    static final String TITLE = "Custom Buttons"; 
    int row, col; 

    JButton[][] jbut = {{new JButton("0"), new JButton("1"), new JButton("2")}, 
    {new JButton("3"), new JButton("4"), new JButton("5"), new JButton("6"), new JButton("7")}, 
    {new JButton("8"), new JButton("9")}, {new JButton("10")} 

    }; 

    public GUICustombuttonstwodimarray() { 

     setLayout(new GridBagLayout()); 
     GridBagConstraints gbc = new GridBagConstraints(); 
     gbc.gridx = 0; 
     gbc.gridy = 0; 

     for (row = 0; row < jbut.length; row++) { 
      for (col = 0; col < jbut[row].length; col++) { 
       add(jbut[row][col], gbc); 

       gbc.gridx++; 
       if (gbc.gridy == 0 && gbc.gridx >= 3) { 
        gbc.gridy++; 
        gbc.gridx = 0; 
       } else if (gbc.gridy == 1 && gbc.gridx >= 5) { 
        gbc.gridy++; 
        gbc.gridx = 0; 
       } 

      } 

     } 
    } 

    public static void main(String[] args) { 
     EventQueue.invokeLater(new Runnable() { 
      @Override 
      public void run() { 
       try { 
        UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName()); 
       } catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) { 
        ex.printStackTrace(); 
       } 

       JFrame frame = new JFrame(TITLE); 
       frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
       frame.add(new GUICustombuttonstwodimarray()); 
       frame.pack(); 
       frame.setLocationRelativeTo(null); 
       frame.setVisible(true); 
      } 
     }); 
    } 

    public class TestPane extends JPanel { 

     public TestPane() { 
     } 

     @Override 
     public Dimension getPreferredSize() { 
      return new Dimension(200, 200); 
     } 

     protected void paintComponent(Graphics g) { 
      super.paintComponent(g); 
      Graphics2D g2d = (Graphics2D) g.create(); 
      g2d.dispose(); 
     } 

    } 
} 

看看How to Use GridLayout瞭解更多詳情