Advertisement
Guest User

DisplayString

a guest
Feb 14th, 2016
46
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.41 KB | None | 0 0
  1. import java.awt.Color;
  2. import java.awt.event.ActionEvent;
  3. import java.awt.event.ActionListener;
  4.  
  5. import javax.swing.BorderFactory;
  6. import javax.swing.JButton;
  7. import javax.swing.JFrame;
  8. import javax.swing.JPanel;
  9. import javax.swing.JTextArea;
  10.  
  11. public class DisplayString extends JPanel implements ActionListener
  12. {
  13. //Static/Final Variables
  14. private static final long serialVersionUID = 1L;
  15. private static final int WIDTH = 640;
  16. private static final int HEIGHT = 480;
  17.  
  18. //Instance Variables
  19. private JButton submit;
  20. private JTextArea result;
  21. private JTextArea sentence;
  22.  
  23. //Main
  24. public static void main(String args[])
  25. {
  26. //Create Frame and assign values.
  27. JFrame frame = new JFrame("Displaying String in Uppercase and Lowercase");
  28. frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  29. frame.setLayout(null);
  30. frame.setContentPane(new DisplayString());
  31. frame.setSize(WIDTH, HEIGHT);
  32. frame.setResizable(false);
  33. frame.setVisible(true);
  34. }
  35.  
  36. //Constructor
  37. public DisplayString()
  38. {
  39. init();
  40. add();
  41. }
  42.  
  43. //Initialization
  44. public void init()
  45. {
  46. //Variable Initialization
  47. submit = new JButton("Submit");
  48. submit.setBounds(16, 400, WIDTH-40, 40);
  49. submit.addActionListener(this);
  50.  
  51. result = new JTextArea();
  52. result.setBorder(BorderFactory.createLineBorder(Color.GRAY));
  53. result.setEditable(false);
  54. result.setOpaque(true);
  55. result.setBackground(Color.WHITE);
  56. result.setBounds(16, 96, WIDTH-40, HEIGHT-(96*2));
  57.  
  58. sentence = new JTextArea();
  59. sentence.setBorder(BorderFactory.createLineBorder(Color.BLACK));
  60. sentence.setOpaque(true);
  61. sentence.setBackground(Color.WHITE);
  62. sentence.setBounds(16, 16, WIDTH-40, 64);
  63.  
  64. //JPane Settings
  65. setLayout(null);
  66. setBackground(Color.LIGHT_GRAY);
  67. }
  68.  
  69. //Add in buttons to the frame.
  70. public void add()
  71. {
  72. add(submit);
  73. add(result);
  74. add(sentence);
  75. }
  76.  
  77. //When an action is performed.
  78. @Override
  79. public void actionPerformed(ActionEvent e)
  80. {
  81. //If the submit button is clicked.
  82. if (e.getSource() == submit)
  83. {
  84. displayString();
  85. }
  86. }
  87.  
  88. //Displays the string in the text box on the JLabel
  89. public void displayString()
  90. {
  91. //Variables
  92. String str = sentence.getText();
  93.  
  94. //Clear the text field
  95. sentence.setText("");
  96.  
  97. //Add the upper and lowercase strings to the JLabel
  98. result.setText(String.format("%s%n%n%s%n%n", str.toUpperCase(), str.toLowerCase()));
  99. }
  100.  
  101. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement