import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.io.*;
import java.lang.*;
// This example demonstrates the use of JButton, JTextField
// and JLabel.
public class ConvertText implements ActionListener, MouseListener{
JFrame convertTextFrame;
JPanel convertTextPanel;
JTextField enterString;
JLabel enterAString;
JButton upper, lower;
// Constructor
public ConvertText()
{
// Create the frame and container.
convertTextFrame = new JFrame("Convert String");
convertTextPanel = new JPanel();
convertTextPanel.setLayout(new GridLayout(2,2));
// Add the widgets.
addWidgets();
// Add the panel to the frame.
convertTextFrame.getContentPane().add(convertTextPanel, BorderLayout.CENTER);
// Exit when the window is closed.
convertTextFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
// Show the converter.
convertTextFrame.pack();
convertTextFrame.setVisible(true);
upper.addMouseListener( this );
lower.addMouseListener(this);
}
// Create and add the widgets for converter.
private void addWidgets()
{
// Create widgets.
enterAString = new JLabel("Enter A String", SwingConstants.LEFT);
upper = new JButton("Upper");
enterString = new JTextField(2);
lower = new JButton("Lower");
// Listen to events from the buttons.
upper.addActionListener(this);
lower.addActionListener(this);
// Add widgets to container.
convertTextPanel.add(enterAString);
convertTextPanel.add(upper);
convertTextPanel.add(enterString);
convertTextPanel.add(lower);
enterAString.setBorder(BorderFactory.createEmptyBorder(5,5,5,5));
}
// Implementation of ActionListener interface.
public void actionPerformed(ActionEvent event)
{
if (event.getSource() instanceof JButton){
JButton clickedButton = (JButton) event.getSource();
String buttonText = clickedButton.getText();
if (buttonText.equals("Upper")){
for (int i = 0; i < enterString.length; i++) {
enterString[i] = Character.toUpperCase(enterString[i]);
}
}
else {
for (int i = 0; i < enterString.length; i++) {
enterString[i] = Character.toLowerCase(enterString[i]);
}
}
}
else {}
}
public void mouseClicked(MouseEvent e){}
public void mouseEntered(MouseEvent e){}
public void mouseExited(MouseEvent e){ }
public void mouseReleased(MouseEvent e){}
public void mousePressed(MouseEvent e){ }
// main method
public static void main(String[] args)
{
// Set the look and feel.
try
{
UIManager.setLookAndFeel(
UIManager.getCrossPlatformLookAndFeelClassName());
}
catch(ClassNotFoundException e){ }
catch(InstantiationException e){ }
catch(IllegalAccessException e){ }
catch(UnsupportedLookAndFeelException e){ }
ConvertText convert = new ConvertText();
}
}