Hope someone could give me a wee bit of advice. I am really new to Java (1-2 weeks) and have been using tutorials to get this far but nothing I try allows me to do a (simple) task.
When I click the button send in my GUI, I want it to set text/add text to the JTextArea on my GUI of which I typed in to the JTextField. I have provided my code below so someone can guide me to where I have went wrong.
package firstjavagui;
import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.*;
public class FirstJavaGui extends JPanel implements ActionListener {
private static ActionListener e;
protected JTextField tf;
protected JTextArea ta;
protected JButton send;
private final static String newL = "\n";
public FirstJavaGui() {
}
@Override
public void actionPerformed(ActionEvent e) {
throw new UnsupportedOperationException("Not supported yet.");
}
public class e implements ActionListener {
@Override
public void actionPerformed(ActionEvent e) {
String text = tf.getText();
if (send.isSelected()) {
ta.setText(text + newL);
}
}
}
public static void main(String[] args) {
JFrame frame = new JFrame("Thomas' first application");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(575,300);
JMenuBar mb = new JMenuBar();
JMenu m1 = new JMenu("File");
JMenu m2 = new JMenu("Help");
mb.add(m1);
mb.add(m2);
JMenuItem m21 = new JMenuItem("Save");
JMenuItem m22 = new JMenuItem("Exit");
m1.add(m21);
m1.add(m22);
JButton send = new JButton("Send");
JButton reset = new JButton("Start/Restart");
JLabel enter = new JLabel("Enter text here:");
JTextField text = new JTextField(25);
JPanel p = new JPanel();
p.add(enter);
p.add(text);
p.add(send);
p.add(reset);
JTextArea ta = new JTextArea();
frame.getContentPane().add(BorderLayout.SOUTH,p);
frame.getContentPane().add(BorderLayout.NORTH, mb);
frame.getContentPane().add(BorderLayout.CENTER, ta);
send.addActionListener(e);
frame.setVisible(true);
}
}
Be greatly appreciated if someone could assist me slightly.
I think it is because all my buttons etc are within my main so my actions can not recognize the "send" as the button...but if I write my gui outside of the main method it doesn't compile.. I am new to this but I am learning. Just need a small hand.