carddemo
package myprojects.cardlayoutdemo;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
/**
* @(#)CardLayoutDemo.java
*
* JFC Sample application
*
* @author
* @version 1.00 09/04/03
*/
public class CardLayoutDemoFrame extends JFrame implements ActionListener
{
/**
* The constructor.
*/
JPanel p;
JPanel p1,p2,p3,p4;
JRadioButton bt1,bt2,bt3;
JButton btNext,btPre;
CardLayout card = new CardLayout();
ButtonGroup bg;
public CardLayoutDemoFrame()
{
this.setLayout(new FlowLayout());
p = new JPanel(card);
p1 = new JPanel();
p2 = new JPanel();
p3 = new JPanel();
p4 = new JPanel();
bt1 = new JRadioButton("A");
bt2 = new JRadioButton("B");
bt3 = new JRadioButton("C");
bg = new ButtonGroup();
bg.add(bt1);
bg.add(bt2);
bg.add(bt3);
p1.add(bt1);
p1.add(bt2);
p1.add(bt3);
p1.setBorder(BorderFactory.createTitledBorder("Cau 1"));
bt1 = new JRadioButton("A");
bt2 = new JRadioButton("B");
bt3 = new JRadioButton("C");
bg = new ButtonGroup();
bg.add(bt1);
bg.add(bt2);
bg.add(bt3);
p2.add(bt1);
p2.add(bt2);
p2.add(bt3);
p2.setBorder(BorderFactory.createTitledBorder("Cau 2"));
bt1 = new JRadioButton("A");
bt2 = new JRadioButton("B");
bt3 = new JRadioButton("C");bg = new ButtonGroup();
bg.add(bt1);
bg.add(bt2);
bg.add(bt3);
p3.add(bt1);
p3.add(bt2);
p3.add(bt3);
p3.setBorder(BorderFactory.createTitledBorder("Cau 3"));
bt1 = new JRadioButton("A");
bt2 = new JRadioButton("B");
bt3 = new JRadioButton("C");
bg = new ButtonGroup();
bg.add(bt1);
bg.add(bt2);
bg.add(bt3);
p4.add(bt1);
p4.add(bt2);
p4.add(bt3);
p4.setBorder(BorderFactory.createTitledBorder("Cau 4"));
p.add(p1,"Cau 1");p.add(p2,"Cau 2");p.add(p3,"Cau 3");p.add(p4,"Cau 4");
this.getContentPane().add(p);
btNext = new JButton("Next");
btNext.addActionListener(this);
btPre = new JButton("Pre");
btPre.addActionListener(this);
this.getContentPane().add(btNext);
this.getContentPane().add(btPre);
JMenuBar menuBar = new JMenuBar();
JMenu menuFile = new JMenu();
JMenuItem menuFileExit = new JMenuItem();
menuFile.setText("File");
menuFileExit.setText("Exit");
// Add action listener.for the menu button
menuFileExit.addActionListener
(
new ActionListener() {
public void actionPerformed(ActionEvent e) {
CardLayoutDemoFrame.this.windowClosed();
}
}
);
menuFile.add(menuFileExit);
menuBar.add(menuFile);
setTitle("CardLayoutDemoFrame");
setJMenuBar(menuBar);
setSize(new Dimension(400, 400));
// Add window listener.
this.addWindowListener
(
new WindowAdapter() {
public void windowClosing(WindowEvent e) {
CardLayoutDemoFrame.this.windowClosed();
}
}
);
}
public void actionPerformed(ActionEvent ae)
{
if(ae.getSource() == btNext)
{
card.next(p);
}
else if(ae.getSource() == btPre)
{
card.previous(p);
}
}
/**
* Shutdown procedure when run as an application.
*/
protected void windowClosed() {
// TODO: Check if it is safe to close the application
// Exit application.
System.exit(0);
}
}
Bạn đang đọc truyện trên: AzTruyen.Top