Skip to content

Commit 3285323

Browse files
committed
lab #6
1 parent 17e1c51 commit 3285323

10 files changed

+246
-79
lines changed

lab-05/.idea/misc.xml

-6
This file was deleted.

lab-05/.idea/modules.xml

-8
This file was deleted.

lab-05/.idea/vcs.xml

-6
This file was deleted.

lab-05/.idea/workspace.xml

-46
This file was deleted.

lab-05/lab-05.iml

-11
This file was deleted.
Binary file not shown.

lab-06-swing/Calculator.java

+156
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,156 @@
1+
/*
2+
By Rustam Zokirov,
3+
U190049
4+
Lab: #6
5+
Calculator Application
6+
*/
7+
8+
// import some libraries
9+
import javax.swing.*;
10+
import java.awt.*;
11+
import java.awt.event.*;
12+
13+
public class Calculator extends JFrame {
14+
JTextField jtxtField = new JTextField(); // text field
15+
JButton jbt0Button = new JButton("0"); // some buttons from 0-9
16+
JButton jbt1Button = new JButton("1");
17+
JButton jbt2Button = new JButton("2");
18+
JButton jbt3Button = new JButton("3");
19+
JButton jbt4Button = new JButton("4");
20+
JButton jbt5Button = new JButton("5");
21+
JButton jbt6Button = new JButton("6");
22+
JButton jbt7Button = new JButton("7");
23+
JButton jbt8Button = new JButton("8");
24+
JButton jbt9Button = new JButton("9");
25+
JButton jbtDeleteButton = new JButton("/"); // operations
26+
JButton jbtMultiplyButton = new JButton("*");
27+
JButton jbtMinusButton = new JButton("-");
28+
JButton jbtPlusButton = new JButton("+");
29+
JButton jbtEqualButton = new JButton("=");
30+
JButton jbtDotButton = new JButton(".");
31+
32+
public Calculator() {
33+
// Panel for the text field
34+
JPanel panel_text = new JPanel();
35+
panel_text.setLayout(new BorderLayout()); // implementing BorderLayout
36+
panel_text.add(jtxtField, BorderLayout.NORTH);
37+
38+
// Create a panel to hold buttons
39+
JPanel panel = new JPanel();
40+
panel.setLayout(new GridLayout(4, 4)); // implementing GridLayout 4 by 4
41+
panel.add(jbt7Button);
42+
panel.add(jbt8Button);
43+
panel.add(jbt9Button);
44+
panel.add(jbtDeleteButton);
45+
panel.add(jbt4Button);
46+
panel.add(jbt5Button);
47+
panel.add(jbt6Button);
48+
panel.add(jbtMultiplyButton);
49+
panel.add(jbt1Button);
50+
panel.add(jbt2Button);
51+
panel.add(jbt3Button);
52+
panel.add(jbtMinusButton);
53+
panel.add(jbt0Button);
54+
panel.add(jbtDotButton);
55+
panel.add(jbtEqualButton);
56+
panel.add(jbtPlusButton);
57+
58+
panel_text.add(panel, BorderLayout.CENTER);
59+
add(panel_text); // Add panel to the frame
60+
61+
// Register listeners
62+
ListenerClass listener = new ListenerClass();
63+
jbt0Button.addActionListener(listener);
64+
jbt1Button.addActionListener(listener);
65+
jbt2Button.addActionListener(listener);
66+
jbt3Button.addActionListener(listener);
67+
jbt4Button.addActionListener(listener);
68+
jbt5Button.addActionListener(listener);
69+
jbt6Button.addActionListener(listener);
70+
jbt7Button.addActionListener(listener);
71+
jbt8Button.addActionListener(listener);
72+
jbt9Button.addActionListener(listener);
73+
jbtPlusButton.addActionListener(listener);
74+
jbtMinusButton.addActionListener(listener);
75+
jbtMultiplyButton.addActionListener(listener);
76+
jbtDeleteButton.addActionListener(listener);
77+
jbtEqualButton.addActionListener(listener);
78+
jbtDotButton.addActionListener(listener);
79+
}
80+
81+
public static void main(String[] args) {
82+
JFrame frame = new Calculator(); // creating object
83+
frame.setTitle("Handle Event");
84+
frame.setSize(200, 200); // set size
85+
frame.setLocation(200, 100); // set location in window
86+
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
87+
frame.setVisible(true);
88+
}
89+
90+
// ListenerClass - Inner Class for handing the actions
91+
class ListenerClass implements ActionListener {
92+
char operation; // operation
93+
double num1; // number 1
94+
double num2; // number 2
95+
double result; // result
96+
97+
public void actionPerformed(ActionEvent e) {
98+
// handle the text field
99+
if (e.getSource() == jbt0Button)
100+
jtxtField.setText(jtxtField.getText() + '0');
101+
else if (e.getSource() == jbt1Button)
102+
jtxtField.setText(jtxtField.getText() + '1');
103+
else if (e.getSource() == jbt2Button)
104+
jtxtField.setText(jtxtField.getText() + '2');
105+
else if (e.getSource() == jbt3Button)
106+
jtxtField.setText(jtxtField.getText() + '3');
107+
else if (e.getSource() == jbt5Button)
108+
jtxtField.setText(jtxtField.getText() + '5');
109+
else if (e.getSource() == jbt6Button)
110+
jtxtField.setText(jtxtField.getText() + '6');
111+
else if (e.getSource() == jbt7Button)
112+
jtxtField.setText(jtxtField.getText() + '7');
113+
else if (e.getSource() == jbt8Button)
114+
jtxtField.setText(jtxtField.getText() + '8');
115+
else if (e.getSource() == jbt9Button)
116+
jtxtField.setText(jtxtField.getText() + '9');
117+
else if (e.getSource() == jbtDotButton)
118+
jtxtField.setText(jtxtField.getText() + '.');
119+
else if (e.getSource() == jbtPlusButton) {
120+
operation = '+';
121+
num1 = Double.parseDouble(jtxtField.getText());
122+
jtxtField.setText("");
123+
}
124+
else if (e.getSource() == jbtMinusButton) {
125+
operation = '-';
126+
num1 = Double.parseDouble(jtxtField.getText());
127+
jtxtField.setText("");
128+
}
129+
else if (e.getSource() == jbtMultiplyButton) {
130+
operation = '*';
131+
num1 = Double.parseDouble(jtxtField.getText());
132+
jtxtField.setText("");
133+
}
134+
else if (e.getSource() == jbtDeleteButton) {
135+
operation = '/';
136+
num1 = Double.parseDouble(jtxtField.getText());
137+
jtxtField.setText("");
138+
}
139+
else if (e.getSource() == jbtEqualButton) {
140+
num2 = Double.parseDouble(jtxtField.getText());
141+
142+
// handle calculation
143+
if (operation == '+')
144+
result = num1 + num2;
145+
else if (operation == '-')
146+
result = num1 - num2;
147+
else if (operation == '*')
148+
result = num1 * num2;
149+
else if (operation == '/')
150+
result = num1 / num2;
151+
152+
jtxtField.setText(Double.toString(result)); // ouput the resust
153+
}
154+
}
155+
} // ListenerClass
156+
} // Calculator

lab-06-swing/Printer.java

+89
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
1+
/**
2+
* Rustam Zokirov, U1910049
3+
* Working with Layouts
4+
*/
5+
import javax.swing.*; // import library to work with GUI
6+
7+
public class Printer extends JFrame { // inherit JFrame
8+
public static void main(String[] args) {
9+
// Labels
10+
JLabel jlblPrinter = new JLabel("Printer: My Printer");
11+
jlblPrinter.setBounds(30, 10, 150, 20); // set the coordinates, length and width
12+
JLabel jlblQuality = new JLabel("Print Quality:");
13+
jlblQuality.setBounds(30, 100, 150, 20); // set the coordinates, length and width
14+
15+
// Text areas
16+
JTextArea jtxtFirst = new JTextArea();
17+
jtxtFirst.setBounds(25, 35, 50, 60);
18+
JTextArea jtxtSecond = new JTextArea();
19+
jtxtSecond.setBounds(150, 35, 40, 60);
20+
JTextArea jtxtThird = new JTextArea();
21+
jtxtThird.setBounds(270, 35, 50, 60);
22+
23+
// Check box
24+
JCheckBox checkBox1 = new JCheckBox("Image", true);
25+
checkBox1.setBounds(75, 37, 60, 15);
26+
JCheckBox checkBox2 = new JCheckBox("Text");
27+
checkBox2.setBounds(75, 57, 60, 15);
28+
JCheckBox checkBox3 = new JCheckBox("Code");
29+
checkBox3.setBounds(75, 77, 60, 15);
30+
JCheckBox printFileBox = new JCheckBox("Print to file");
31+
printFileBox.setBounds(195, 100, 120, 20);
32+
33+
// Radio Buttons
34+
JRadioButton radioButton1 = new JRadioButton("Selection");
35+
radioButton1.setBounds(190, 37, 80, 15);
36+
JRadioButton radioButton2 = new JRadioButton("All", true);
37+
radioButton2.setBounds(190, 57, 80, 15);
38+
JRadioButton radioButton3 = new JRadioButton("Applet");
39+
radioButton3.setBounds(190, 77, 80, 15);
40+
41+
// ComboBox
42+
String quality[] = {"High","Medium","Low"}; // array which contatins all the choices
43+
JComboBox comboBox = new JComboBox(quality);
44+
comboBox.setBounds(115, 100, 70, 20);
45+
46+
// Buttons
47+
JButton jbtOkButton = new JButton("Ok");
48+
jbtOkButton.setBounds(330, 20, 75, 25);
49+
JButton jbtCancelButton = new JButton("Cancel");
50+
jbtCancelButton.setBounds(330, 50, 75, 25);
51+
JButton jbtSetupButton = new JButton("Setup");
52+
jbtSetupButton.setBounds(330, 80, 75, 25);
53+
JButton jbtHelpButton = new JButton("Help");
54+
jbtHelpButton.setBounds(330, 110, 75, 25);
55+
56+
// Create Frame
57+
JFrame frame = new Printer();
58+
frame.add(jlblPrinter);
59+
frame.add(jlblQuality);
60+
61+
frame.add(jtxtFirst);
62+
frame.add(jtxtSecond);
63+
frame.add(jtxtThird);
64+
65+
frame.add(checkBox1);
66+
frame.add(checkBox2);
67+
frame.add(checkBox3);
68+
frame.add(printFileBox);
69+
70+
frame.add(radioButton1);
71+
frame.add(radioButton2);
72+
frame.add(radioButton3);
73+
74+
frame.add(comboBox);
75+
76+
frame.add(jbtOkButton);
77+
frame.add(jbtCancelButton);
78+
frame.add(jbtSetupButton);
79+
frame.add(jbtHelpButton);
80+
81+
// Some settings
82+
frame.setTitle("Printer");
83+
frame.setLayout(null);
84+
frame.setSize(450, 200);
85+
frame.setLocation(200, 100);
86+
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
87+
frame.setVisible(true);
88+
}
89+
}

lab-06-swing/U1910049_Lab#6.zip

2.41 KB
Binary file not shown.

programs/HandleEvent1.java

+1-2
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,7 @@
33
import javax.swing.*;
44
import java.awt.event.*;
55

6-
public class HandleEvent1 extends JFrame
7-
{
6+
public class HandleEvent1 extends JFrame {
87
JButton jbtOK = new JButton("OK");
98
JButton jbtCancel = new JButton("Cancel");
109

0 commit comments

Comments
 (0)