1

I write Swing code to display three text fields and labels and one button, but the button displays at top the frame. I want to move it to the bottom of the three text fields. Here is my code:

import javax.swing.*;
import java.awt.*;

class textfield 
{
    public static void main(String[] args) 
    {
        JFrame jf;
        JLabel rcno,name,amount;
        JTextField rc_no,na_me,am_t;
        JButton ok;
        JPanel p1,p2;

        jf=new JFrame("Billing");

        rcno=new JLabel("Recipt No:   ");
        rc_no=new JTextField(6);
        name=new JLabel("Student Name:   ");
        na_me=new JTextField(20);
        amount=new JLabel("Amount:   ");
        am_t=new JTextField(5);
        ok=new JButton("ok");

        p1=new JPanel();
        //p1.setLayout(new FlowLayout());
        p1.setLayout(new GridLayout(3,2,5,5));

        p1.add(rcno);
        p1.add(rc_no);
        p1.add(name);
        p1.add(na_me);
        p1.add(amount);
        p1.add(am_t);

        p2=new JPanel();
        p2.setLayout(new FlowLayout(FlowLayout.RIGHT));
        //ok.setPreferredSize(new Dimension(50,20));
        p2.add(ok);

        p1.setBounds(110,100,200,100);

        //p2.setBounds(220,220,10,10);

        jf.add(p1);
        jf.add(p2);

        jf.setSize(400,500);
        jf.setVisible(true);
        jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    }
}
trashgod
  • 203,806
  • 29
  • 246
  • 1,045

2 Answers2

5

Some notes on your example:

  • Don't use setXxxSize() or setBounds(); let the layout do the work.

  • For p1, also consider GridBagLayout, shown here, or GroupLayout, shown here.

  • The default layout of JFrame is BorderLayout; add p2 to the SOUTH.

  • Swing GUI objects should be constructed and manipulated only on the event dispatch thread.

image

import javax.swing.*;
import java.awt.*;

class FormTest {

    public static void main(String[] args) {
        EventQueue.invokeLater(new FormTest()::display);
    }

    private void display() {
        JFrame jf = new JFrame("Billing");

        JButton ok = new JButton("ok");
        JLabel rcno = new JLabel("Recipt No:", JLabel.RIGHT);
        JTextField rc_no = new JTextField(12);
        JLabel name = new JLabel("Student Name:", JLabel.RIGHT);
        JTextField na_me = new JTextField(12);
        JLabel amount = new JLabel("Amount:", JLabel.RIGHT);
        JTextField am_t = new JTextField(12);

        JPanel p1 = new JPanel();
        p1.setLayout(new GridLayout(0, 2, 5, 5));
        p1.setBorder(BorderFactory.createEmptyBorder(5, 5, 5, 5));
        p1.add(rcno);
        p1.add(rc_no);
        p1.add(name);
        p1.add(na_me);
        p1.add(amount);
        p1.add(am_t);

        JPanel p2 = new JPanel();
        p2.setLayout(new FlowLayout(FlowLayout.RIGHT));
        p2.add(ok);

        jf.add(p1);
        jf.add(p2, BorderLayout.SOUTH);
        jf.pack();
        jf.setVisible(true);
        jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    }
}
Community
  • 1
  • 1
trashgod
  • 203,806
  • 29
  • 246
  • 1,045
1

When adding 2 Panels to a frame they will stack and will therefore interfere. Try adding another line to your gridlayout in p1 and add p2 to p1. If you want a more flexible layout (because the button will have the size of the text fields) you might want to use GridBagLayout as described here.

omgBob
  • 527
  • 3
  • 9