Each of the buttons that I have listed performs some type of action. The button that says "reverse" in the text field should output the
word hello backward olleh. The
string at the very bottom, private String reverse(String inStr) is just not outputting correctly.
I'm really new at this and I can't figure out why it's not working. Most of it is working except for the hello reverse part. Can anyone help, please.
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class MemoSaver2
extends JFrame
implements ActionListener
{
public static final int WIDTH = 600;
public static final int HEIGHT = 300;
public static final int LINES = 10;
public static final int CHAR_PER_LINE = 40;
private JTextArea theText;
private String memo1 = "Memo 1 Saved.";
private String memo2 = "Memo 2 Saved.";
private String memo3 = "No Memo 3.";
private String memo4 = "No Memo 4.";
private String memo5 = "No Memo 5.";
private String memo6 = "No Memo 6.";
private String reverse;
public MemoSaver2()
{
setTitle("Memo Saver");
setSize(WIDTH, HEIGHT);
JLabel myLabel =
new JLabel();
addWindowListener(
new WindowDestroyer());
Container contentPane = getContentPane();
contentPane.setLayout(
new BorderLayout());
JPanel buttonPanel =
new JPanel();;
buttonPanel.setBackground(Color.GRAY);
buttonPanel.setLayout(
new GridLayout(3,2));/*ii*/
JButton memo1Button =
new JButton("Save Memo 1");
memo1Button.addActionListener(
this);
buttonPanel.add(memo1Button);
JButton memo2Button =
new JButton("Save Memo 2");
memo2Button.addActionListener(
this);
buttonPanel.add(memo2Button);
JButton clearButton =
new JButton("Clear");
clearButton.addActionListener(
this);
buttonPanel.add(clearButton);
JButton memo3Button =
new JButton("Get Memo 1");
memo3Button.addActionListener(
this);
buttonPanel.add(memo3Button);
JButton get1Button =
new JButton("Get Memo 2");
get1Button.addActionListener(
this);
buttonPanel.add(get1Button);
JButton get2Button =
new JButton("Exit");
get2Button.addActionListener(
this);
buttonPanel.add(get2Button);
JButton get3Button =
new JButton("Reverse");
get3Button.addActionListener(
this);
buttonPanel.add(get3Button);
JButton get4Button =
new JButton("My Name");
get4Button.addActionListener(
this);
buttonPanel.add(get4Button);
JButton get5Button =
new JButton("My Info");
get5Button.addActionListener(
this);
buttonPanel.add(get5Button);
contentPane.add(buttonPanel, BorderLayout.SOUTH);
//contentPane.add(buttonPanel, GridLayout);
JPanel textPanel =
new JPanel();
textPanel.setBackground(Color.BLUE);
theText =
new JTextArea(LINES, CHAR_PER_LINE);
theText.setBackground(Color.WHITE);
textPanel.add(theText);
contentPane.add(textPanel, BorderLayout.CENTER);
}
public void actionPerformed(ActionEvent e)
{
String actionCommand = e.getActionCommand();
if (actionCommand.equals("Save Memo 1"))
{
memo1 = theText.getText();
theText.setText("Memo 1 saved."); /*iii*/
}
else if (actionCommand.equals("Save Memo 2")) /*iii*/
{
memo2 = theText.getText();
theText.setText("Memo 2 saved."); /*iii*/
}
else if (actionCommand.equals("Exit")) /*iv*/
{
System.exit(0);
}
else if (actionCommand.equals("Student"))
JOptionPane.showMessageDialog(
null, "Student");
else if (actionCommand.equals("Clear"))
theText.setText("");
else if (actionCommand.equals("Get Memo 1"))
theText.setText(memo1);
else if (actionCommand.equals("Get Memo 2"))
theText.setText(memo2);
else if (actionCommand.equals("My Info"))
{
memo5 = theText.getText();
theText.setText("" + ""
+ "
[email protected]");
}
else if (actionCommand.equals("Hello"))
{
memo3 = theText.getText();
theText.setText("");
}
else if (actionCommand.equals("My Name"))
theText.setText("Student");
}
public static void main(String[]args)
{
MemoSaver2 guiMemo =
new MemoSaver2();
guiMemo.setVisible(
true);
}
public class WindowDestroyer
extends WindowAdapter
{
public void windowClosing(WindowEvent e)
{
System.exit(0);
}
private String reverse(String inStr)
{
memo3 = theText.getText();
theText.setText("hello");
//reverse the string then return it
return memo3;
}
}
}