调整JFrame的大小

时间:2009-12-02 22:11:06

标签: java

好吧,这将是相当noobish - 我正在使用Eclipse Visual Editor(带有内容的JFrame)制作GUI,基本上在左侧有一些按钮和文本字段等,右侧包含4 JPanels(其中包含一些图形)。

问题是,当我在运行时调整gui(JFrame)的大小时,里面的组件保持不变(大小明智),我希望这4个JPanels可以用JFrame调整大小,是否可能?

5 个答案:

答案 0 :(得分:2)

简单回答 - 使用布局管理,包括嵌套具有不同布局的多个面板以获得所需的结果。您基本上有两个选择:(a)使用布局管理器来管理组件位置和大小调整,或(b)手动完成所有操作。你真的不想做(b)。

您可能希望从layout manager tutorial开始。

使用基于表格的布局可以使布局显着比使用Java分发的标准布局管理器更容易;有很多免费提供。我在tech.dolhub.com

这是一个例子(来自MatrixLayout的帮助,但我不记得我是否真的编译了示例代码......我希望我做过一次):

+--------------------------------------------------------------------------------+
+                                                                                |
+     Name : |________________________________________________________________|  |
+                                                                                |
+  Address : |________________________________________________________________|  |
+                                                                                |
+            |________________________________________________________________|  |
+                                                                                |
+            |________________________________________________________________|  |
+                                                                                |
+     City : |____________________| State |__| Zip |_____| - |____|              |
+                                                                                |
+    Phone : |___|-|___|-|____|                                                  |
+                                                                                |
+    Notes : |                              |  |                              |  |
+            |                              |  |                              |  |
+            |                              |  |                              |  |
+            |                              |  |                              |  |
+            |                              |  |                              |  |
+            |______________________________|  |______________________________|  |
+            [BOTTOM-LEFT]                                     [ BOTTOM-RIGHT ]  |
+                                                                                |
+                       [      Yes      ]   [      No      ]   [    Abort     ]  |
+                                                                                |
+--------------------------------------------------------------------------------+
private void createContent(Container main){
    String[]                            rows,cols;                                  // row/column specification arrays

    JPanel                              phnpnl,cszpnl,btnpnl;                       // special nested panels

    // create components here...

    // CREATE MAIN PANEL WITH DESIRED ROWS AND COLUMNS
    rows=MatrixLayout.arrayOf(10,"Size=Pref CellAlign=Middle CellInsets=5,0");   // standard row spec
    rows[6]                     ="Size=100% CellAlign=Top    CellInsets=5,0";    // note: row 7 ([6] is index)
    rows[7]                     ="Size=Pref CellAlign=Top    CellInsets=5,0";    // note: row 8 ([7] is index)
    rows[8]                     ="Size=Pref CellAlign=Top    CellInsets=5,0";    // note: row 9 ([8] is index)
    cols=MatrixLayout.arrayOf(3 ,"size=Pref CellAlign=Right  CellInsets=5,0");   // standard column spec
    cols[1]                     ="Size=50%  CellAlign=Left   CellInsets=5,0";    // note: col 2 ([1] is index)
    cols[2]                     ="Size=50%  CellAlign=Left   CellInsets=5,0";    // note: col 3 ([2] is index)
    con.setLayout(new MatrixLayout(rows,cols,"Row=Cur Col=Next"));

    // CREATE SPECIAL NESTED PANELS
    phnpnl=MatrixLayout.singleRowBar(5,false,new DctComponent[]{phnPart1,phnPart2,phnPart3                                   });
    cszpnl=MatrixLayout.singleRowBar(5,1    ,new DctComponent[]{city,createLabel("State"),state,createLabel("Zip"),zip,zipext});
    btnpnl=MatrixLayout.singleRowBar(5,true ,new DctComponent[]{yes,no,cancel                                                });
    phnpnl.setName("PhonePanel");
    cszpnl.setName("CityStateZipPanel");
    btnpnl.setName("ButtonPanel");

    // ADD COMPONENTS TO MAIN PANEL
    con.add(createLabel(   "Name :"),"row=Next col=1"); con.add(name    ,"               hAlign=Fill  hSpan=2                               ");
    con.add(createLabel("Address :"),"row=Next col=1"); con.add(address1,"               hAlign=Fill  hSpan=2                               ");
                                                        con.add(address2,"Row=Next Col=2 hAlign=Fill  hSpan=2                               ");
                                                        con.add(address3,"Row=Next Col=2 hAlign=Fill  hSpan=2                               ");
    con.add(createLabel(   "City :"),"row=Next col=1"); con.add(cszpnl  ,"                            hSpan=2                               ");
    con.add(createLabel(  "Phone :"),"row=Next col=1"); con.add(phnpnl  ,"                            hSpan=2                               ");
    con.add(createLabel(  "Notes :"),"row=Next col=1"); con.add(notes1  ,"Row=Cur  Col=2 hAlign=Fill          vAlign=Fill                   ");
                                                        con.add(notes2  ,"Row=Cur        hAlign=Fill          vAlign=Fill                   ");
                                                        con.add(notes3  ,"Row=Next Col=2 hAlign=Left                      hGroup=NoteButtons");
                                                        con.add(notes4  ,"Row=Cur        hAlign=Right                     hGroup=NoteButtons");
    con.add(btnpnl                  ,"row=Next col=1 hAlign=Right hSpan=3");
    main.setBorder(new DctEmptyBorder(10));
    main.setBackground(SystemColor.window);
    }

答案 1 :(得分:0)

这取决于您将组件添加到框架的方式以及您使用的布局管理器。在这个例子中,四个按钮将随窗口调整大小。

JFrame frame = new JFrame();
Container cp = frame.getContentPane();
cp.setLayout(new GridLayout(2, 2));
cp.add(new JButton("one"));
cp.add(new JButton("two"));
cp.add(new JButton("three"));
cp.add(new JButton("four"));
frame.pack();
frame.setVisible(true);

但是,您可以使用许多不同的布局管理器。 read this

答案 2 :(得分:0)

使用布局管理器。如果你不使用它,你必须自己调整大小(包括每次设置新位置)

LayoutManagers似乎有问题,实际上它们是。

在Java 1.6中添加了GroupLayout管理器,我认为它非常有用且简单。

alt text

布局管理器的功能在与其他人一起使用时会被释放。这是一个A Visual Guide to Layout Managers

如果您发布应用程序的屏幕截图,我非常确定我们可以提供一个可以使用的基本布局。

答案 3 :(得分:0)

好的,是的,我知道我应该使用布局管理器等但是我仍然选择使用Eclipse Visual Editor(它没有所有布局管理器)来构建它是因为我没有太多时间对于这个项目,我认为这样会更快。

基本上这个屏幕显示了程序的外观 - 左边的一些随机输入/按钮(暂时添加了随机输入/按钮)和右边的4个JPanels - 它们对我来说非常重要。

screen http://img130.imageshack.us/img130/5282/screenmy.png

答案 4 :(得分:0)

嗯,如果没有布局管理员,这可能会很糟糕,但是如果你想要它.....我们走了。

  1. 在您的框架中添加ComponentListener以侦听其调整大小,您可以在调整大小后获取窗口大小。
  2. 像:

    frame.addComponentListener(new ComponentListener() {
    
        @Override
        public void componentShown(ComponentEvent e) {
    
        }
    
        @Override
        public void componentResized(ComponentEvent e) {
            if (e.getSource() instanceof JFrame) {
                  JFrame frame = (JFrame)(e.getSource());
                  frame.repaintElements(frame.getWidth(), frame.getHeight());
            }
        }
    
        @Override
        public void componentMoved(ComponentEvent e) {
    
        }
    
        @Override
        public void componentHidden(ComponentEvent e) {
    
        }
    });
    
    1. 在您的JFrame课程中(在这种情况下,您必须将其设为JFrame的子课程才能添加您自己的方法),请定义repaintElements(int w, int h)方法,高度和新宽度作为参数。

    2. repaintElements(int x, int y)中,实现您的逻辑以更改每个元素的位置和大小。我使用setBound(int x, int y, int w, int h)xy确定元素左上角相对于其容器的位置,wh确定其大小。

    3. 毕竟我让它在我的项目中工作,但这一切都很乏味。想一想:

      我有一个很长的标签,然后是一张桌子,然后是桌子下方的另一个面板,最后是右下角一个出口按钮。

      首先,标题标签顶部。我想让它水平扩展而不是垂直扩展,所以在repaintElements()我设置它:

      label1.setBounds(5, 0, x-5, 30);
      

      所以每当窗口改变大小时,标签位于距离最左边5个像素,距离最顶部0像素,宽度为x-5像素(为边框留出一些空间),并始终30像素的高度。

      现在我的JScrollPane包含JTable。我希望它随窗口一起扩展,所以:

      table.setBounds(5, 30, x-15, y-30-60-5);
      

      因为垂直我必须为上面的标签(30像素)和下面的面板(60像素)和下面的按钮(5像素)留出空间,并且该表的位置由标签的高度和宽度决定上面(5和30,如果没有边界......)。该表有一个垂直滚动,所以我也必须为它留出空间(所以x-15代替x-5。神圣的狗屎。)

      然后是下面的另一个小组。如果窗口展开,水平我希望这个面板也增加,但不是垂直增加,所以:

      panel.setBounds(5, y-30-60-5+30, x-10, 60); //the y value is "y coordinate of the element above" + "width of the element above"
      

      最后,按钮。我决定不增加它的大小,但只是让它留在角落里,所以:

      button.setBounds(x-20, y-10, 15, 5); //the button is 15*5 pixels. extra space is for border.
      

      容易吗?看起来似乎如此,但是你需要进行大量的测试........如果边框的大小发生变化,另外还有一些测试......实际上你会浪费时间在一些琐碎的事情上,很快就会感到无聊。所以,使用布局管理器。

相关问题