请看下面的这张图片:
如您所见,“添加程序”,“删除程序”,“添加新单元”和“删除单元”与前面的按钮重叠。
前面的按钮位于(0,9),指定的按钮位于(0,10)。
这是代码:
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class guiStyle extends JFrame
{
Container cp = getContentPane();
public guiStyle()
{
super( "Student Study Planner 2014" );
makeFrame2();
pack();
setVisible(true);
setResizable(true);
setDefaultCloseOperation(EXIT_ON_CLOSE);
}
public void makeFrame2()
{
setPreferredSize( new Dimension ( 1000 , 600 ) );
cp.setLayout( new GridBagLayout() );
GridBagConstraints gbc = new GridBagConstraints();
gbc.insets = new Insets ( 5 , 5 , 5 , 5 );
JLabel lblProgram , lblUnit , lblDetails;
JList programList , unitList;
JTextArea pl = new JTextArea("ProgramList" , 20 , 20);
JTextArea ul = new JTextArea("UnitList" , 20 , 20);
JScrollPane sp , sp2;
JButton btnAddProgram , btnDelProgram;
JButton btnAddUnit2Prog,btnDelUnitFromProgram;
JButton btnAddUnit, btnDelUnit;
JButton btnAddPrere,btnDelPrere;
JButton btnAddCoreq,btnDelCoreq;
lblProgram = new JLabel ( "Program" );
lblUnit = new JLabel ( "Unit" );
programList = new JList();
unitList = new JList();
btnAddProgram = new JButton ( "Add Program" );
btnDelProgram = new JButton ( "Delete Program" );
btnAddUnit2Prog=new JButton ( "Add Unit To Program" );
btnDelUnitFromProgram=new JButton("Delete Unit From Program");
btnAddUnit = new JButton ("Add New Unit");
btnDelUnit = new JButton ("Delete Unit");
btnAddPrere = new JButton("Add Prerequisite");
btnDelPrere = new JButton("Delete Prerequisite");
btnAddCoreq = new JButton("Add Corequisite");
btnDelCoreq = new JButton("Delete Corequisite");
gbc.gridx = 0;
gbc.gridy = 0;
gbc.gridwidth = 2 ;
gbc.fill = GridBagConstraints.HORIZONTAL;
cp.add ( lblProgram , gbc );
gbc.gridx = 0;
gbc.gridy = 2;
gbc.gridwidth = 2;
gbc.gridheight= 7;
gbc.fill = GridBagConstraints.HORIZONTAL;
gbc.fill = GridBagConstraints.VERTICAL;
cp.add ( pl , gbc );
JPanel p1 = new JPanel();
p1.add ( btnAddProgram );
p1.add ( btnDelProgram );
gbc.gridx = 0;
gbc.gridy = 9;
cp.add ( p1 , gbc );
JPanel p2 = new JPanel();
p2.add ( btnAddUnit2Prog );
p2.add ( btnDelUnitFromProgram );
gbc.gridx = 0;
gbc.gridy = 10;
cp.add ( p2 , gbc );
//Unit
gbc.gridx = 2;
gbc.gridy = 0;
gbc.gridwidth = 2 ;
gbc.fill = GridBagConstraints.HORIZONTAL;
cp.add ( lblUnit , gbc );
gbc.gridx = 2;
gbc.gridy = 2;
gbc.gridwidth = 2;
gbc.gridheight= 7;
gbc.fill = GridBagConstraints.HORIZONTAL;
gbc.fill = GridBagConstraints.VERTICAL;
cp.add ( ul , gbc );
JPanel p3 = new JPanel();
p3.add ( btnAddUnit );
p3.add ( btnDelUnit );
gbc.gridx = 2;
gbc.gridy = 9;
cp.add ( p3 , gbc );
JPanel p4 = new JPanel();
p4.add ( btnAddPrere );
p4.add ( btnDelPrere );
gbc.gridx = 2;
gbc.gridy = 10;
cp.add ( p4 , gbc );
}
}
是什么原因导致此显示错误?
最佳答案
基本上,面板使用的是先前设置的属性,例如...
gbc.gridx = 0;
gbc.gridy = 2;
gbc.gridwidth = 2;
gbc.gridheight = 7;
gbc.fill = GridBagConstraints.HORIZONTAL;
gbc.fill = GridBagConstraints.VERTICAL;
cp.add(pl, gbc);
JPanel p1 = new JPanel();
p1.add(btnAddProgram);
p1.add(btnDelProgram);
gbc.gridx = 0;
gbc.gridy = 9;
cp.add(p1, gbc);
您为
fill
设置了gridwidth
,gridheight
和pl
属性,但仅更改了gridx
的gridy
和p1
,这意味着它仍在使用相同的gridheight
属性。\如果将
gridheight
属性重置为1
,则应该可以解决问题...JPanel p1 = new JPanel();
p1.setBorder(new LineBorder(Color.MAGENTA));
p1.add(btnAddProgram);
p1.add(btnDelProgram);
gbc.gridheight = 1;
gbc.gridx = 0;
gbc.gridy = 9;
cp.add(p1, gbc);
(我添加了颜色边框,所以我可以看到自己在做什么)
也,
gbc.fill = GridBagConstraints.HORIZONTAL;
gbc.fill = GridBagConstraints.VERTICAL;
不会按照您的想法去做,您可能想使用
gbc.fill = GridBagConstraints.BOTH;
代替...