[英]JPanel on CardLayout Swing Java
I am new to Java and I am making a navigation through CardLayout
in Swing. 我是Java新手,正在通过Swing中的
CardLayout
进行导航。 Basically I have two buttons on a JFrame
and when I click on one button it should go to card 1 where I have kept two buttons on JPanel
, card1
and when I click on another button it should go to card 2 where I have kept a JTextField
on panel card2
. 基本上,我在
JFrame
上有两个按钮,当我单击一个按钮时,它应该转到卡1,其中我在JPanel
, card1
上保留了两个按钮,而当我单击另一个按钮时,它应该转到已保留JTextField
卡2。上面板card2
。 But it is not happening. 但这没有发生。
Can anyone fix it? 有人可以修复它吗?
My code is as below. 我的代码如下。
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class CardLayoutTest extends JFrame implements ActionListener {
JFrame f;
JButton b;
JButton c;
JPanel panel;
JPanel cards;
JPanel card1;
JPanel card2;
CardLayout card;
Container pane;
final String card1Text = "One";
final String card2Text = "Two";
CardLayoutTest() {
}
public void passBtn() {
f=new JFrame("Card Layout Test");
card1 = new JPanel();
card1.add(new JButton("Button 1 - Card 1"));
card1.add(new JButton("Button 2 - Card 1"));
card1.setBackground(new Color(255,0,0));
card2 = new JPanel();
card2.add(new JTextField("TextField on Card 2", 20));
card2.setBackground(new Color(0,255,0));
//Create the panel that contains the "cards".
cards = new JPanel(new CardLayout());
cards.add(card1, card1Text);
cards.add(card2, card2Text);
b = new JButton("Page 1");
b.setBounds(50,50,70,30);
b.setBackground(Color.red);
c = new JButton("Page 2");
c.setBounds(50,80,70,30);
c.setBackground(Color.blue);
pane = f.getContentPane();
pane.add(cards, BorderLayout.CENTER);
b.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e) {
card.show(card1, card1Text);
}
});
c.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e) {
card.show(card2, card2Text);
}
});
f.add(b); f.add(c); f.add(panel); f.add(cards);
}
public static void main(String[] args) {
new CardLayoutTest();
}
}
Try: 尝试:
card = new CardLayout();
cards = new JPanel(card);
instead of cards = new JPanel(new CardLayout());
而不是
cards = new JPanel(new CardLayout());
UPDATE UPDATE
public class CardLayoutTest extends JFrame {
JButton b;
JButton c;
JPanel cards;
JPanel card1;
JPanel card2;
CardLayout card;
final String card1Text = "One";
final String card2Text = "Two";
CardLayoutTest() {
super();
setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
passBtn();
}
public void passBtn() {
card1 = new JPanel();
card1.setBackground(new Color(255,0,0));
card2 = new JPanel();
card2.add(new JTextField("TextField on Card 2", 20));
card2.setBackground(new Color(0, 255, 0));
//Create the panel that contains the "cards".
card = new CardLayout();
cards = new JPanel(card);
cards.add(card1, card1Text);
cards.add(card2, card2Text);
b = new JButton("Page 1");
b.setBounds(50, 50, 70, 30);
b.setBackground(Color.red);
card1.add(b);
c = new JButton("Page 2");
c.setBounds(50, 80, 70, 30);
c.setBackground(Color.blue);
card1.add(c);
add(cards);
card.show(cards, card1Text);
b.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
try {
card.show(cards, card1Text);
} catch (Exception e1) {
e1.printStackTrace();
}
}
});
c.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
try {
card.show(cards, card2Text);
} catch (Exception e1) {
e1.printStackTrace();
}
}
});
}
public static void main(String[] args) {
CardLayoutTest cardLayoutTest = new CardLayoutTest();
cardLayoutTest.setVisible(true);
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.