Skip to content

Commit 083460c

Browse files
authored
SWING
1 parent a259175 commit 083460c

12 files changed

+621
-0
lines changed

Diff for: BasicCalculator.java

+8
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
public class BasicCalculator
2+
{
3+
public static void main(String[] args)
4+
{
5+
new Calculator();
6+
}
7+
8+
}

Diff for: BorderLayoutExample.java

+42
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
import java.awt.*;
2+
import javax.swing.*;
3+
public class BorderLayoutExample
4+
{
5+
BorderLayoutExample()
6+
{
7+
JFrame frame = new JFrame("Border Layout");
8+
frame.setSize(400, 400);
9+
frame.setResizable(false);
10+
11+
JPanel panel = new JPanel();
12+
panel.setLayout(new BorderLayout(2,2));
13+
14+
JButton b1 = new JButton("Button 1");
15+
JButton b2 = new JButton("Button 2");
16+
JButton b3 = new JButton("Button 3");
17+
JButton b4 = new JButton("Button 4");
18+
JButton b5 = new JButton("Button 5");
19+
20+
b1.setBorder(BorderFactory.createLineBorder(Color.red));
21+
b2.setBorder(BorderFactory.createLineBorder(Color.red));
22+
b3.setBorder(BorderFactory.createLineBorder(Color.red));
23+
b4.setBorder(BorderFactory.createLineBorder(Color.red));
24+
b5.setBorder(BorderFactory.createLineBorder(Color.red));
25+
26+
panel.add(b1, BorderLayout.NORTH);
27+
panel.add(b5, BorderLayout.SOUTH);
28+
panel.add(b2, BorderLayout.WEST);
29+
panel.add(b4, BorderLayout.EAST);
30+
panel.add(b3, BorderLayout.CENTER);
31+
32+
frame.add(panel);
33+
34+
frame.setVisible(true);
35+
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
36+
}
37+
public static void main(String[] args)
38+
{
39+
new BorderLayoutExample();
40+
}
41+
42+
}

Diff for: Calculator.java

+79
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
import java.awt.Color;
2+
import java.awt.event.ActionListener;
3+
import java.awt.event.ActionEvent;
4+
import javax.swing.*;
5+
6+
public class Calculator implements ActionListener
7+
{
8+
JFrame frame;
9+
JPanel panel;
10+
JButton b1, b2;
11+
JTextArea inp1, inp2, result;
12+
13+
public void actionPerformed(ActionEvent e)
14+
{
15+
String s1 = inp1.getText();
16+
String s2 = inp2.getText();
17+
18+
try
19+
{
20+
double d1 = Double.parseDouble(s1);
21+
double d2 = Double.parseDouble(s2);
22+
double res = 0.0;
23+
if( e.getSource() == b1)
24+
res = d1 + d2;
25+
else if( e.getSource() == b2)
26+
res = d1 - d2;
27+
28+
result.setText(res+"");
29+
}
30+
catch(NumberFormatException nfe)
31+
{
32+
JFrame newFrame = new JFrame();
33+
JOptionPane.showMessageDialog(newFrame, "Please enter numeric values only!");
34+
}
35+
}
36+
37+
Calculator()
38+
{
39+
frame = new JFrame("Basic Calculator");
40+
41+
panel = new JPanel();
42+
panel.setLayout(null);
43+
44+
inp1 = new JTextArea("");
45+
inp2 = new JTextArea("");
46+
47+
result = new JTextArea("");
48+
result.setEditable(false);
49+
50+
b1 = new JButton(new ImageIcon("F:\\Heritage\\5th Sem Edu\\Advanced Java\\Lab Solutions\\Swing\\plus.png"));
51+
b2 = new JButton(new ImageIcon("F:\\Heritage\\5th Sem Edu\\Advanced Java\\Lab Solutions\\Swing\\minus.png"));
52+
53+
inp1.setBounds(30, 50, 430, 20);
54+
inp2.setBounds(30, 100, 430, 20);
55+
result.setBounds(30, 150, 430, 20);
56+
b1.setBounds(110, 180, 60, 60);
57+
b2.setBounds(270, 180, 60, 60);
58+
59+
inp1.setBorder(BorderFactory.createLineBorder(Color.RED));
60+
inp2.setBorder(BorderFactory.createLineBorder(Color.RED));
61+
result.setBorder(BorderFactory.createDashedBorder(Color.BLUE));
62+
63+
panel.add(inp1);
64+
panel.add(inp2);
65+
panel.add(result);
66+
panel.add(b1);
67+
panel.add(b2);
68+
69+
frame.add(panel);
70+
71+
b1.addActionListener(this);
72+
b2.addActionListener(this);
73+
74+
frame.setSize(500, 500);
75+
frame.setResizable(false);
76+
frame.setVisible(true);
77+
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
78+
}
79+
}

Diff for: Convert.java

+88
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
1+
import java.awt.event.*;
2+
import javax.swing.*;
3+
public class Convert
4+
{
5+
Convert()
6+
{
7+
// Frame
8+
JFrame frame = new JFrame("Temperature Convertor"); // create reference of the frame
9+
frame.setSize(400, 400); // set size of the frame
10+
frame.setResizable(false); // size of the frame cannot be changed
11+
// Panel
12+
JPanel panel = new JPanel(); // create reference of the panel
13+
panel.setLayout(null);
14+
15+
JLabel label1 = new JLabel("Enter Temperature: ");
16+
label1.setBounds(70, 30, 150, 50);
17+
18+
JTextArea text = new JTextArea("");
19+
text.setBounds(220, 30, 100, 50);
20+
21+
JLabel label2 = new JLabel("Convert into: ");
22+
label2.setBounds(150, 130, 120, 30);
23+
24+
JRadioButton radio1 = new JRadioButton("Farenheit",true); // choose Farenheit by default
25+
JRadioButton radio2 = new JRadioButton("Celsius");
26+
radio1.setBounds(150, 160, 100, 20);
27+
radio2.setBounds(150, 180, 100, 20);
28+
29+
ButtonGroup bg = new ButtonGroup(); // to enable choosing only one radio button
30+
31+
JButton button = new JButton("CONVERT");
32+
button.setBounds(150, 230, 100, 20);
33+
34+
JLabel label3 = new JLabel("");
35+
label3.setBounds(70, 280, 250,50);
36+
37+
// the anonymous event
38+
button.addActionListener
39+
(
40+
new ActionListener()
41+
{
42+
public void actionPerformed(ActionEvent e)
43+
{
44+
try
45+
{
46+
double temp = Double.parseDouble(text.getText());
47+
double result;
48+
if(radio1.isSelected())
49+
{
50+
result = 9.0/5*temp + 32;
51+
label3.setText("The temperature in Farenheit = "+result);
52+
}
53+
else if(radio2.isSelected())
54+
{
55+
result = 5.0/9 * (temp - 32);
56+
label3.setText("The temperature in Farenheit = "+result);
57+
}
58+
}
59+
catch(NumberFormatException nfe)
60+
{
61+
JFrame newFrame = new JFrame();
62+
JOptionPane.showMessageDialog(newFrame, "Please enter a number!");
63+
}
64+
}
65+
}
66+
);
67+
68+
69+
// adding components to the button group
70+
bg.add(radio1);
71+
bg.add(radio2);
72+
73+
// adding components to the panel
74+
panel.add(label1);
75+
panel.add(text);
76+
panel.add(label2);
77+
panel.add(radio1);
78+
panel.add(radio2);
79+
panel.add(button);
80+
panel.add(label3);
81+
82+
// adding component(s) to the frame
83+
frame.add(panel); // add the panel to the frame
84+
85+
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); // to terminate the program on closing the frame
86+
frame.setVisible(true); // to make the frame visible
87+
}
88+
}

Diff for: GridLayoutExample.java

+47
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
import java.awt.*;
2+
import javax.swing.*;
3+
public class GridLayoutExample
4+
{
5+
GridLayoutExample()
6+
{
7+
JFrame frame = new JFrame("Grid Layout");
8+
frame.setSize(400, 400);
9+
frame.setResizable(false);
10+
11+
JPanel panel = new JPanel();
12+
panel.setLayout(new GridLayout(2,3,4,4));
13+
14+
JButton b1 = new JButton("Button 1");
15+
JButton b2 = new JButton("Button 2");
16+
JButton b3 = new JButton("Button 3");
17+
JButton b4 = new JButton("Button 4");
18+
JButton b5 = new JButton("Button 5");
19+
JButton b6 = new JButton("Button 6");
20+
21+
b1.setBorder(BorderFactory.createLineBorder(Color.red));
22+
b2.setBorder(BorderFactory.createLineBorder(Color.red));
23+
b3.setBorder(BorderFactory.createLineBorder(Color.red));
24+
b4.setBorder(BorderFactory.createLineBorder(Color.red));
25+
b5.setBorder(BorderFactory.createLineBorder(Color.red));
26+
b6.setBorder(BorderFactory.createLineBorder(Color.red));
27+
28+
panel.applyComponentOrientation(ComponentOrientation.LEFT_TO_RIGHT);
29+
30+
panel.add(b1);
31+
panel.add(b2);
32+
panel.add(b3);
33+
panel.add(b4);
34+
panel.add(b5);
35+
panel.add(b6);
36+
37+
frame.add(panel);
38+
39+
frame.setVisible(true);
40+
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
41+
}
42+
public static void main(String[] args)
43+
{
44+
new GridLayoutExample();
45+
}
46+
47+
}

Diff for: MenuExample.java

+57
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
import javax.swing.*;
2+
public class MenuExample
3+
{
4+
JFrame frame;
5+
JMenuBar mb;
6+
JMenu file, edit, saving;
7+
JMenuItem open, close, save, saveAs, cut, copy, paste;
8+
MenuExample()
9+
{
10+
frame = new JFrame("Menu");
11+
// menu bar
12+
mb = new JMenuBar();
13+
// menus
14+
file = new JMenu("File");
15+
edit = new JMenu("Edit");
16+
saving = new JMenu("Save");
17+
18+
// menu items
19+
open = new JMenuItem("Open");
20+
close = new JMenuItem("Close");
21+
22+
file.add(open);
23+
file.add(close);
24+
25+
cut = new JMenuItem("Cut");
26+
copy = new JMenuItem("Copy");
27+
paste = new JMenuItem("Paste");
28+
29+
edit.add(copy);
30+
edit.add(paste);
31+
edit.add(cut);
32+
33+
save = new JMenuItem("Save");
34+
saveAs = new JMenuItem("Save As");
35+
36+
saving.add(save);
37+
saving.add(saveAs);
38+
39+
file.add(saving);
40+
41+
// adding componentes to menu bar
42+
mb.add(file);
43+
mb.add(edit);
44+
45+
// adding components to frame
46+
frame.setJMenuBar(mb);
47+
48+
frame.setSize(400, 400);
49+
frame.setVisible(true);
50+
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
51+
}
52+
public static void main(String[] args)
53+
{
54+
new MenuExample();
55+
}
56+
57+
}

Diff for: MouseListenerExample.java

+56
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
import java.awt.event.*;
2+
import javax.swing.*;
3+
public class MouseListenerExample implements MouseListener
4+
// extends MouseAdapter
5+
{
6+
JFrame frame;
7+
JPanel panel;
8+
JLabel label;
9+
10+
public void mouseClicked(MouseEvent e)
11+
{
12+
label.setText("Mouse Clicked!");
13+
}
14+
public void mousePressed(MouseEvent e)
15+
{
16+
label.setText("Mouse Pressed!");
17+
}
18+
public void mouseReleased(MouseEvent e)
19+
{
20+
label.setText("Mouse Released!");
21+
}
22+
public void mouseEntered(MouseEvent e)
23+
{
24+
label.setText("Mouse Entered!");
25+
}
26+
public void mouseExited(MouseEvent e)
27+
{
28+
label.setText("Mouse Exited!");
29+
}
30+
MouseListenerExample()
31+
{
32+
frame = new JFrame("MouseListenerExample");
33+
34+
panel = new JPanel();
35+
panel.setLayout(null);
36+
37+
label = new JLabel();
38+
label.setBounds(200,200,200,50);
39+
40+
panel.add(label);
41+
42+
panel.addMouseListener(this);
43+
44+
frame.add(panel);
45+
46+
frame.setSize(400, 400);
47+
frame.setVisible(true);
48+
frame.setResizable(false);
49+
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
50+
}
51+
public static void main(String[] args)
52+
{
53+
new MouseListenerExample();
54+
}
55+
56+
}

0 commit comments

Comments
 (0)