java布局管理器 cardlayout 70
importjava.awt.*;importjava.awt.event.*;importjavax.swing.*;importjavax.swing.JFrame;...
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.JFrame;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
public class MyGUI extends JFrame implements ActionListener{
private JButton[] button = new JButton[21];
private JButton buttona,buttonb,buttonc,buttond,buttone;
private JPanel p1 = new JPanel();
private JPanel p2 = new JPanel();
private CardLayout cardlayout = new CardLayout();
public MyGUI(){
super("布局演示");
this.setSize(580,200);
p1.setLayout(new FlowLayout(FlowLayout.CENTER,5,5));
for(int i = 0;i < 21;i++){
button[i] = new JButton(""+i);
}
for(int i = 0;i < 21;i++){
p1.add(button[i]);
}
buttona = new JButton("FlowLayout");
buttonb = new JButton("BoxLayoutY");
buttonc = new JButton("BoxLayoutX");
buttond = new JButton("GridLayout");
buttone = new JButton("CardLayout");
this.getContentPane().add(p1);
p2.setLayout(new FlowLayout(FlowLayout.CENTER,5,5));
p2.add(buttona);
p2.add(buttonb);
p2.add(buttonc);
p2.add(buttond);
p2.add(buttone);
this.getContentPane().add(p2,BorderLayout.SOUTH);
buttona.addActionListener(this);
buttonb.addActionListener(this);
buttonc.addActionListener(this);
buttond.addActionListener(this);
buttone.addActionListener(this);
}public void actionPerformed(ActionEvent event) {
if(event.getSource() == buttona){
cardlayout.removeLayoutComponent(getContentPane());
this.getContentPane().remove(p1);
p1 = new JPanel();
for(int i = 0;i < 21;i++){
p1.add(button[i]);
}this.getContentPane().add(p1);
p1.setLayout(new FlowLayout(FlowLayout.CENTER,5,5));
this.validate();}
if(event.getSource() == buttonb){
this.getContentPane().remove(p1);
p1 = new JPanel();
for(int i = 0;i < 21;i++){
p1.add(button[i]);
}
this.getContentPane().add(p1);
p1.setLayout(new BoxLayout(p1,BoxLayout.Y_AXIS));
this.validate();
}
if(event.getSource() == buttonc){
this.getContentPane().remove(p1);
p1 = new JPanel();
for(int i = 0;i < 21;i++){
p1.add(button[i]);
}
this.getContentPane().add(p1);
p1.setLayout(new BoxLayout(p1,BoxLayout.X_AXIS));
this.validate();
}
if(event.getSource() == buttond){
this.getContentPane().remove(p1);
p1 = new JPanel();
for(int i = 0;i < 21;i++){
p1.add(button[i]);
}
this.getContentPane().add(p1);
p1.setLayout(new GridLayout(4,6));
this.validate();
}
if(event.getSource() == buttone){
this.getContentPane().remove(p1);
p1 = new JPanel(cardlayout);
for(int i = 0;i < 21;i++){
p1.add(button[i]);
}
this.getContentPane().add(p1);
this.validate();
for(int i = 0;i<21;i++){
button[i].addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent arg0) {
cardlayout.next(p1);
}});}}}}
切换到cardlayout后,再切换其他布局,按钮只剩一个了,怎么解决 展开
import java.awt.event.*;
import javax.swing.*;
import javax.swing.JFrame;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
public class MyGUI extends JFrame implements ActionListener{
private JButton[] button = new JButton[21];
private JButton buttona,buttonb,buttonc,buttond,buttone;
private JPanel p1 = new JPanel();
private JPanel p2 = new JPanel();
private CardLayout cardlayout = new CardLayout();
public MyGUI(){
super("布局演示");
this.setSize(580,200);
p1.setLayout(new FlowLayout(FlowLayout.CENTER,5,5));
for(int i = 0;i < 21;i++){
button[i] = new JButton(""+i);
}
for(int i = 0;i < 21;i++){
p1.add(button[i]);
}
buttona = new JButton("FlowLayout");
buttonb = new JButton("BoxLayoutY");
buttonc = new JButton("BoxLayoutX");
buttond = new JButton("GridLayout");
buttone = new JButton("CardLayout");
this.getContentPane().add(p1);
p2.setLayout(new FlowLayout(FlowLayout.CENTER,5,5));
p2.add(buttona);
p2.add(buttonb);
p2.add(buttonc);
p2.add(buttond);
p2.add(buttone);
this.getContentPane().add(p2,BorderLayout.SOUTH);
buttona.addActionListener(this);
buttonb.addActionListener(this);
buttonc.addActionListener(this);
buttond.addActionListener(this);
buttone.addActionListener(this);
}public void actionPerformed(ActionEvent event) {
if(event.getSource() == buttona){
cardlayout.removeLayoutComponent(getContentPane());
this.getContentPane().remove(p1);
p1 = new JPanel();
for(int i = 0;i < 21;i++){
p1.add(button[i]);
}this.getContentPane().add(p1);
p1.setLayout(new FlowLayout(FlowLayout.CENTER,5,5));
this.validate();}
if(event.getSource() == buttonb){
this.getContentPane().remove(p1);
p1 = new JPanel();
for(int i = 0;i < 21;i++){
p1.add(button[i]);
}
this.getContentPane().add(p1);
p1.setLayout(new BoxLayout(p1,BoxLayout.Y_AXIS));
this.validate();
}
if(event.getSource() == buttonc){
this.getContentPane().remove(p1);
p1 = new JPanel();
for(int i = 0;i < 21;i++){
p1.add(button[i]);
}
this.getContentPane().add(p1);
p1.setLayout(new BoxLayout(p1,BoxLayout.X_AXIS));
this.validate();
}
if(event.getSource() == buttond){
this.getContentPane().remove(p1);
p1 = new JPanel();
for(int i = 0;i < 21;i++){
p1.add(button[i]);
}
this.getContentPane().add(p1);
p1.setLayout(new GridLayout(4,6));
this.validate();
}
if(event.getSource() == buttone){
this.getContentPane().remove(p1);
p1 = new JPanel(cardlayout);
for(int i = 0;i < 21;i++){
p1.add(button[i]);
}
this.getContentPane().add(p1);
this.validate();
for(int i = 0;i<21;i++){
button[i].addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent arg0) {
cardlayout.next(p1);
}});}}}}
切换到cardlayout后,再切换其他布局,按钮只剩一个了,怎么解决 展开
展开全部
帮你改了下。
改动比较大。就不贴代码了。
问题出在这里。
private CardLayout cardlayout = new CardLayout();
不应该这么申明。
你初始化成卡片布局。就是扑克牌布局。是一次只显示一个
改为
private LayoutManager cardlayout;
充分利用JAVA的OOP, 申明为所有布局的父类LayoutManager 类型。
下面事件处理的时候,具体再切换布局为具体子类。
所以你程序的设计是不太好。
改动比较大。就不贴代码了。
问题出在这里。
private CardLayout cardlayout = new CardLayout();
不应该这么申明。
你初始化成卡片布局。就是扑克牌布局。是一次只显示一个
改为
private LayoutManager cardlayout;
充分利用JAVA的OOP, 申明为所有布局的父类LayoutManager 类型。
下面事件处理的时候,具体再切换布局为具体子类。
所以你程序的设计是不太好。
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询