import java.awt.*;
import javax.swing.*;
import java.awt.event.*; // for event-handling
public class MyGUI implements ActionListener
{
private JFrame window; // main program window
private JButton okayButton;
private JButton clearButton;
private JTextField input;
private JLabel output;
// Control logic
private Actions a;
public MyGUI()
{
a = new Actions();
}
public void display()
{
window = new JFrame("My First GUI Application");
window.setSize(400, 200); // 400 px wide, 200 px high
window.setLayout(new BorderLayout()); //FlowLayout());
okayButton = new JButton("Okay");
okayButton.addActionListener(this);
clearButton = new JButton("Clear");
clearButton.addActionListener(this);
clearButton.setEnabled(false);
JPanel inputPanel = new JPanel();
input = new JTextField(20); // 20 chars in width
input.setBackground(Color.BLACK);
input.setForeground(Color.RED);
inputPanel.add(input);
inputPanel.setBackground(Color.BLUE);
output = new JLabel("Enter a string to reverse"); // empty label to start
JPanel outputPanel = new JPanel();
outputPanel.add(output);
outputPanel.setBackground(Color.ORANGE);
JPanel buttonPanel = new JPanel();
buttonPanel.setBackground(Color.GREEN);
buttonPanel.add(okayButton);
buttonPanel.add(clearButton);
window.add(BorderLayout.NORTH, inputPanel);
window.add(BorderLayout.CENTER, outputPanel);
window.add(BorderLayout.SOUTH, buttonPanel);
// window.setResizable(false);
window.setVisible(true);
}
// This method is required by ActionListener
public void actionPerformed (ActionEvent e)
{
if (e.getSource() == clearButton)
{
input.setText(""); // clear the input text field
output.setText("Please enter a string to reverse...");
clearButton.setEnabled(false);
}
else if (e.getSource() == okayButton)
{
// Read the current contents of the textfield
String temp = input.getText();
if (temp.length() < 1)
{
output.setText("Enter some text first!");
}
else
{
String reversed = a.reverse(temp);
output.setText("Reversed text: " + reversed);
input.setText("");
clearButton.setEnabled(true);
}
}
}
}