请看下面的图片:
如您所见“添加程序”,“删除程序”,“添加新单元”和“删除单元”与之前的按钮重叠。
之前的按钮位于(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 );
}
}
导致这种错误显示的原因是什么?
答案 0 :(得分:2)
基本上,面板使用您之前设置的属性,例如......
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
属性,但仅为{{1}更改了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;
,而不是...