Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package QuizFIve;
- import java.awt.*;
- import java.awt.event.*;
- import javax.swing.*;
- class OptionsPanel extends JPanel implements ActionListener
- {
- private JLabel quote;
- private JRadioButton comedy, philosophy, compsci;
- private String comedyQuote, philosophyQuote, compSciQuote;
- //-----------------------------------------------------------------
- // Sets up a panel with a label and a set of radio buttons
- // that control its text.
- //-----------------------------------------------------------------
- public OptionsPanel(String cq, String pq, String csq)
- {
- comedyQuote = cq;
- philosophyQuote = pq;
- compSciQuote = csq;
- quote = new JLabel(comedyQuote);
- quote.setFont(new Font("Helvetica", Font.BOLD, 24));
- comedy = new JRadioButton("Comedy", true);
- comedy.setBackground(Color.green);
- philosophy = new JRadioButton("Philosophy");
- philosophy.setBackground(Color.green);
- compsci = new JRadioButton("Comp Sci");
- compsci.setBackground(Color.cyan);
- ButtonGroup group = new ButtonGroup();
- group.add(comedy);
- group.add(philosophy);
- group.add(compsci);
- comedy.addActionListener(this);
- philosophy.addActionListener(this);
- compsci.addActionListener(this);
- add(quote);
- add(comedy);
- add(philosophy);
- add(compsci);
- setBackground(Color.green);
- setPreferredSize(new Dimension(500, 100));
- }
- public void actionPerformed(ActionEvent event)
- {
- Object source = event.getSource();
- if (source == comedy) {
- quote.setText(comedyQuote);
- setBackground(Color.pink);
- }
- else if (source == philosophy) {
- quote.setText(philosophyQuote);
- setBackground(Color.yellow);
- } else {
- quote.setText(compSciQuote);
- setBackground(Color.cyan);
- }
- }
- }
- public class TwoRadioButtons {
- public static void main(String[] args)
- {
- JFrame frame = new JFrame("Quote Options");
- frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
- OptionsPanel P = new OptionsPanel("Where there's a will, there's a relative.","You cannot step in the same river twice.","Programmed to work and not to feel.");
- frame.getContentPane().add(P);
- frame.pack();
- frame.setVisible(true);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement