1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 | import javax.swing.*; import java.awt.*; import java.awt.event.*; public class JTabbedPaneDemo extends JFrame{ public static void main(String[] args){ JTabbedPaneDemo ap=new JTabbedPaneDemo(); } public JTabbedPaneDemo(){ super("Electronics Tutorial"); setSize(640,120); JTabbedPane tab=new JTabbedPane(); JPanel panel1=new JPanel(); JLabel l1=new JLabel("Resistors are designed to oppose the flow of current"); panel1.add(l1); tab.addTab("Resistor", panel1); tab.setSelectedIndex(0); JPanel panel2=new JPanel(); JLabel l2=new JLabel("Capacitors temporarily store energy as an electric field"); panel2.add(l2); tab.addTab("Capacitor",panel2); JPanel panel3=new JPanel(); JLabel l3=new JLabel("Inductors temporarily store energy as a magnetic field"); panel2.add(l2); tab.addTab("Inductor",panel3); getContentPane().add(tab); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); show(); } } |
Here is the result:
Below, JTabbedPane is utilized to create a simple quiz. Just like the program above, there are three tabs on the window. Each contains a different question.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 | ===== import javax.swing.*; import javax.swing.event.*; import java.awt.*; import java.awt.event.*; public class JTabbedPaneDemo2 extends JFrame{ JTabbedPane tab; JPanel panel1,panel2,panel3; ButtonGroup g1; JRadioButton rb1a,rb1b,rb1c; ButtonGroup g2; JRadioButton rb2a,rb2b,rb2c; ButtonGroup g3; JRadioButton rb3a,rb3b,rb3c; JButton b1,b2,b3; public static void main(String[] args){ JTabbedPaneDemo2 ap=new JTabbedPaneDemo2(); } public JTabbedPaneDemo2(){ super("Electronics Quiz"); setSize(640,120); tab=new JTabbedPane(); panel1=new JPanel(); JLabel l1=new JLabel("The frequency of common household AC in US is:"); g1=new ButtonGroup(); rb1a=new JRadioButton("120Hz",false); rb1b=new JRadioButton("60Hz",false); rb1c=new JRadioButton("100Hz",false); g1.add(rb1a); g1.add(rb1b); g1.add(rb1c); panel1.add(l1); panel1.add(rb1a); panel1.add(rb1b); panel1.add(rb1c); b1=new JButton("Submit"); panel1.add(b1); tab.addTab("Level1", panel1); panel2=new JPanel(); JLabel l2=new JLabel("Ohm is the standard unit of:"); g2=new ButtonGroup(); rb2a=new JRadioButton("Current",false); rb2b=new JRadioButton("Charge",false); rb2c=new JRadioButton("Resistance",false); g2.add(rb2a); g2.add(rb2b); g2.add(rb2c); panel2.add(l2); panel2.add(rb2a); panel2.add(rb2b); panel2.add(rb2c); b2=new JButton("Submit"); panel2.add(b2); tab.addTab("Level2", panel2); panel3=new JPanel(); JLabel l3=new JLabel("A vector has (Choose the wrong option):"); g3=new ButtonGroup(); rb3a=new JRadioButton("Magnitude",false); rb3b=new JRadioButton("Direction",false); rb3c=new JRadioButton("Inductance",false); g3.add(rb3a); g3.add(rb3b); g3.add(rb3c); panel3.add(l3); panel3.add(rb3a); panel3.add(rb3b); panel3.add(rb3c); b3=new JButton("Submit"); panel3.add(b3); tab.addTab("Level3", panel3); tab.setEnabledAt(0,true); tab.setEnabledAt(1,false); tab.setEnabledAt(2,false); ButtonHandler handler=new ButtonHandler(); b1.addActionListener(handler); b2.addActionListener(handler); b3.addActionListener(handler); getContentPane().add(tab); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); show(); } class ButtonHandler implements ActionListener{ public void actionPerformed(ActionEvent e){ if(e.getSource()==b1&&rb1a.isSelected()){ tab.setEnabledAt(1,true); } if(e.getSource()==b2&&rb2c.isSelected()){ tab.setEnabledAt(2,true); } if(e.getSource()==b3&&rb3c.isSelected()){ CongratDialog cd=new CongratDialog(null); cd.show(); } } } class CongratDialog extends JDialog { public CongratDialog(JFrame parent) { super(parent, "Result", true); Container cp = getContentPane(); cp.setLayout(new FlowLayout()); cp.add(new JLabel("Congratulation!")); JButton ok = new JButton("OK"); ok.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { dispose(); } }); cp.add(ok); setSize(150,100); } } } |
tab.setEnabledAt(0,true); //Only the first tab is enabled initially tab.setEnabledAt(1,false); tab.setEnabledAt(2,false);
Initially, the second and the third tabs are not enabled. They cannot respond to user input.
if(e.getSource()==b1&&rb1a.isSelected()){ tab.setEnabledAt(1,true); }
When the user selects the correct answer, the second tab will be enabled.
if(e.getSource()==b2&&rb2c.isSelected()){ tab.setEnabledAt(2,true); }
When the user chooses the correct answer, the thids tab will be enabled.
if(e.getSource()==b3&&rb3c.isSelected()){ CongratDialog cd=new CongratDialog(null); cd.show(); }
When the right option on the third tab is selected, a dialog box will be displayed.
No comments:
Post a Comment