如何布置包含多个文本字段和OK,CANCEL按钮的输入面板?

use*_*567 5 java layout swing jpanel

我试图在Java中实现以下效果:

输入面板

但是,我不确定使用什么布局以及如何使用.FlowLayout显然不起作用.GridLayout将无法工作,因为前4行应该是1列行,但第5行需要有2列.

到目前为止这是我的代码:

public class DepositPanel extends JPanel
{
    private JLabel cashL, checksL;
    private JTextField cashTF, checksTF;
    private JButton ok, cancel;

    DepositPanel()
    {
        JPanel depositP = new JPanel();
        depositP.setLayout(new FlowLayout(FlowLayout.LEFT, 2, 2));
        depositP.setPreferredSize(new Dimension(250, 85));

        JTextField cashTF = new JTextField(22);
        JTextField checksTF = new JTextField(22);

        JLabel cashL = new JLabel("Cash:");
        JLabel checksL = new JLabel("Checks:");

        ok = new JButton("OK");
        cancel = new JButton("CANCEL");

        depositP.add(cashL);
        depositP.add(cashTF);
        depositP.add(checksL);
        depositP.add(checksTF);
        depositP.add(ok);
        depositP.add(cancel):
    }
}
Run Code Online (Sandbox Code Playgroud)

Fra*_*ool 6

您可以尝试使用Layouts的组合,JPanels按钮为2,1 ,字段为1,FlowLayout为按钮面板,BoxLayout为字段面板.并将它们添加到框架中.(我做了一个JFrame测试,但你可以将它改成JPanel并将该面板添加到你的JFrame中).请确保只有1个JFrame,请参阅使用多个JFrame,好/坏练习.

在此输入图像描述

例如:

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.util.*;
public class DepositExample {
    JFrame frame;
    JPanel buttonPane, fieldsPanel;
    JLabel cash, checks;
    JTextField cashField, checksField;
    JButton ok, cancel;

    DepositExample() {
        frame = new JFrame("Deposit");
        buttonPane = new JPanel();
        fieldsPanel = new JPanel();
        cash = new JLabel("Cash");
        checks = new JLabel("Checks");
        cashField = new JTextField("");
        checksField = new JTextField("");
        ok = new JButton("OK");
        cancel = new JButton("Cancel");

        fieldsPanel.setLayout(new BoxLayout(fieldsPanel, BoxLayout.PAGE_AXIS));
        buttonPane.setLayout(new FlowLayout());

        fieldsPanel.add(cash);
        fieldsPanel.add(cashField);
        fieldsPanel.add(checks);
        fieldsPanel.add(checksField);
        buttonPane.add(ok);
        buttonPane.add(cancel);
        frame.add(fieldsPanel, BorderLayout.PAGE_START);
        frame.add(buttonPane, BorderLayout.PAGE_END);

        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.pack();
        frame.setVisible(true);
    }
    public static void main(String args[]) {
        new DepositExample();
    }
}
Run Code Online (Sandbox Code Playgroud)

要在组件之间获得更多间距,您可以在下面的评论中按照@LuxxMiner的建议添加EmptyBorders.


小智 -2

尝试这个:

public class Window extends JFrame{
....
}

JLabel example;
//Constructor
public Window(){
    example = new JLabel("Sample text");
    example.setBounds(x,y,width,height)
    //JComponent...
    setLayout(null);
    setSize(width,height);
    setVisible(true);
}
Run Code Online (Sandbox Code Playgroud)

如果没有 JPanel,您可以指定 x 和 y 坐标

  • 请参阅:[为什么在 Swing 中使用 null 布局不受欢迎?](http://stackoverflow.com/questions/6592468/why-is-it-frowned-upon-to-use-a-null-layout- in-swing)和 [空布局是邪恶的](http://www.leepoint.net/GUI/layouts/nulllayout.html) 来阅读我投反对票的原因,你不应该使用和/或推荐使用空布局。 (3认同)