Advertisement
Guest User

Untitled

a guest
Apr 19th, 2019
116
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.48 KB | None | 0 0
  1. package QuizFIve;
  2. import java.awt.*;
  3. import java.awt.event.*;
  4. import javax.swing.*;
  5. class OptionsPanel extends JPanel implements ActionListener
  6. {
  7. private JLabel quote;
  8. private JRadioButton comedy, philosophy, compsci;
  9. private String comedyQuote, philosophyQuote, compSciQuote;
  10.  
  11. //-----------------------------------------------------------------
  12. // Sets up a panel with a label and a set of radio buttons
  13. // that control its text.
  14. //-----------------------------------------------------------------
  15. public OptionsPanel(String cq, String pq, String csq)
  16. {
  17. comedyQuote = cq;
  18. philosophyQuote = pq;
  19. compSciQuote = csq;
  20.  
  21. quote = new JLabel(comedyQuote);
  22. quote.setFont(new Font("Helvetica", Font.BOLD, 24));
  23. comedy = new JRadioButton("Comedy", true);
  24. comedy.setBackground(Color.green);
  25. philosophy = new JRadioButton("Philosophy");
  26. philosophy.setBackground(Color.green);
  27. compsci = new JRadioButton("Comp Sci");
  28. compsci.setBackground(Color.cyan);
  29.  
  30.  
  31. ButtonGroup group = new ButtonGroup();
  32. group.add(comedy);
  33. group.add(philosophy);
  34. group.add(compsci);
  35.  
  36.  
  37. comedy.addActionListener(this);
  38. philosophy.addActionListener(this);
  39. compsci.addActionListener(this);
  40.  
  41. add(quote);
  42. add(comedy);
  43. add(philosophy);
  44. add(compsci);
  45.  
  46.  
  47. setBackground(Color.green);
  48. setPreferredSize(new Dimension(500, 100));
  49. }
  50.  
  51. public void actionPerformed(ActionEvent event)
  52. {
  53. Object source = event.getSource();
  54.  
  55. if (source == comedy) {
  56. quote.setText(comedyQuote);
  57. setBackground(Color.pink);
  58. }
  59. else if (source == philosophy) {
  60. quote.setText(philosophyQuote);
  61. setBackground(Color.yellow);
  62. } else {
  63. quote.setText(compSciQuote);
  64. setBackground(Color.cyan);
  65. }
  66. }
  67. }
  68.  
  69.  
  70. public class TwoRadioButtons {
  71. public static void main(String[] args)
  72. {
  73. JFrame frame = new JFrame("Quote Options");
  74. frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  75. 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.");
  76. frame.getContentPane().add(P);
  77.  
  78. frame.pack();
  79. frame.setVisible(true);
  80. }
  81. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement