package com.app;import java.awt.Button;
import java.awt.CardLayout;
import java.awt.Frame;
import java.awt.Panel;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;public class TestCardLayout extends WindowAdapter implements ActionListener{
private Frame frame;
private CardLayout cardLayout;
public void actionPerformed(ActionEvent e){
cardLayout.next(frame);
}
public void run(){
frame = new Frame("Test Card Layout");
frame.setSize(150,120);
frame.addWindowListener(this);
Button button1 = new Button("卡片1");
Button button2 = new Button("卡片2");
Button button3 = new Button("卡片3");
Panel panel1 = new Panel();
Panel panel2 = new Panel();
Panel panel3 = new Panel();
panel1.add(button1);
button1.addActionListener(this);
panel2.add(button2);
button2.addActionListener(this);
panel3.add(button3);
button3.addActionListener(this);
cardLayout = new CardLayout();
frame.add(panel1,"第1张");
frame.add(panel2,"第2张");
frame.add(panel3,"第3张");
frame.setVisible(true);
}
public void windowClosing(WindowEvent e){
System.exit(0);
}
public static void main(String[] args) {
TestCardLayout myCardLayout = new TestCardLayout();
myCardLayout.run(); }}没有语法错误,但是运行不了
Exception in thread "main" java.lang.IllegalArgumentException: cannot add to layout: unknown constraint: 第1张
at java.awt.BorderLayout.addLayoutComponent(BorderLayout.java:446)
at java.awt.BorderLayout.addLayoutComponent(BorderLayout.java:407)
at java.awt.Container.addImpl(Container.java:1068)
at java.awt.Container.add(Container.java:903)
at com.app.TestCardLayout.run(TestCardLayout.java:37)
at com.app.TestCardLayout.main(TestCardLayout.java:51)各位帮小弟看看是什么错误
import java.awt.CardLayout;
import java.awt.Frame;
import java.awt.Panel;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;public class TestCardLayout extends WindowAdapter implements ActionListener{
private Frame frame;
private CardLayout cardLayout;
public void actionPerformed(ActionEvent e){
cardLayout.next(frame);
}
public void run(){
frame = new Frame("Test Card Layout");
frame.setSize(150,120);
frame.addWindowListener(this);
Button button1 = new Button("卡片1");
Button button2 = new Button("卡片2");
Button button3 = new Button("卡片3");
Panel panel1 = new Panel();
Panel panel2 = new Panel();
Panel panel3 = new Panel();
panel1.add(button1);
button1.addActionListener(this);
panel2.add(button2);
button2.addActionListener(this);
panel3.add(button3);
button3.addActionListener(this);
cardLayout = new CardLayout();
frame.add(panel1,"第1张");
frame.add(panel2,"第2张");
frame.add(panel3,"第3张");
frame.setVisible(true);
}
public void windowClosing(WindowEvent e){
System.exit(0);
}
public static void main(String[] args) {
TestCardLayout myCardLayout = new TestCardLayout();
myCardLayout.run(); }}没有语法错误,但是运行不了
Exception in thread "main" java.lang.IllegalArgumentException: cannot add to layout: unknown constraint: 第1张
at java.awt.BorderLayout.addLayoutComponent(BorderLayout.java:446)
at java.awt.BorderLayout.addLayoutComponent(BorderLayout.java:407)
at java.awt.Container.addImpl(Container.java:1068)
at java.awt.Container.add(Container.java:903)
at com.app.TestCardLayout.run(TestCardLayout.java:37)
at com.app.TestCardLayout.main(TestCardLayout.java:51)各位帮小弟看看是什么错误
解决方案 »
- 将多个页面放到Book里面,但只能打印第一页
- 关于JAVA网络编程,求指教!
- 求教,提取系统时间问题
- 急! J2SE、J2ME网络编程问题!
- 请问,面向对像的思想,怎样建立?怎样把面向对像的思想用在程序设计中?谢谢了
- 请问:使用ServerSocket如何实现在服务器端的监听多个端口?
- 如何将String转为int类型
- 为和我写的java程序会占用全部的cpu资源,如何做到尽量少占资源?
- 請問Borland Enterprise Server5.0在那裡有下載呢?JBuilder6.0有自帶的嗎?
- 我的程序中子类为什么不能代替父类进行参数传递?
- 在构造方法中调用被子类重写的方法抛出异常
- 初学者关于变量的问题
cardLayout = new CardLayout();
frame.setLayout(cardLayout);frame.add(panel1, "第1张");
frame.add(panel2, "第2张");
frame.add(panel3, "第3张");
呵呵,你可以去www.itzcn.com网站上看看,还有免费的Java教学视频呢