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

时间:2015-12-09 18:25:25

标签: java swing layout jpanel

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

input panel

但是,我不确定使用什么布局以及如何使用。 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):
    }
}

4 个答案:

答案 0 :(得分:5)

您可以尝试使用布局组合,2 JPanels,按钮为1,字段为1,按钮面板为FlowLayout,字段面板为BoxLayout。并将它们添加到框架中。 (我做了一个JFrame测试,但你可以将它改成JPanel并将该面板添加到你的JFrame中)。请确保只有1个JFrame,请参阅The use of multiple JFrames, Good / Bad Practice

enter image description here

例如:

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();
    }
}

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

答案 1 :(得分:4)

在这种情况下,您可以使用JOptionPane为您构建一个简单的面板:

JTextField firstName = new JTextField(10);
JTextField lastName = new JTextField(10);

Object[] msg = {"First Name:", firstName, "Last Name:", lastName};

result = JOptionPane.showConfirmDialog(
    frame,
    msg,
    "Use default layout",
    JOptionPane.OK_CANCEL_OPTION,
    JOptionPane.PLAIN_MESSAGE);

if (result == JOptionPane.YES_OPTION)
{
    System.out.println(firstName.getText() + " : " + lastName.getText());
}
else
{
    System.out.println("Canceled");
}

这种方法的唯一问题是焦点将放在按钮上,而不是名字文本字段。

因此,要解决此问题,您可以查看Dialog Focus中找到的RequestFocusListener,这会在显示对话框后将焦点放在第一个名称文字字段上。

JTextField firstName = new JTextField(10);
firstName.addAncestorListener( new RequestFocusListener() );

虽然对于更复杂的布局,最好使用适当的布局管理器创建一个或多个面板,以满足要求。

答案 2 :(得分:2)

有很多方法可以实现这样的布局。您需要习惯的第一件事是,它通常更简单使用不同的布局管理器将不同的需求分成不同的容器。

如果您将两个按钮分开到自己的面板中,并将按钮处理为"只需另一行"在窗口中,您基本上只需使用一个GridLayout和一列。然后,带按钮的面板可以使用FlowLayout将按钮并排放置。

答案 3 :(得分:-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);
}

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

相关问题