Skip to content

Instantly share code, notes, and snippets.

@parasj
Last active April 14, 2016 16:57
Show Gist options
  • Save parasj/5df9dcebee84eebc8a28 to your computer and use it in GitHub Desktop.
Save parasj/5df9dcebee84eebc8a28 to your computer and use it in GitHub Desktop.
Swing Test to learn Java GUIs - GUI for string reverser https://www.dropbox.com/s/ib6fk9pmhyua2jb/Screenshot%202014-08-18%2012.31.10.png
package com.jainlabs;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.*;
public class SwingTest {
private JFrame frame;
private JLabel result;
private JTextField textfield;
private JButton button;
public SwingTest() {
frame = new JFrame("String Reverser");
frame.setLayout(new BorderLayout());
result = new JLabel("Result: ");
textfield = new JTextField("Enter String Here");
button = new JButton("Reverse");
button.addActionListener(new ReverseListener(result, textfield));
frame.add(textfield, BorderLayout.WEST);
frame.add(button, BorderLayout.EAST);
frame.add(result, BorderLayout.SOUTH);
frame.setSize(200,200);
frame.setVisible(true);
}
public static void main(String[] args) {
SwingTest swingtest = new SwingTest();
}
}
class ReverseListener implements ActionListener {
private JLabel label;
private JTextField textfield;
public ReverseListener(JLabel label, JTextField textfield) {
this.label = label;
this.textfield = textfield;
}
public void actionPerformed(ActionEvent e) {
String s1 = textfield.getText();
String s2 = "";
for (int i = s1.length() - 1; i >= 0; i--) {
s2 += s1.charAt(i);
}
label.setText(s2);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment