Advertisement
Guest User

Untitled

a guest
Mar 26th, 2019
78
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.66 KB | None | 0 0
  1. package checkboxtest;
  2.  
  3. import java.awt.*;
  4. import java.awt.event.*;
  5. import javax.swing.*;
  6.  
  7. public class CheckBoxFrame extends JFrame {
  8.  
  9. private final JTextField textField;
  10. private final JCheckBox boldJCheckBox;
  11. private final JCheckBox italicJCheckBox;
  12.  
  13. public CheckBoxFrame() {
  14.  
  15. super( "JCheckBox Test" );
  16. setLayout( new FlowLayout() );
  17.  
  18. textField = new JTextField( "Watch the font style change", 20 );
  19. italicJCheckBox = new JCheckBox( "Italic" );
  20. textField.setFont( new Font( "Serif", Font.PLAIN, 14));
  21. add( textField );
  22. boldJCheckBox = new JCheckBox( "Bold" );
  23. add( boldJCheckBox );
  24. add( italicJCheckBox );
  25. CheckBoxHandler handler = new CheckBoxHandler();
  26. boldJCheckBox.addItemListener( handler );
  27. italicJCheckBox.addItemListener( handler );
  28.  
  29. }
  30.  
  31. private class CheckBoxHandler implements ItemListener{
  32. @Override
  33. public void itemStateChanged( ItemEvent event ){
  34. Font font = null;
  35. if (boldJCheckBox.isSelected () && italicJCheckBox.isSelected()){
  36. font = new Font ("Serif", Font.BOLD + Font.ITALIC, 14);
  37. } else {
  38. if (boldJCheckBox.isSelected()) {
  39. font = new Font ("Serif", Font.BOLD, 14);
  40. } else {
  41. if (italicJCheckBox.isSelected()) {
  42. font = new Font ("Serif", Font.ITALIC, 14);
  43. } else {
  44. font = new Font ("Serif", Font.PLAIN, 14);
  45. }
  46. }
  47. }
  48. }
  49. }
  50. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement