How to create an input panel with several text fields and OK, CANCEL buttons?

I am trying to achieve the following effect in Java:

input panel

However, I am not sure which layout to use and how. FlowLayoutobviously not working. GridLayoutwill not work because the first 4 rows should be 1 column of the row, but the 5th row should have 2 columns.

This is my code:

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
source share
4 answers

Layouts, 2 JPanels, 1 1 , FlowLayout BoxLayout. . ( JFrame, JPanel JFrame). , 1 JFrame, . 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();
    }
}

, EmptyBorders, @LuxxMiner comment .

+5

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

, , .

, , RequestFocusListener , .

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

, .

+4

. , , , .

If you separate the two buttons on your panel and view that panel using the “just another row” buttons in the window, you can simply use the GridLayout with one column. The button bar can then use FlowLayout to place the buttons side by side.

+2
source

Try the following:

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

Without JPanel you can specify x and y coordinates

-2
source

Source: https://habr.com/ru/post/1619244/


All Articles