-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcalculator
More file actions
55 lines (45 loc) · 1.59 KB
/
Copy pathcalculator
File metadata and controls
55 lines (45 loc) · 1.59 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
package javalab;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class Sample {
public static void main(String[] args) {
// Create a new JFrame with a title
JFrame f = new JFrame("Calculator");
f.setSize(500, 400);
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.setLayout(null);
// Label for instruction
JLabel l = new JLabel("Enter the string:");
l.setBounds(100, 100, 150, 25);
f.add(l);
// Text field to input the string
JTextField t = new JTextField(20);
t.setBounds(250, 100, 150, 25);
f.add(t);
// Label to display the reversed string
JLabel l2 = new JLabel("");
l2.setBounds(150, 200, 300, 25);
f.add(l2);
// Button to trigger the reversal
JButton b = new JButton("Reverse");
b.setBounds(200, 150, 100, 25);
f.add(b);
// Action listener for the button
b.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
// Get text from the text field
String s = t.getText();
// Reverse the string without using inbuilt functions
String s1 = "";
for (int i = s.length() - 1; i >= 0; i--) {
s1 += s.charAt(i);
}
// Set the reversed string in the label
l2.setText("Reversed string: " + s1);
}
});
// Make the frame visible
f.setVisible(true);
}
}