java--卡片布局管理
importjava.awt.Button;importjava.awt.CardLayout;importjava.awt.Frame;publicclassNewCa...
import java.awt.Button;
import java.awt.CardLayout;
import java.awt.Frame;
publicclass NewCardLayout {
publicstaticvoid main (String[] args){
Frame f = new Frame ("example");
f.setLayout(new CardLayout());
f.add( new Button("one"), "card1");
f.add( new Button("two"), "card2");
f.setBounds(100,100,250,250);
f.setVisible(true);
}
}
可以帮我看看并加点什么。。现在只能出现one,是不是应该点击会出现two丫,怎么修改丫。谢啦 展开
import java.awt.CardLayout;
import java.awt.Frame;
publicclass NewCardLayout {
publicstaticvoid main (String[] args){
Frame f = new Frame ("example");
f.setLayout(new CardLayout());
f.add( new Button("one"), "card1");
f.add( new Button("two"), "card2");
f.setBounds(100,100,250,250);
f.setVisible(true);
}
}
可以帮我看看并加点什么。。现在只能出现one,是不是应该点击会出现two丫,怎么修改丫。谢啦 展开
5个回答
展开全部
我看了你的程序,帮你改完了,你看看吧。
import java.awt.BorderLayout;
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 NewCardLayout {
public static void main (String[] args){
Frame f = new Frame ("example");
final Panel p=new Panel();
final CardLayout cl=new CardLayout();
p.setLayout(cl);
Panel p1=new Panel();
p1.setLayout(new BorderLayout());
Panel p2=new Panel();
p2.setLayout(new BorderLayout());
Button b1=new Button("one");
b1.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent ae) {
cl.show(p,"card2");
}
});
p1.add(b1,BorderLayout.CENTER);
Button b2=new Button("two");
b2.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent ae) {
cl.show(p,"card1");
}
});
p2.add(b2,BorderLayout.CENTER);
p.add(p1, "card1");
p.add(p2, "card2");
f.add(p);
f.setBounds(100,100,250,250);
f.setVisible(true);
f.addWindowListener(new WindowAdapter(){
public void windowClosing(WindowEvent we){
System.exit(0);
}});
}
}
import java.awt.BorderLayout;
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 NewCardLayout {
public static void main (String[] args){
Frame f = new Frame ("example");
final Panel p=new Panel();
final CardLayout cl=new CardLayout();
p.setLayout(cl);
Panel p1=new Panel();
p1.setLayout(new BorderLayout());
Panel p2=new Panel();
p2.setLayout(new BorderLayout());
Button b1=new Button("one");
b1.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent ae) {
cl.show(p,"card2");
}
});
p1.add(b1,BorderLayout.CENTER);
Button b2=new Button("two");
b2.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent ae) {
cl.show(p,"card1");
}
});
p2.add(b2,BorderLayout.CENTER);
p.add(p1, "card1");
p.add(p2, "card2");
f.add(p);
f.setBounds(100,100,250,250);
f.setVisible(true);
f.addWindowListener(new WindowAdapter(){
public void windowClosing(WindowEvent we){
System.exit(0);
}});
}
}
展开全部
修改的真好
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
是啊修改的真好!
真好丫!!!
但是看不懂
真好丫!!!
但是看不懂
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
试试这个
import java.awt.CardLayout;
import java.awt.FlowLayout;
import java.awt.TextField;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
public class CardLayoutExample extends JFrame {
static CardLayout Card = new CardLayout(5, 5);
static JPanel JPane1 = new JPanel();
static JPanel JPane3 = new JPanel(Card);
JButton b1 = new JButton("切换2");
JButton b2 = new JButton("切换1");
public void Test() {
JPane1.add(new TextField("真实的面板,不可缺少,否则第一次会覆盖"));
JPane3.add(JPane1, "jp1");
b1.addActionListener(new MyEvent());
b2.addActionListener(new MyEvent());
}
/** 监听类 */
class MyEvent implements ActionListener {
public void actionPerformed(ActionEvent e) {
if("切换1".equals(e.getActionCommand())){
JPanel panel = new JPanel();
panel.add(new TextField("面版1"));
JPane3.add(panel,"jp2");Card.show(JPane3, "jp2");
}else{
JPanel JPane2 = new JPanel();
JPane2.add(new TextField("面板2"));
JPane3.add(JPane2, "jp2");
Card.show(JPane3, "jp2");
}
}
}
public static void main(String[] args) {
CardLayoutExample test = new CardLayoutExample();
test.Test();
test.add(JPane3);
test.add(test.b2);
test.add(test.b1);
test.setLayout(new FlowLayout());
test.setSize(500, 500);
test.setVisible(true);
test.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
}
import java.awt.CardLayout;
import java.awt.FlowLayout;
import java.awt.TextField;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
public class CardLayoutExample extends JFrame {
static CardLayout Card = new CardLayout(5, 5);
static JPanel JPane1 = new JPanel();
static JPanel JPane3 = new JPanel(Card);
JButton b1 = new JButton("切换2");
JButton b2 = new JButton("切换1");
public void Test() {
JPane1.add(new TextField("真实的面板,不可缺少,否则第一次会覆盖"));
JPane3.add(JPane1, "jp1");
b1.addActionListener(new MyEvent());
b2.addActionListener(new MyEvent());
}
/** 监听类 */
class MyEvent implements ActionListener {
public void actionPerformed(ActionEvent e) {
if("切换1".equals(e.getActionCommand())){
JPanel panel = new JPanel();
panel.add(new TextField("面版1"));
JPane3.add(panel,"jp2");Card.show(JPane3, "jp2");
}else{
JPanel JPane2 = new JPanel();
JPane2.add(new TextField("面板2"));
JPane3.add(JPane2, "jp2");
Card.show(JPane3, "jp2");
}
}
}
public static void main(String[] args) {
CardLayoutExample test = new CardLayoutExample();
test.Test();
test.add(JPane3);
test.add(test.b2);
test.add(test.b1);
test.setLayout(new FlowLayout());
test.setSize(500, 500);
test.setVisible(true);
test.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
}
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询