文件编译能通过,就是在运行执行的时候出现这样的问题:
Exception in thread "main" java.langIllegalArgumentException:cannot add to layout:constraint must be a string下边是我的源代码:
import java.sql.*;
import java.awt.*;
import java.io.*;
import java.net.*;
import java.awt.event.*;
import javax.swing.JOptionPane;
import java.util.StringTokenizer;public class All extends Frame implements ActionListener /////,Runnable
{
CardLayout mycard;
Panel pA = new Panel();
Panel pB = new Panel();
Panel panela = new Panel();
Panel panelb = new Panel();
Panel panelc = new Panel();
Button button1,button2,button3;
TextField text21,text22;
TextArea see= new TextArea("",13,80,TextArea.SCROLLBARS_VERTICAL_ONLY);
public All() {
super("档案管理");
setLayout(new BorderLayout());
setBackground(Color.gray);
setBounds(50,50,200,100);
setVisible(true);
setResizable(false);
validate();
mycard=new CardLayout();
pB.setLayout(mycard); ////pB的布局设置为卡片式布局
button1= new Button("档案输入");
button2= new Button("信息查看");
button3= new Button("信息编辑"); text21=new TextField(10);
text22=new TextField(20);
text22.setEditable(false);
///////########################################################################
panela.add(text21);//////档案输入面板///////########################################################################
panelb.add(text22);//////信息查看面板///////########################################################################
panelc.add(see);//////档案修改面板///////########################################################################
pA.add(button1);
pA.add(button2);
pA.add(button3); pB.add(panela);
pB.add(panelb);
pB.add(panelc);
add(pA, BorderLayout.NORTH);
add(pB, BorderLayout.CENTER);
button1.addActionListener(this);
button2.addActionListener(this);
button3.addActionListener(this);
addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {
System.exit(0);
}
});}public void actionPerformed(ActionEvent e) {
if(e.getSource()==button1) {
mycard.first(pB);
}
if (e.getSource()==button2) {
mycard.next(pB);
}
else if(e.getSource()==button3){
mycard.last(pB);
}
}public static void main(String args[]) {
All aa = new All();
aa.pack();
}
}
Exception in thread "main" java.langIllegalArgumentException:cannot add to layout:constraint must be a string下边是我的源代码:
import java.sql.*;
import java.awt.*;
import java.io.*;
import java.net.*;
import java.awt.event.*;
import javax.swing.JOptionPane;
import java.util.StringTokenizer;public class All extends Frame implements ActionListener /////,Runnable
{
CardLayout mycard;
Panel pA = new Panel();
Panel pB = new Panel();
Panel panela = new Panel();
Panel panelb = new Panel();
Panel panelc = new Panel();
Button button1,button2,button3;
TextField text21,text22;
TextArea see= new TextArea("",13,80,TextArea.SCROLLBARS_VERTICAL_ONLY);
public All() {
super("档案管理");
setLayout(new BorderLayout());
setBackground(Color.gray);
setBounds(50,50,200,100);
setVisible(true);
setResizable(false);
validate();
mycard=new CardLayout();
pB.setLayout(mycard); ////pB的布局设置为卡片式布局
button1= new Button("档案输入");
button2= new Button("信息查看");
button3= new Button("信息编辑"); text21=new TextField(10);
text22=new TextField(20);
text22.setEditable(false);
///////########################################################################
panela.add(text21);//////档案输入面板///////########################################################################
panelb.add(text22);//////信息查看面板///////########################################################################
panelc.add(see);//////档案修改面板///////########################################################################
pA.add(button1);
pA.add(button2);
pA.add(button3); pB.add(panela);
pB.add(panelb);
pB.add(panelc);
add(pA, BorderLayout.NORTH);
add(pB, BorderLayout.CENTER);
button1.addActionListener(this);
button2.addActionListener(this);
button3.addActionListener(this);
addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {
System.exit(0);
}
});}public void actionPerformed(ActionEvent e) {
if(e.getSource()==button1) {
mycard.first(pB);
}
if (e.getSource()==button2) {
mycard.next(pB);
}
else if(e.getSource()==button3){
mycard.last(pB);
}
}public static void main(String args[]) {
All aa = new All();
aa.pack();
}
}
小弟初学JAVA,那位大哥给我下
java.lang.IllegalArgumentException: cannot add to layout:
constraint must be a string
at java.awt.CardLayout.addLayoutComponent(CardLayout.java:190)
at java.awt.Container.addImpl(Container.java:664)
at java.awt.Container.add(Container.java:307)
at All.<init>(All.java:51)
at All.main(bbb.java:119)
import java.awt.event.*;
import javax.swing.*;public class CardLayoutDemo{
private static String[] texts = {"Button_Panel","TextField_Panel"};
private JPanel cards;
//create a JPanel containing JButtons
private JPanel createJButtonPanel(){
JPanel p = new JPanel();
p.setLayout(new FlowLayout());
p.add(new JButton("JButton1"));
p.add(new JButton("JButton2"));
p.add(new JButton("JButton3"));
return p;
}
//create a JPanel containing JTextFields
private JPanel createJTextFieldPanel(){
JPanel p = new JPanel();
p.setLayout(new FlowLayout());
p.add(new JTextField("First"));
p.add(new JTextField("Second"));
p.add(new JTextField("Third"));
return p;
}
//add the above 2 Panels to the Container
public void addToContainer(Container cp){
cards = new JPanel();
cards.setLayout(new CardLayout());//use CardLayout
cards.add(createJButtonPanel(),texts[0]);
cards.add(createJTextFieldPanel(),texts[1]);
cp.add(cards);
}
//show the index-th card in the cards
public void showCard(int index){
((CardLayout)cards.getLayout()).show(cards,texts[index]);
}
//get the count of components in the cards
public int getComponentCount(){
return cards.getComponentCount();
}
public static void main(String[] args){
JFrame f = new JFrame();
f.setTitle("Card Layout Demo");
f.setSize(200,150);
Container cp = f.getContentPane();
final CardLayoutDemo demo = new CardLayoutDemo();
demo.addToContainer(cp);
JButton btnSwith = new JButton("Swith");//do some swith
cp.add(btnSwith,BorderLayout.SOUTH);
btnSwith.addActionListener(new ActionListener(){
int index = 0;
public void actionPerformed(ActionEvent e){
index = (index + 1)% demo.getComponentCount();
demo.showCard(index);
}
});
f.setVisible(true);//show the JFrame
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
}
创建CardLayout对象并设置了布局后在调用布局时应该是: 容器名.add(组件,组件代号)
其中组件代号应该是用 " " 修饰的字符。后来的按钮事件中用show方法调用
用创建的布局变量调用 假设创建的是 CardLayout mycard; 调用的时候是mycard.show(组件,组件代号)。
我自己弄出来啦~~~` 不过谢谢guozhang 就是参数设定错误了。
pB.add(panelb);
pB.add(panelc);
改成,cardlayout布局必须有一个字符串描述加入的组件
pB.add(panela,"a");
pB.add(panelb,"b");
pB.add(panelc,"c");