Java 添加包含JButton的Jpanel会干扰帧的结构
在添加Java 添加包含JButton的Jpanel会干扰帧的结构,java,swing,user-interface,layout,gridbaglayout,Java,Swing,User Interface,Layout,Gridbaglayout,在添加按钮面板之前 添加按钮面板后 我想在框架的中间底部添加按钮面板,我如何才能做到这一点?您的网格布局只包含5个组件,因此使用6个组件时会把所有组件都搞糟。将按钮面板添加到sscp面板,然后将sscp面板添加到整个框架 class Frame extends JFrame{ public Frame() { JFrame jf= new JFrame("Student Admission"); jf.setLayout(new GridLayout(5,1));
按钮面板之前
添加按钮面板后
我想在框架的中间底部添加按钮面板,我如何才能做到这一点?您的网格布局只包含5个组件,因此使用6个组件时会把所有组件都搞糟。将
按钮面板添加到sscp
面板,然后将sscp
面板添加到整个框架
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);
}
}
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);