-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSlidingPanel.java
148 lines (116 loc) · 4.09 KB
/
SlidingPanel.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
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
import java.awt.Color;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;
import javax.swing.Timer;
public class SlidingPanel {
JPanel panel1,panel2,panel4;
int x = 0;
int y = -350;
int z = 350;
public void makeUI() {
panel1 = new JPanel();
panel1.setLayout(null);
panel1.setBackground(Color.RED);
panel1.setBounds(z, x, 400, 400);
panel2 = new JPanel();
panel2.setLayout(null);
panel2.setBackground(Color.BLUE);
panel2.setBounds(y, x, 400, 400);
panel4 = new JPanel();
panel4.setLayout(null);
panel4.setBackground(Color.YELLOW);
panel4.setBounds(x, y, 400, 400);
JButton b1 = new JButton("sleft");
JButton b2 = new JButton("sright");
JButton b3 = new JButton("sup");
JButton b4 = new JButton("sdown");
panel1.add(b1);
panel2.add(b2);
panel4.add(b4);
b1.setBounds(10, 160, 50, 30);
b2.setBounds(350, 160, 50, 30);
b4.setBounds(160, 350, 60, 30);
//Slide Left
b1.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
((JButton) e.getSource()).setEnabled(false);
new Timer(1, new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
y = panel1.getX() - 1;
panel1.setLocation(y, x);
//if (panel1.getX() + panel1.getWidth() == 0)
if (x == 0 && y == 0)
{
((Timer) e.getSource()).stop();
System.out.println("Timer stopped");
}
}
}).start();
}
});
//Slide Right
b2.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
((JButton) e.getSource()).setEnabled(false);
new Timer(1, new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
y = panel2.getX() + 1;
panel2.setLocation(y, x);
//if (panel2.getX() + panel2.getWidth() == 0)
if ( x == 0 && y == 0)
{
((Timer) e.getSource()).stop();
System.out.println("Timer stopped");
}
}
}).start();
}
});
//Slide Down
b4.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e)
{
((JButton) e.getSource()).setEnabled(false);
new Timer(1, new ActionListener(){
public void actionPerformed(ActionEvent e) {
y = panel4.getY()+1;
panel4.setLocation(x, y);
if (x ==0 && y ==0)
{
((Timer) e.getSource()).stop();
System.out.println("Timer stopped");
}
}
}).start();
}
});
JFrame frame = new JFrame("Sliding Panel");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(400, 400);
frame.setLocationRelativeTo(null);
frame.setLayout(null);
frame.add(panel1);
frame.add(panel2);
frame.add(panel4);
frame.setVisible(true);
}
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
public void run() {
new SlidingPanel().makeUI();
}
});
}
}