Domanda

class Frame extends JFrame{

public Frame()
{
    JFrame jf= new JFrame("Student Admission");
    jf.setLayout(new GridLayout(5,1));
    JPanel jpn= new JPanel();


    JPanel enr= new JPanel();
    enr.setLayout(new FlowLayout(FlowLayout.LEFT));
    JLabel enrno= new JLabel("Enrollment Number",JLabel.LEFT);
    JTextField enrnoinput=new JTextField(3);
    enr.add(enrno);
    enr.add(enrnoinput);
    jf.add(enr);


    JLabel name= new JLabel("Student's Name",JLabel.LEFT);
    JTextField nameinput=new JTextField(60);
    jpn.add(name);
    jpn.add(nameinput);
    jf.add(jpn);

    JPanel jpfn= new JPanel();
    JLabel fname= new JLabel("Fathers's Name",JLabel.LEFT);
    JTextField fnameinput=new JTextField(60);
    jpfn.add(fname);
    jpfn.add(fnameinput);
    jf.add(jpfn);


    JPanel hscp= new JPanel();
    hscp.setLayout(new FlowLayout(FlowLayout.LEFT));
    JLabel hscper= new JLabel("Hsc Percentage",JLabel.LEFT);
    JTextField hscperinput=new JTextField(3);
    hscp.add(hscper);
    hscp.add(hscperinput);
    jf.add(hscp);


    JPanel sscp= new JPanel();
    sscp.setLayout(new FlowLayout(FlowLayout.LEFT));
    JLabel sscper= new JLabel("Ssc Percentage",JLabel.LEFT);
    JTextField sscperinput=new JTextField(3);
    sscp.add(sscper);
    sscp.add(sscperinput);
    jf.add(sscp);

    //After Adding this panel the frame's structure get disturbed
    JPanel buttonPanel= new JPanel();
    JButton save= new JButton("Save");
    JButton cancel= new JButton("Cancel");
    buttonPanel.add(save);
    buttonPanel.add(cancel);
    jf.add(buttonPanel);


    jf.setResizable(false);
    jf.pack();
    jf.setVisible(true);
    jf.setDefaultCloseOperation(EXIT_ON_CLOSE);
}
} 

Before Adding buttonPanel enter image description here

After adding buttonPanel enter image description here

I want to add button panel in the Middle-Bottom of the frame how can I do that?

È stato utile?

Soluzione

Your grid layout only accounts for 5 components, so it's screwing up everything when you use 6. Add the buttonPanel to the sscp panel and then add the sscp panel to the overall Frame.

JPanel sscp= new JPanel();
sscp.setLayout(new FlowLayout(FlowLayout.LEFT));
JLabel sscper= new JLabel("Ssc Percentage",JLabel.LEFT);
JTextField sscperinput=new JTextField(3);
sscp.add(sscper);
sscp.add(sscperinput);

JPanel buttonPanel= new JPanel();
JButton save= new JButton("Save");
JButton cancel= new JButton("Cancel");
buttonPanel.add(save);
buttonPanel.add(cancel);
//change here
sscp.add(buttonPanel);
jf.add(sscp);

GUI

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top