-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path55 .GridLayout.java
124 lines (96 loc) · 2.71 KB
/
55 .GridLayout.java
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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
package test;
import java.util.Arrays;
import java.util.Scanner;
//import javax.swing.*;
//import java.lang.Math;
//import java.util.Random;
//import java.io.IOException;
//import java.util.Arrays;
//import java.util.ArrayList;
//import java.util.List;
//import java.util.Map;
//import java.util.HashMap;
import java.io.*;
import java.util.*;
//import javax.sound.sampled.*;
import java.awt.*; // abstract window toolkit
import javax.swing.*;
import javax.imageio.ImageIO;
public class main{
// GridLayout .
// GridLayout :
//
public static void main(String[] args) {
// TODO Auto-generated method stub
// Declare the Scanner library
Scanner scanner = new Scanner(System.in);
JFrame frame = new JFrame("GridLAyout");
frame.setSize(400, 400);
// creating a panel with a GridLayout
JPanel panel = new JPanel(new GridLayout(3, 3));
int[] buttons = {1,2,3,4,5,6,7,8,9};
//
for(int button : buttons) {
JButton butto = new JButton("Button " + button);
panel.add(butto);
}
frame.add(panel);
frame.setVisible(true);
}
}
////////// method 2
package test;
import java.util.Arrays;
import java.util.Scanner;
//import javax.swing.*;
//import java.lang.Math;
//import java.util.Random;
//import java.io.IOException;
//import java.util.Arrays;
//import java.util.ArrayList;
//import java.util.List;
//import java.util.Map;
//import java.util.HashMap;
import java.io.*;
import java.util.*;
//import javax.sound.sampled.*;
import java.awt.*; // abstract window toolkit
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.*;
import javax.imageio.ImageIO;
public class main{
// GridLayout .
// GridLayout :
//
public static void main(String[] args) {
// TODO Auto-generated method stub
// Declare the Scanner library
Scanner scanner = new Scanner(System.in);
JFrame frame = new JFrame("Calculator");
JTextField res = new JTextField(10);
res.setEditable(false);
// creating a panel with a GridLayout
JPanel panel = new JPanel(new GridLayout(4, 4));
String[] buttons = {"7", "8", "9", "/", "4", "5", "6", "*", "1", "2", "3", "-", "0", ".", "=", "+"};
// on clicked function
ActionListener buttonListener = new ActionListener() {
public void actionPerformed(ActionEvent e) {
// get the button label
String label = ((JButton) e.getSource()).getText();
// append the label to the result field
res.setText(res.getText() + label);
}
};
//
for(String button : buttons) {
JButton butto = new JButton(button);
butto.addActionListener(buttonListener);
panel.add(butto);
}
frame.add(res, "North");
frame.add(panel);
frame.pack();
frame.setVisible(true);
}
}