-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathAufgabe4.java
83 lines (65 loc) · 2.29 KB
/
Aufgabe4.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
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class Aufgabe4 {
JLabel labelA;
JLabel labelB;
int labelB_Counter = 0;
public static void main(String[] args){
Aufgabe4 gui = new Aufgabe4();
gui.initializeFrame();
}
public void initializeFrame(){
// Setup default frame
JFrame frame = new JFrame("My Menu Button App");
frame.setSize(480,480);
frame.setResizable(false);
frame.setLocationRelativeTo(null);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
// Create MenuBar elements and and the menuBar to the frame
JMenuBar menuBar = new JMenuBar();
JMenu menuFile = new JMenu("File");
JMenu menuHelp = new JMenu("Help");
menuBar.add(menuFile);
menuBar.add(menuHelp);
JMenuItem menuFileItemOpen = new JMenuItem("Open");
menuFile.add(menuFileItemOpen);
frame.setJMenuBar(menuBar);
// Layout
// Create/Setup panel
JPanel content = new JPanel(new GridBagLayout());
GridBagConstraints gbc = new GridBagConstraints();
// Setup labels and a button (=elements)
labelA = new JLabel("Ich kann zeichnen ...", SwingConstants.CENTER);
labelA.setAlignmentX(JLabel.LEFT_ALIGNMENT);
labelB = new JLabel("Du hast mich zum 0-mal gedrückt!", SwingConstants.CENTER);
labelB.setAlignmentX(JLabel.LEFT_ALIGNMENT);
JButton button = new JButton("Drücke mich!");
button.setAlignmentX(JButton.LEFT_ALIGNMENT);
button.addActionListener(new ClickListener());
// Layout + Adding components to the "content" panel
gbc.insets = new Insets(10, 10, 10, 10);
gbc.fill = GridBagConstraints.HORIZONTAL;
gbc.gridx = 0;
gbc.gridy = 0;
content.add(labelA, gbc);
gbc.fill = GridBagConstraints.HORIZONTAL;
gbc.gridx = 0;
gbc.gridy = 1;
content.add(button, gbc);
gbc.fill = GridBagConstraints.HORIZONTAL;
gbc.gridx = 0;
gbc.gridy = 2;
content.add(labelB, gbc);
// Add panel to the frame and make it visible
frame.add(content);
// THIS MUST STAY AT THE VERY END111!!
frame.setVisible(true);
}
class ClickListener implements ActionListener{
public void actionPerformed(ActionEvent event){
labelB_Counter += 1;
labelB.setText("Du hast mich zum " + String.valueOf(labelB_Counter) +"-mal gedrückt!");
}
}
}