JAVA CardLayout布局问题
JAVA,新建了一个窗体,用了一个循环在窗体中添加了4个JPanel,又用了一个循环在JPanel上添加了4个按钮,用CardLayout布局,给按钮添加事件,当单击按钮...
JAVA,新建了一个窗体,用了一个循环在窗体中添加了4个JPanel,又用了一个循环在JPanel上添加了4个按钮,用CardLayout布局,给按钮添加事件,当单击按钮时显示下一个JPanel,但最后一行代码不知怎么改,无法实现预期效果,求解!
import java.awt.Frame;
import java.awt.CardLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
public class CardLayoutUse extends JFrame implements ActionListener{
private JPanel p;
private CardLayout cl;
public CardLayoutUse(){
CardLayout cl = new CardLayout();
setLayout(cl);
JPanel p[] = new JPanel[4];
for(int i = 0; i < 4; i++){
p[i] = new JPanel();
add(p [i],p[i]);
}
JButton b[] = new JButton[4];
for(int i = 0; i < 4; i++){
int a = i + 1;
b[i] = new JButton("第" + a + "页,下一页");
b[i].addActionListener(this);
p[i].add(b[i]);
}
setVisible(true);
setBounds(300, 300, 300, 300);
}
public static void main(String[] args) {
new CardLayoutUse();
}
@Override
public void actionPerformed(ActionEvent arg0) {
for(int i = 0; i < 4; i++){
cl.show(this,p[i]);
}
}
} 展开
import java.awt.Frame;
import java.awt.CardLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
public class CardLayoutUse extends JFrame implements ActionListener{
private JPanel p;
private CardLayout cl;
public CardLayoutUse(){
CardLayout cl = new CardLayout();
setLayout(cl);
JPanel p[] = new JPanel[4];
for(int i = 0; i < 4; i++){
p[i] = new JPanel();
add(p [i],p[i]);
}
JButton b[] = new JButton[4];
for(int i = 0; i < 4; i++){
int a = i + 1;
b[i] = new JButton("第" + a + "页,下一页");
b[i].addActionListener(this);
p[i].add(b[i]);
}
setVisible(true);
setBounds(300, 300, 300, 300);
}
public static void main(String[] args) {
new CardLayoutUse();
}
@Override
public void actionPerformed(ActionEvent arg0) {
for(int i = 0; i < 4; i++){
cl.show(this,p[i]);
}
}
} 展开
4个回答
展开全部
你的程序我帮你改了一下,现在运行没问题了,你看看吧。
import java.awt.CardLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
public class CardLayoutUse extends JFrame implements ActionListener{
private JPanel p1;
private CardLayout cl;
private int j=1;
public CardLayoutUse(){
p1=new JPanel();
cl = new CardLayout();
p1.setLayout(cl);
JPanel p[] = new JPanel[4];
for(int i = 0; i < 4; i++){
p[i] = new JPanel();
p1.add(p[i],String.valueOf(i));
}
JButton b[] = new JButton[4];
for(int i = 0; i < 4; i++){
int a = i + 1;
b[i] = new JButton("第" + a + "页,下一页");
b[i].addActionListener(this);
p[i].add(b[i]);
}
add(p1);
setVisible(true);
setBounds(300, 300, 300, 300);
}
public static void main(String[] args) {
new CardLayoutUse();
}
public void actionPerformed(ActionEvent arg0) {
cl.show(p1,String.valueOf(j++));
if(j==4) j=0;
}
}
import java.awt.CardLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
public class CardLayoutUse extends JFrame implements ActionListener{
private JPanel p1;
private CardLayout cl;
private int j=1;
public CardLayoutUse(){
p1=new JPanel();
cl = new CardLayout();
p1.setLayout(cl);
JPanel p[] = new JPanel[4];
for(int i = 0; i < 4; i++){
p[i] = new JPanel();
p1.add(p[i],String.valueOf(i));
}
JButton b[] = new JButton[4];
for(int i = 0; i < 4; i++){
int a = i + 1;
b[i] = new JButton("第" + a + "页,下一页");
b[i].addActionListener(this);
p[i].add(b[i]);
}
add(p1);
setVisible(true);
setBounds(300, 300, 300, 300);
}
public static void main(String[] args) {
new CardLayoutUse();
}
public void actionPerformed(ActionEvent arg0) {
cl.show(p1,String.valueOf(j++));
if(j==4) j=0;
}
}
展开全部
同求!
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
NullPointerException!
看下这个有用没:
JButton jb = (JButton)e.getSource();
JPanel pel = (JPanel)jb.getParent();
看下这个有用没:
JButton jb = (JButton)e.getSource();
JPanel pel = (JPanel)jb.getParent();
本回答被网友采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
main语句,好像不是这样的吧
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询