Swing程序显示JButton

时间:2015-06-30 10:25:19

标签: java swing layout-manager

我编写Swing代码来显示三个文本字段和标签以及一个按钮, 但按钮显示在框架的顶部。我想将它移动到三个文本字段的底部。这是我的代码:

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

2 个答案:

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

答案 1 :(得分:1)

将2个面板添加到框架时,它们会堆叠,因此会产生干扰。尝试在p1中向gridlayout添加另一行,并将p2添加到p1。如果您想要更灵活的布局(因为按钮将具有文本字段的大小),您可能希望使用GridBagLayout,如here所述。