2017-08-03 128 views
0

我打算設計一個包含一個外部JPanel的gui程序,其中包含許多具有文本和按鈕的重複JPanel。但是,我無法確定哪個佈局適合這個任務。我希望它是這樣的:如何選擇嵌套JPanel的最佳佈局?

enter image description here

我只是複製和粘貼,將你喜歡的圖片中看到以編程的復發第一的JPanel。 爲了得到這樣的結果,我應該使用哪種佈局?

+0

看起來外面板應該使用垂直的「BoxLayout」,內部的面板可以使用水平的「BoxLayout」或「GridLayout」。看看[教程](https://docs.oracle.com/javase/tutorial/uiswing/layout/visual.html)。要得到明確的答案,您需要指定調整行爲。 – user1803551

+0

@ user1803551感謝您的鏈接,但是您指的是「指定調整大小行爲」的含義 – gingerdd

+0

調整框架大小時,您希望如何在組件之間分配空間? – user1803551

回答

2

在我的頭上,它看起來是這樣的:

JScrollPane>JPanel (outerPane)>JPanel (innerPane [many])

在此基礎上,我們需要考慮其佈局管理器是outerPane要使用和innerPane小號...

爲了提供innerPane S之間的間距我會去GridLayout (rows, columns, hgap, vgap)像:

GridLayout(0, 1, 5, 5); 

現在每個innerPane我們可以去GridLayoutGridBagLayoutFlowLayout,讓我們看看會與每發生什麼:

  • 如果我們使用FlowLayout的組件將不會在「網格」或「表」之類的佈局,所以,這是一個沒有沒有。這是它會是什麼樣子:

    enter image description here

    Altough他們看起來像我們所需要的,我不知道如果每個標籤GOI NG隨着時間的推移至少調整時,它改變與否,但你可以嘗試...

    • 使用GridLayout將使我們的JButton s到取電池的整個空間,而不會好看( ),這裏是一個圖像與之前和調整的GUI後,顯示我的意思(裁剪到之後不使用大量的空間):

    enter image description here

    如果你的GUI不會調整你可以走這條路,如果可以的話,那你應該換個佈局。

    • GridBagLayout是我在這種情況下的最愛,因爲每個標籤會留在自己的欄和按鈕將無法填補所有可用空間,我們的GUI看起來更像你張貼的形象:

    enter image description here

    在上面的例子中,我使用的CustomBorder類以提供innerPane S和outerPane,同時還提供AA彩色邊框之間間距以及表示垂直JScrollPane送花兒給人秒。

    產生這些產出的代碼是:

    package sof; 
    
    import java.awt.Color; 
    import java.awt.Component; 
    import java.awt.Graphics; 
    import java.awt.Graphics2D; 
    import java.awt.GridBagConstraints; 
    import java.awt.GridBagLayout; 
    import java.awt.GridLayout; 
    import java.awt.Insets; 
    import java.awt.geom.Rectangle2D; 
    
    import javax.swing.BorderFactory; 
    import javax.swing.JButton; 
    import javax.swing.JFrame; 
    import javax.swing.JLabel; 
    import javax.swing.JPanel; 
    import javax.swing.JScrollPane; 
    import javax.swing.SwingUtilities; 
    import javax.swing.border.AbstractBorder; 
    
    public class NestedPanels { 
        private JFrame frame; 
        private JPanel outerPane; 
        private JPanel innerPane; 
        private GridBagConstraints gbc; 
        private JScrollPane scrollPane; 
    
        public static void main(String[] args) { 
         SwingUtilities.invokeLater(new NestedPanels()::createAndShowGui); 
        } 
    
        private void createAndShowGui() { 
         frame = new JFrame(getClass().getSimpleName()); 
    
         outerPane = new JPanel(); 
         outerPane.setLayout(new GridLayout(0, 1, 5, 5)); 
    
         for (int i = 0; i < 5; i++) { 
          innerPane = new JPanel(); 
          innerPane.setLayout(new GridBagLayout()); 
    
          gbc = new GridBagConstraints(); 
    
          gbc.gridx = 0; 
          gbc.gridy = 0; 
          gbc.fill = GridBagConstraints.BOTH; 
          gbc.insets = new Insets(10, 10, 10, 10); 
    
          innerPane.add(new JLabel("Recurring JLabel1"), gbc); 
    
          gbc.gridx++; 
          innerPane.add(new JLabel("Recurring JLabel2"), gbc); 
          gbc.gridx++; 
          innerPane.add(new JLabel("Recurring JLabel3"), gbc); 
          gbc.gridx++; 
          innerPane.add(new JButton("Recurring JButton1"), gbc); 
    
          innerPane.setBorder(BorderFactory.createLineBorder(Color.BLACK)); 
    
          outerPane.add(innerPane); 
         } 
         outerPane.setBorder(new CustomBorder(5, Color.BLACK)); 
    
         scrollPane = new JScrollPane(outerPane, JScrollPane.VERTICAL_SCROLLBAR_ALWAYS, JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED); 
    
         frame.add(scrollPane); 
         frame.pack(); 
         frame.setVisible(true); 
         frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
        } 
    
        @SuppressWarnings("serial") 
        class CustomBorder extends AbstractBorder { 
         private int gap; 
         private Color color; 
         public CustomBorder(int gap, Color color) { 
          this.gap = gap; 
          this.color = color; 
         } 
    
         @Override 
         public Insets getBorderInsets(Component c) { 
          return new Insets(gap, gap, gap, gap); 
         } 
    
         @Override 
         public void paintBorder(Component c, Graphics g, int x, int y, int width, int height) { 
          super.paintBorder(c, g, x, y, width, height); 
          Graphics2D g2d = (Graphics2D) g; 
          g2d.setColor(color); 
          g2d.draw(new Rectangle2D.Double(x, y, width - 1, height - 1)); 
         } 
        } 
    } 
    

播放與邊框樣式以獲得所需的一個,我畫與GUI上-1像素的邊界,如果我不」 t將其只會顯示左側和頂部邊框...

  • 另一種選擇是使用JTable,但我留給你