Java中如何实现点击按钮使容器上的一个JPanel被另一个替换
importjava.awt.*;importjava.awt.event.*;importjava.lang.StringBuffer;importjavax.swin...
import java.awt.*;
import java.awt.event.*;
import java.lang.StringBuffer;
import javax.swing.*;
import java.util.*;
import javax.swing.Timer;
import javax.swing.border.*;
class Change extends JFrame implements ActionListener{
int i=1;
Container cp;
JPanel p1,p2,p3,p4,p5,p6;
JButton b1,b2,b3,b4,b5;
JTextArea a,b,c;
public Change(){
super("Calender");
cp=getContentPane();
cp.setLayout(new BorderLayout());
b1=new JButton("按我的话页面动");b2=new JButton("变");
b3=new JButton("在变");b4=new JButton("hello!");b5=new JButton("恢复");
p1=new JPanel();
p2=new JPanel();
p3=new JPanel();
p4=new JPanel();
p5=new JPanel();
p6=new JPanel();
a=new JTextArea("1111"); b=new JTextArea("2222"); c=new JTextArea("3333");
p1.add(b1);p1.add(b2);p1.add(b3); p4.add(a);p5.add(b);p6.add(c);
p3.add(b5);
b1.addActionListener(this);b2.addActionListener(this);b3.addActionListener(this);
cp.add(p1,BorderLayout.NORTH);
cp.add(p2,BorderLayout.CENTER);
cp.add(p3,BorderLayout.SOUTH);
setSize(300,200);
setLocation(320,200);
setVisible(true);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
public void actionPerformed(ActionEvent e)
{
if(e.getSource()==b1);
{cp.add(p4,BorderLayout.CENTER);}
if(e.getSource()==b2);
{cp.add(p5,BorderLayout.CENTER);}
if(e.getSource()==b3);
{cp.add(p6,BorderLayout.CENTER);}
}
public static void main(String args[]){
Change med=new Change();
}
} 展开
import java.awt.event.*;
import java.lang.StringBuffer;
import javax.swing.*;
import java.util.*;
import javax.swing.Timer;
import javax.swing.border.*;
class Change extends JFrame implements ActionListener{
int i=1;
Container cp;
JPanel p1,p2,p3,p4,p5,p6;
JButton b1,b2,b3,b4,b5;
JTextArea a,b,c;
public Change(){
super("Calender");
cp=getContentPane();
cp.setLayout(new BorderLayout());
b1=new JButton("按我的话页面动");b2=new JButton("变");
b3=new JButton("在变");b4=new JButton("hello!");b5=new JButton("恢复");
p1=new JPanel();
p2=new JPanel();
p3=new JPanel();
p4=new JPanel();
p5=new JPanel();
p6=new JPanel();
a=new JTextArea("1111"); b=new JTextArea("2222"); c=new JTextArea("3333");
p1.add(b1);p1.add(b2);p1.add(b3); p4.add(a);p5.add(b);p6.add(c);
p3.add(b5);
b1.addActionListener(this);b2.addActionListener(this);b3.addActionListener(this);
cp.add(p1,BorderLayout.NORTH);
cp.add(p2,BorderLayout.CENTER);
cp.add(p3,BorderLayout.SOUTH);
setSize(300,200);
setLocation(320,200);
setVisible(true);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
public void actionPerformed(ActionEvent e)
{
if(e.getSource()==b1);
{cp.add(p4,BorderLayout.CENTER);}
if(e.getSource()==b2);
{cp.add(p5,BorderLayout.CENTER);}
if(e.getSource()==b3);
{cp.add(p6,BorderLayout.CENTER);}
}
public static void main(String args[]){
Change med=new Change();
}
} 展开
展开全部
用cardlayout布局
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐于2016-11-25
展开全部
使用 CardLayout
CardLayout layout = new CardLayout();
JPanel panel = new JPanel(layout);
JPanel p1 = new JPanel();
JPanel p2 = new JPanel();
panel.add(p1,"p1");
panel.add(p2,"p2");
JButton switchToP1 = new JButton("Switch to P1);
switchToP1.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent evt){
layout.show(panel,"p1");
}
});
CardLayout layout = new CardLayout();
JPanel panel = new JPanel(layout);
JPanel p1 = new JPanel();
JPanel p2 = new JPanel();
panel.add(p1,"p1");
panel.add(p2,"p2");
JButton switchToP1 = new JButton("Switch to P1);
switchToP1.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent evt){
layout.show(panel,"p1");
}
});
本回答被提问者和网友采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询