各位大侠帮忙看下是哪里出错了 小弟不才  菜鸟一枚  望各位不吝赐教
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class PanelDemo extends JFrame implements ActionListener{
private JButton oneButton,twoButton,threeButton,fourButton,fiveButton;
private JLabel Label;
private BorderLayout layout;
public PanelDemo(){
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setTitle("JPanelDemo");
setSize(400,200);
layout = new BorderLayout();
Container cp = getContentPane();
cp.setLayout(layout);
oneButton = new JButton("Button one");
twoButton = new JButton("Button two");
threeButton = new JButton("Button three");
fourButton = new JButton("Button four");
fiveButton = new JButton("Button five");
JPanel pNorth = new JPanel();//添加第一个面板,放在窗体的顶部(North)
pNorth.add(oneButton);//此面板放置oneButton和twoButton
pNorth.add(twoButton);
cp.add(pNorth,"North");
JPanel pSouth = new JPanel();//添加第二面板,放在窗体的底部(South)
pSouth.add(threeButton);//此面板里放置threeButton fourButton fiveButton
pSouth.add(fourButton);
pSouth.add(fiveButton);
cp.add(pSouth,"south");
Label = new JLabel("中区是JLabel组件,显示您单击了哪一个按钮",JLabel.CENTER);
cp.add(Label,"Center");
oneButton.addActionListener(this);
twoButton.addActionListener(this);
threeButton.addActionListener(this);
fourButton.addActionListener(this);
fiveButton.addActionListener(this);
}
public void actionPerformed(ActionEvent e){
Object source = e.getSource();
if(source == oneButton) Label.setText("您单击了Button one 按钮");
else if(source == twoButton)Label.setText("您单击了Button two按钮");
else if(source == threeButton)Label.setText("您单击了 Button three 按钮");
else if(source == fourButton)Label.setText("您单击了Button four按钮");
else if(source == fiveButton)Label.setText("您单击了Button five按钮");
}
public static void main(String[] args){
JFrame Frame = new PanelDemo();
Frame.show();
}
}

解决方案 »

  1.   

    运行时出下面的提示
    Exception in thread "main" java.lang.IllegalArgumentException: cannot add to layout: unknown constraint: south
    at java.awt.BorderLayout.addLayoutComponent(BorderLayout.java:446)
    at java.awt.BorderLayout.addLayoutComponent(BorderLayout.java:407)
    at java.awt.Container.addImpl(Container.java:1058)
    at java.awt.Container.add(Container.java:899)
    at PanelDemo.<init>(PanelDemo.java:28)
    at PanelDemo.main(PanelDemo.java:46)
      

  2.   

     cp.add(pSouth,"south");  楼主是在这里出错的!把参数south改成首字母大写的!
    即 cp.add(pSouth,"South"); 这样就行了,楼主试试看!
      

  3.   

    直接用cp.add(pSouth,BorderLayout.SOUTH);