【问题标题】:Question about making a JAVA GUI of a certain format关于制作某种格式的JAVA GUI的问题
【发布时间】:2011-05-18 03:45:24
【问题描述】:

我正在尝试制作一个看起来像这样的 GUI:

我只知道如何使用有 5 个按钮空间的 BorderLayout。北、西、中、东、南。

由于我需要在顶线上有 6 个组件,因此这种方法行不通。我不知道如何制作它,以便我可以在顶线上拥有超过 1 个组件。是否有其他布局可以使用,或者有什么方法可以操作 BorderLayout 以便我可以将 6 个组件放在顶行?

【问题讨论】:

    标签: java user-interface swing components panel


    【解决方案1】:

    我再次求助于miglayout,它绝对是最好的 Java 布局管理器。没有嵌套的 JPanel,只是一个使用基于字符串的约束的简单布局。

    启用调试模式:

    调整窗口大小后(注意文本字段的大小比例保持不变)

    import javax.swing.JButton;
    import javax.swing.JFrame;
    import javax.swing.JLabel;
    import javax.swing.JPanel;
    import javax.swing.JTextField;
    import net.miginfocom.swing.MigLayout;
    
    /**
     *
     * @author nicholasdunn
     */
    public class InterestCalculator extends JPanel {
    
        public InterestCalculator() {
            super(new MigLayout("debug, fill", "align center"));
            // Make 6 components cram into one cell
            add(new JLabel("Principal:"), "split 6");
            // This textfield grows at twice the normal rate
            add(new JTextField(), "growx 200");
            add(new JLabel("Interest rate (percentage):"));
            // This one at a normal rate
            add(new JTextField(), "growx 100");
            add(new JLabel("Years:"));
            // This one at half the normal rate
            add(new JTextField(), "growx 50, wrap");
    
            // The row with the two buttons
            add(new JButton("Compute simple interest"), "split 2");
            add(new JButton("Compute compound interest"), "wrap");
    
            // The result label
            add(new JLabel("The result with simple interest would be"));
        }
    
        public static void main(String[] args) {
            JFrame frame = new JFrame("");
            frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            JPanel panel = new InterestCalculator();
            frame.add(panel);
            frame.pack();
            frame.setVisible(true);
        }
    }
    

    【讨论】:

    • 为了清楚起见,如果您真的这样做,您需要保存对 TextFields 的引用,以便您可以捕获它们的值并计算公式。
    【解决方案2】:

    Here 是关于布局管理器的教程。

    请记住,您始终可以向 JPanel 添加多个元素并将特定布局应用到该 JPanel。然后你可以嵌套面板(在其他面板中添加面板)。

    【讨论】:

      【解决方案3】:

      您需要做的是将组件嵌套在其他组件中。例如,顶部(北)应该是一个JPanelJPanel 将包含顶部的 6 个组件。

      代码可能类似于以下内容:

      JPanel northPane = new JPanel();
      northPane.add(new JLabel("Principle: "));
      northPane.add(principleTextBox);
      ... and so on
      mainPanel.setLayout(new BorderLayout());
      mainPanel.add(northPanel, BorderLayout.NORTH);
      

      Center 组件可能是另一个包含两个中心按钮的JPanel。而 South 组件将是另一个 JPanel 包含单个 JLabel 或只是 JLabel

      如果您不必为主面板使用BorderLayout,则使用BoxLayout 可能更容易。

      【讨论】:

        【解决方案4】:

        如果我要重新创建该 UI,我将从使用 3 行 1 列的 GridLayout 的 JPanel 开始。在每一列中,我将添加一个子 JPanel。

        然后对于每一行,我将使用 GridBagLayout 来定位组件。

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2013-12-27
          • 2021-04-09
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多