import javax.swing.*;
import java.awt.event.*;
import java.awt.*;
public class NewClass2 extends JFrame{
JButton jButton1;
JButton jButton2;
JTextArea jTextArea;
public NewClass2()
{
jButton1 = new JButton("JButton1");
jButton2 = new JButton("JButton2");
jTextArea = new JTextArea(null,"jtextarea",5,20);
this.add(jButton1);
this.add(jButton2);
this.add(jTextArea);
jButton1.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
jTextArea.setText("jButton1");
}
});
jButton2.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
jTextArea.setText("jButton2");
}
});
this.setLayout(new FlowLayout());
this.setBounds(200, 100, 300, 170);
this.setVisible(true);
}
public static void main(String[] args)
{
new NewClass2();
}
}
下面的位置使用CardLayout切换。
下面可以使用CardLayout点击某个按钮就将去相应的卡片显示出来