Can you help me with this problem
I have a class and I need to create separate class justo fro reste button, but I am not sure how to do this?
Here is what I have done
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class MyEventC extends JFrame implements ActionListener
{
JTextField text;
public MyEventC()
{
setLayout(new BorderLayout(5,10));
JPanel topSide = new JPanel();
topSide.setLayout(new FlowLayout(FlowLayout.LEFT, 25,3));
JLabel one = new JLabel("Current value");
text = new JTextField("0", 10);
topSide.add(one);
topSide.add(text);
text.addActionListener(this);
add(topSide, "North");
JPanel southSide = new JPanel();
southSide.setLayout(new FlowLayout(FlowLayout.CENTER, 20,3));
JButton btn1 = new JButton("+");
JButton btn2 = new JButton("-");
JButton btn3 = new JButton("Reset");
JButton btn4 = new JButton("Quit");
southSide.add(btn1);
southSide.add(btn2);
southSide.add(btn3);
southSide.add(btn4);
add(southSide, BorderLayout.SOUTH);
//text.addActionListener();
btn1.addActionListener(this);
btn2.addActionListener(this);
btn3.addActionListener(this);
btn4.addActionListener(this);
}
public void actionPerformed(ActionEvent e)
{
if (e.getActionCommand() == "+")
{
int num1 = Integer.parseInt(text.getText());
num1++;
String result = num1 + "";
text.setText(result);
System.out.println(num1+"");
}
else if (e.getActionCommand() == "-")
{
int num1 = Integer.parseInt(text.getText());
num1--;
String result = num1 + "";
text.setText(result);
System.out.println(num1+"");
}
else if(e.getActionCommand() == "Quit")
{
System.exit(0);
}
}
public static void main(String[]args)
{
MyEventC event = new MyEventC();
ResetListener reset = new ResetListener();
event.setTitle("Part 4 Using separte class for Reset Button");
event.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
event.setSize(400,150);
event.setVisible(true);
}
}
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class ResetListener extends MyEventC implements ActionListener
{
public void actionPerformed(ActionEvent e)
{
if (e.getActionCommand() == "Reset")
{
text.setText("0");
System.out.println("test1");
}
}
}