In: Computer Science
java:
Given the definitions: public interface ActionListener {
public void actionPerformed(ActionEvent e);
} public JTextField {
public JTextField(){} public void setText(String text) {}
public String getText() {}
}
Write the code to create a JButton on the South of the window and a JTextField on the North. The first time you click on the button, it should print out “hello!” on the JTextField. For the second time, should show “hello!hello!”. For the third time, should show “hello!hello!hello!”.
import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JTextField;
public class HelloGUI {
static JFrame frame = new JFrame("JFrame Example");
static JTextField str = new JTextField(10);
static int x,y;
public static void main(String s[]) {
JPanel panel = new JPanel();
panel.setLayout(new BorderLayout());
JButton convert = new JButton();
convert.setText("Click");
convert.setBounds(200, 150, 100, 20);
convert.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
//appending the hello! to the already existing text
str.setText("hello!"+str.getText());
}
});
panel.add(str,BorderLayout.NORTH);
panel.add(convert,BorderLayout.SOUTH);
frame.add(panel);
frame.setSize(600, 300);
frame.setLocationRelativeTo(null);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
}
}
NOTE : PLEASE COMMENT BELOW IF YOU HAVE CONCERNS.
Please Like and Support me as it helps me a lot