java - 调整 JFrame 的大小

标签 java

好吧,这将是相当菜鸟 - 我正在使用 Eclipse 可视化编辑器(一个包含内容的 JFrame)制作一个 GUI,基本上左侧有一些按钮和文本字段等,右侧包含 4 个JPanels(其中有一些图形)。

问题是,当我在运行时调整 gui(JFrame)的大小时,内部组件保持不变(大小方面),并且我希望这 4 个 JPanel 随 JFrame 一起调整大小,这可能吗?

最佳答案

简单的答案 - 使用布局管理,包括嵌套具有不同布局的多个面板以获得您想要的结果。您基本上有两种选择:(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);
    }

关于java - 调整 JFrame 的大小,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1836205/

相关文章:

java - datainputstream 接收消息时抛出 eofException

java - JAXB 解码时出错

java - Java 中的 boolean 信号量

java - 简单的java聊天服务器,仅向其他客户端而不是发送者广播

java - 使用 Python 读取 AVRO 文件

java - 文件未被写入,已关闭文件

java - 连续运行改变 TextView 的函数

java springdoc @ApiResponses 如何定义一个列表作为返回对象使用

java - Netbeans 8.0.2 和 JDK 1.8.0 中的 JFreechart 不工作

java - 使用 mp4parser 合并 mp4 剪辑使音频隐藏在视频后面