GridLayout / BorderLayout不工作

时间:2014-12-01 15:37:17

标签: java swing layout-manager grid-layout border-layout

我需要制作一个最终看起来像这样的程序:

到目前为止,我的代码已发布,我似乎无法理解如何使其外观与我提供的图像完全相同。我只是不知道我还能做些什么,我已经尝试过寻找解决方案,但是我无法将这些信息与这些信息结合起来以使它们能够协同工作。

所以,我希望其他人可以在这里帮助我,非常感谢! :D

import java.awt.*;     // Needed for BorderLayout class  
import javax.swing.*;  // Needed for Swing classes  

/** 
   This class demonstrates how JPanels can be nested 
   inside each region of a content pane governed by 
   a BorderLayout manager. 
*/  

public class BorderPanelWindow extends JFrame  
{  
   /** 
      Constructor 
   */  

   public BorderPanelWindow()  
   {  
      // Set the title bar text.  
      setTitle("Border Layout");  

      // Specify an action for the close button.  
      setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);  

      // Add a BorderLayout manager to the content pane.  
      setLayout(new BorderLayout());  

      // Create five panels.  
      JPanel panel1 = new JPanel();  
      JPanel panel2 = new JPanel();  
      JPanel panel3 = new JPanel();  
      JPanel panel4 = new JPanel();  
      panel4.setLayout(new GridLayout(3,3));  
      JPanel panel5 = new JPanel();  
      panel5.setLayout(new GridLayout(3,3));  
      JPanel panel6 = new JPanel();  
      JPanel panel7 = new JPanel();  
      JPanel panel8 = new JPanel();  
      JPanel panel9 = new JPanel();  
      JPanel panel10 = new JPanel();  


      // Create five buttons.  
      JButton button1 = new JButton("Button 1");  
      JButton button2 = new JButton("Button 2");  
      JButton button3 = new JButton("Button 3");  
      JButton button4 = new JButton("Button 4");  
      JButton button5 = new JButton("Button 5");  
      JButton button6 = new JButton("Button 6");  
      JButton button7 = new JButton("Button 7");  
      JButton button8 = new JButton("Button 8");  
      JButton button9 = new JButton("Button 9");  
      JButton button10 = new JButton("Button 10");  

      //Add buttons to panel4  
      panel4.add(button1);  
      panel4.add(button2);  
      panel4.add(button3);  
      panel4.add(button4);  
      panel4.add(button5);  
      panel4.add(button6);  


      // Add the buttons to the panels.  
      panel7.add(button7);  
      panel8.add(button8);  
      panel9.add(button9);  
      panel10.add(button10);  

      // Add the five panels to the content pane.  
      add(panel7, BorderLayout.NORTH);  
      add(panel8, BorderLayout.SOUTH);  
      add(panel9, BorderLayout.EAST);  
      add(panel4, BorderLayout.WEST);  
      //add(panel10, BorderLayout.WEST);  
      add(panel5, BorderLayout.CENTER);  

      // Pack and display the window.  
      pack();  
      setVisible(true);  
   }  

   /** 
      The main method creates an instance of the 
      BorderPanelWindow class, causing it to display 
      its window. 
   */  

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

更新

所以这就是我到目前为止我认为答案建议我做的事情。就像我说的那样,当我在那里有按钮10时,我看不到我的面板4,我确信这是我只是没有看到但仍然存在的东西。

import java.awt.*;     // Needed for BorderLayout class
import javax.swing.*;  // Needed for Swing classes

/**
   This class demonstrates how JPanels can be nested
   inside each region of a content pane governed by
   a BorderLayout manager.
*/

public class BorderPanelWindow extends JFrame
{
   /**
      Constructor
   */

   public BorderPanelWindow()
   {
      // Set the title bar text.
      setTitle("Border Layout");

      // Specify an action for the close button.
      setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

      // Add a BorderLayout manager to the content pane.
      setLayout(new BorderLayout());

      // Create five panels.
      JPanel panel1 = new JPanel();
      JPanel panel2 = new JPanel();
      JPanel panel3 = new JPanel();
      JPanel panel4 = new JPanel();
      panel4.setLayout(new GridLayout(3,3));
      JPanel panel5 = new JPanel();
      panel5.setLayout(new GridLayout(3,3));
      JPanel panel6 = new JPanel();
      JPanel panel7 = new JPanel();
      JPanel panel8 = new JPanel();
      JPanel panel9 = new JPanel();
      JPanel panel10 = new JPanel();


      // Create five buttons.
      JButton button1 = new JButton("Button 1");
      JButton button2 = new JButton("Button 2");
      JButton button3 = new JButton("Button 3");
      JButton button4 = new JButton("Button 4");
      JButton button5 = new JButton("Button 5");
      JButton button6 = new JButton("Button 6");
      JButton button7 = new JButton("Button 7");
      JButton button8 = new JButton("Button 8");
      JButton button9 = new JButton("Button 9");
      JButton button10 = new JButton("Button 10");
      JButton button11 = new JButton("Button 11");
      JButton button12 = new JButton("Button 12");
      JButton button13 = new JButton("Button 13");
      JButton button14 = new JButton("Button 14");
      JButton button15 = new JButton("Button 15");
      JButton button16 = new JButton("Button 16");
      JButton button17 = new JButton("Button 17");
      JButton button18 = new JButton("Button 18");

      //Add buttons to panel4
      panel4.add(button1);
      panel4.add(button2);
      panel4.add(button3);
      panel4.add(button4);
      panel4.add(button5);
      panel4.add(button6);

      //Add buttons to panel5
      panel5.add(button11);
      panel5.add(button12);
      panel5.add(button13);
      panel5.add(button14);
      panel5.add(button15);
      panel5.add(button16);
      panel5.add(button17);
      panel5.add(button18);


      // Add the buttons to the panels.
      panel7.add(button7);
      panel8.add(button8);
      panel9.add(button9);
      panel10.add(button10);

      // Add the five panels to the content pane.
      add(panel7, BorderLayout.NORTH);
      add(panel8, BorderLayout.SOUTH);
      add(panel9, BorderLayout.EAST);
      add(panel4, BorderLayout.WEST);
      add(panel10, BorderLayout.WEST);
      add(panel5, BorderLayout.CENTER);

      // Pack and display the window.
      pack();
      setVisible(true);
   }

   /**
      The main method creates an instance of the
      BorderPanelWindow class, causing it to display
      its window.
   */

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

UPDATE2

所以现在我把它打磨了一下并尝试做你说的但现在它看起来更糟,所以我必须要做的事情我不会完全大声笑。希望你能看出我的错误。

import java.awt.*;     // Needed for BorderLayout class
import javax.swing.*;  // Needed for Swing classes

/**
   This class demonstrates how JPanels can be nested
   inside each region of a content pane governed by
   a BorderLayout manager.
*/

public class BorderPanelWindow extends JFrame
{
   /**
      Constructor
   */

   public BorderPanelWindow()
   {
      // Set the title bar text.
      setTitle("Border Layout");

      // Specify an action for the close button.
      setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

      // Add a BorderLayout manager to the content pane.
      setLayout(new BorderLayout());

      // Create five panels.
      JPanel panel1 = new JPanel();
      panel1.setLayout(new GridLayout(3,3));
      JPanel panel2 = new JPanel();
      panel2.setLayout(new GridLayout(3,3));
      JPanel panel3 = new JPanel();
      panel3.setLayout(new BorderLayout());
      JPanel panel4 = new JPanel();
      panel4.setLayout(new GridLayout(3,3));


      // Create five buttons.
      JButton button1 = new JButton("Button 1");
      JButton button2 = new JButton("Button 2");
      JButton button3 = new JButton("Button 3");
      JButton button4 = new JButton("Button 4");
      JButton button5 = new JButton("Button 5");
      JButton button6 = new JButton("Button 6");
      JButton button7 = new JButton("Button 7");
      JButton button8 = new JButton("Button 8");
      JButton button9 = new JButton("Button 9");
      JButton button10 = new JButton("Button 10");
      JButton button11 = new JButton("Button 11");
      JButton button12 = new JButton("Button 12");
      JButton button13 = new JButton("Button 13");
      JButton button14 = new JButton("Button 14");
      JButton button15 = new JButton("Button 15");
      JButton button16 = new JButton("Button 16");
      JButton button17 = new JButton("Button 17");
      JButton button18 = new JButton("Button 18");

      //Add buttons to panel1
      panel1.add(button1);
      panel1.add(button2);
      panel1.add(button3);
      panel1.add(button4);
      panel1.add(button5);
      panel1.add(button6);

      //Add buttons to panel2



      //Add the buttons to panel3
      panel3.add(button7);
      panel3.add(button8);
      panel3.add(button9);
      panel3.add(button10);

      //Add the buttons to panel4
      panel4.add(button11);
      panel4.add(button12);
      panel4.add(button13);
      panel4.add(button14);
      panel4.add(button15);
      panel4.add(button16);
      panel4.add(button17);
      panel4.add(button18);

      // Add the five panels to the content pane.
      add(panel1, BorderLayout.WEST);
      add(panel2, BorderLayout.EAST);
      add(panel3, BorderLayout.EAST);
      add(panel4, BorderLayout.CENTER);

      // Pack and display the window.
      pack();
      setVisible(true);
   }

   /**
      The main method creates an instance of the
      BorderPanelWindow class, causing it to display
      its window.
   */

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

UPDATE3

好的,我现在更进一步,现在最大的问题是按钮11-18没有出现在7-10按钮的中间。

import java.awt.*;     // Needed for BorderLayout class
import javax.swing.*;  // Needed for Swing classes

/**
   This class demonstrates how JPanels can be nested
   inside each region of a content pane governed by
   a BorderLayout manager.
*/

public class BorderPanelWindow extends JFrame
{
   /**
      Constructor
   */

   public BorderPanelWindow()
   {
      // Set the title bar text.
      setTitle("Border Layout");

      // Specify an action for the close button.
      setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

      // Add a BorderLayout manager to the content pane.
      setLayout(new BorderLayout());

      // Create five panels.
      JPanel panel1 = new JPanel();
      panel1.setLayout(new GridLayout(2,3));
      JPanel panel2 = new JPanel();
      panel2.setLayout(new GridLayout(4,2));
      JPanel panel3 = new JPanel();
      panel3.setLayout(new BorderLayout());
      JPanel panel4 = new JPanel();
      panel4.setLayout(new GridLayout(1,2));


      // Create five buttons.
      JButton button1 = new JButton("Button 1");
      JButton button2 = new JButton("Button 2");
      JButton button3 = new JButton("Button 3");
      JButton button4 = new JButton("Button 4");
      JButton button5 = new JButton("Button 5");
      JButton button6 = new JButton("Button 6");
      JButton button7 = new JButton("Button 7");
      JButton button8 = new JButton("Button 8");
      JButton button9 = new JButton("Button 9");
      JButton button10 = new JButton("Button 10");
      JButton button11 = new JButton("Button 11");
      JButton button12 = new JButton("Button 12");
      JButton button13 = new JButton("Button 13");
      JButton button14 = new JButton("Button 14");
      JButton button15 = new JButton("Button 15");
      JButton button16 = new JButton("Button 16");
      JButton button17 = new JButton("Button 17");
      JButton button18 = new JButton("Button 18");

      //Add buttons to panel1
      panel1.add(button1);
      panel1.add(button2);
      panel1.add(button3);
      panel1.add(button4);
      panel1.add(button5);
      panel1.add(button6);

      //Add buttons to panel2



      //Add the buttons to panel3
      panel3.add(button7, BorderLayout.NORTH);
      panel3.add(button8, BorderLayout.SOUTH);
      panel3.add(button9, BorderLayout.EAST);
      panel3.add(button10, BorderLayout.WEST);

      //Add the buttons to panel4
      panel4.add(button11);
      panel4.add(button12);
      panel4.add(button13);
      panel4.add(button14);
      panel4.add(button15);
      panel4.add(button16);
      panel4.add(button17);
      panel4.add(button18);




      // Add the five panels to the content pane.
      add(panel1, BorderLayout.WEST);
      add(panel2, BorderLayout.CENTER);
      add(panel3, BorderLayout.EAST);
      add(panel4, BorderLayout.CENTER);



      // Pack and display the window.
      pack();
      setVisible(true);
   }

   /**
      The main method creates an instance of the
      BorderPanelWindow class, causing it to display
      its window.
   */

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

1 个答案:

答案 0 :(得分:3)

我使用3种网格布局和边框布局来实现这一点。 E.G。

    左/右部分的
  • GridLayout(每个部分是一个面板)
      按钮1-6 的
    1. GridLayout
    2. BorderLayout用于按钮7-10 + CENTER中的面板...
        按钮11-18的
      • GridLayout

更新

您的编辑3非常接近,请注意代码中的额外注释和更改以查看:

enter image description here

import java.awt.*;     // Needed for BorderLayout class
import javax.swing.*;  // Needed for Swing classes

/**
   This class demonstrates how JPanels can be nested
   inside each region of a content pane governed by
   a BorderLayout manager.
*/

public class BorderPanelWindow extends JFrame
{
   /**
      Constructor
   */

   public BorderPanelWindow()
   {
      // Set the title bar text.
      setTitle("Border Layout");

      // Specify an action for the close button.
      setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE); // better

      // Add a BorderLayout manager to the content pane.
      setLayout(new BorderLayout());

      // Create five panels.
      JPanel panel1 = new JPanel();
      panel1.setLayout(new GridLayout(2,3));
      JPanel panel2 = new JPanel();
      panel2.setLayout(new GridLayout(4,2));
      JPanel panel3 = new JPanel();
      panel3.setLayout(new BorderLayout());
      JPanel panel4 = new JPanel();
      // we need as many rows as needed (0) in 2 columns (2)
      panel4.setLayout(new GridLayout(0,2)); 


      // Create five buttons.
      JButton button1 = new JButton("Button 1");
      JButton button2 = new JButton("Button 2");
      JButton button3 = new JButton("Button 3");
      JButton button4 = new JButton("Button 4");
      JButton button5 = new JButton("Button 5");
      JButton button6 = new JButton("Button 6");
      JButton button7 = new JButton("Button 7");
      JButton button8 = new JButton("Button 8");
      JButton button9 = new JButton("Button 9");
      JButton button10 = new JButton("Button 10");
      JButton button11 = new JButton("Button 11");
      JButton button12 = new JButton("Button 12");
      JButton button13 = new JButton("Button 13");
      JButton button14 = new JButton("Button 14");
      JButton button15 = new JButton("Button 15");
      JButton button16 = new JButton("Button 16");
      JButton button17 = new JButton("Button 17");
      JButton button18 = new JButton("Button 18");

      //Add buttons to panel1
      panel1.add(button1);
      panel1.add(button2);
      panel1.add(button3);
      panel1.add(button4);
      panel1.add(button5);
      panel1.add(button6);

      //Add buttons to panel2



      //Add the buttons to panel3
      panel3.add(button7, BorderLayout.PAGE_START);
      panel3.add(button8, BorderLayout.PAGE_END);
      panel3.add(button9, BorderLayout.LINE_END);
      panel3.add(button10, BorderLayout.LINE_START);

      //Add the buttons to panel4
      panel4.add(button11);
      panel4.add(button12);
      panel4.add(button13);
      panel4.add(button14);
      panel4.add(button15);
      panel4.add(button16);
      panel4.add(button17);
      panel4.add(button18);

      // Add panel4 to the CENTER of panel3
      panel3.add(panel4, BorderLayout.CENTER);


      // Add the five panels to the content pane.
      add(panel1, BorderLayout.LINE_START);
      add(panel2, BorderLayout.CENTER);
      add(panel3, BorderLayout.LINE_END);
      //add(panel4, BorderLayout.CENTER); // AND DON'T ADD IT HERE!

      // Pack and display the window.
      pack();
      setVisible(true);
   }

   /**
      The main method creates an instance of the
      BorderPanelWindow class, causing it to display
      its window.
   */

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