Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.awt.Color;
- import java.awt.event.ActionEvent;
- import java.awt.event.ActionListener;
- import javax.swing.BorderFactory;
- import javax.swing.JButton;
- import javax.swing.JFrame;
- import javax.swing.JPanel;
- import javax.swing.JTextArea;
- public class DisplayString extends JPanel implements ActionListener
- {
- //Static/Final Variables
- private static final long serialVersionUID = 1L;
- private static final int WIDTH = 640;
- private static final int HEIGHT = 480;
- //Instance Variables
- private JButton submit;
- private JTextArea result;
- private JTextArea sentence;
- //Main
- public static void main(String args[])
- {
- //Create Frame and assign values.
- JFrame frame = new JFrame("Displaying String in Uppercase and Lowercase");
- frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
- frame.setLayout(null);
- frame.setContentPane(new DisplayString());
- frame.setSize(WIDTH, HEIGHT);
- frame.setResizable(false);
- frame.setVisible(true);
- }
- //Constructor
- public DisplayString()
- {
- init();
- add();
- }
- //Initialization
- public void init()
- {
- //Variable Initialization
- submit = new JButton("Submit");
- submit.setBounds(16, 400, WIDTH-40, 40);
- submit.addActionListener(this);
- result = new JTextArea();
- result.setBorder(BorderFactory.createLineBorder(Color.GRAY));
- result.setEditable(false);
- result.setOpaque(true);
- result.setBackground(Color.WHITE);
- result.setBounds(16, 96, WIDTH-40, HEIGHT-(96*2));
- sentence = new JTextArea();
- sentence.setBorder(BorderFactory.createLineBorder(Color.BLACK));
- sentence.setOpaque(true);
- sentence.setBackground(Color.WHITE);
- sentence.setBounds(16, 16, WIDTH-40, 64);
- //JPane Settings
- setLayout(null);
- setBackground(Color.LIGHT_GRAY);
- }
- //Add in buttons to the frame.
- public void add()
- {
- add(submit);
- add(result);
- add(sentence);
- }
- //When an action is performed.
- @Override
- public void actionPerformed(ActionEvent e)
- {
- //If the submit button is clicked.
- if (e.getSource() == submit)
- {
- displayString();
- }
- }
- //Displays the string in the text box on the JLabel
- public void displayString()
- {
- //Variables
- String str = sentence.getText();
- //Clear the text field
- sentence.setText("");
- //Add the upper and lowercase strings to the JLabel
- result.setText(String.format("%s%n%n%s%n%n", str.toUpperCase(), str.toLowerCase()));
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement