关于Java:不显示JFrame中的组件

Components in JFrame aren't displayed

我想为"连续4个"游戏编写GUI ...
所以我用JPanel编写了一个JFrame,它具有GridLayout。
然后我添加了一些JButton和JLabel,但是当我运行该程序时,只有一个空框架... :(
这是我的代码:

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
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
import java.awt.*;
import javax.swing.*;

public class GUI{

/**
 * Constructor of the GUI
 */

 public GUI(){
    JFrame jf = new JFrame("4 Gewinnt");
    jf.setVisible(true);
    jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    jf.setSize(700, 800);
    JPanel jp = new JPanel();
    jp.setLayout(new GridLayout(8,7,0,0));
    jp.setComponentOrientation(ComponentOrientation.LEFT_TO_RIGHT);
    jp.setVisible(true);

    //Row Buttons
    JButton button0 = new JButton("Spalte 0");
    JButton button1 = new JButton("Spalte 1");
    JButton button2 = new JButton("Spalte 2");
    JButton button3 = new JButton("Spalte 3");
    JButton button4 = new JButton("Spalte 4");
    JButton button5 = new JButton("Spalte 5");
    JButton button6 = new JButton("Spalte 6");
    jp.add(button0);
    jp.add(button1);
    jp.add(button2);
    jp.add(button3);
    jp.add(button4);
    jp.add(button5);
    jp.add(button6);




    //Row 0Labels
    JLabel label00 =new JLabel("Label 00");
    JLabel label10 =new JLabel("Label 10");
    JLabel label20 =new JLabel("Label 20");
    JLabel label30 =new JLabel("Label 30");
    JLabel label40 =new JLabel("Label 40");
    JLabel label50 =new JLabel("Label 50");
    JLabel label60 =new JLabel("Label 60");
    jp.add(label00);
    jp.add(label10);
    jp.add(label20);
    jp.add(label30);
    jp.add(label40);
    jp.add(label50);
    jp.add(label60);

    //Row 1Labels
    JLabel label01 =new JLabel("Label 00");
    JLabel label11 =new JLabel("Label 10");
    JLabel label21 =new JLabel("Label 20");
    JLabel label31 =new JLabel("Label 30");
    JLabel label41 =new JLabel("Label 40");
    JLabel label51 =new JLabel("Label 50");
    JLabel label61 =new JLabel("Label 60");
    jp.add(label01);
    jp.add(label11);
    jp.add(label21);
    jp.add(label31);
    jp.add(label41);
    jp.add(label51);
    jp.add(label61);

    //Row 2Labels
    JLabel label02 =new JLabel("Label 00");
    JLabel label12 =new JLabel("Label 10");
    JLabel label22 =new JLabel("Label 20");
    JLabel label32 =new JLabel("Label 30");
    JLabel label42 =new JLabel("Label 40");
    JLabel label52 =new JLabel("Label 50");
    JLabel label62 =new JLabel("Label 60");
    jp.add(label02);
    jp.add(label12);
    jp.add(label22);
    jp.add(label32);
    jp.add(label42);
    jp.add(label52);
    jp.add(label62);

    //Row 3Labels
    JLabel label03 =new JLabel("Label 00");
    JLabel label13 =new JLabel("Label 10");
    JLabel label23 =new JLabel("Label 20");
    JLabel label33 =new JLabel("Label 30");
    JLabel label43 =new JLabel("Label 40");
    JLabel label53 =new JLabel("Label 50");
    JLabel label63 =new JLabel("Label 60");
    jp.add(label03);
    jp.add(label13);
    jp.add(label23);
    jp.add(label33);
    jp.add(label43);
    jp.add(label53);
    jp.add(label63);

    //Row 4Labels
    JLabel label04 =new JLabel("Label 00");
    JLabel label14 =new JLabel("Label 10");
    JLabel label24 =new JLabel("Label 20");
    JLabel label34 =new JLabel("Label 30");
    JLabel label44 =new JLabel("Label 40");
    JLabel label54 =new JLabel("Label 50");
    JLabel label64 =new JLabel("Label 60");
    jp.add(label04);
    jp.add(label14);
    jp.add(label24);
    jp.add(label34);
    jp.add(label44);
    jp.add(label54);
    jp.add(label64);

    //Row 5Labels
    JLabel label05 =new JLabel("Label 00");
    JLabel label15 =new JLabel("Label 10");
    JLabel label25 =new JLabel("Label 20");
    JLabel label35 =new JLabel("Label 30");
    JLabel label45 =new JLabel("Label 40");
    JLabel label55 =new JLabel("Label 50");
    JLabel label65 =new JLabel("Label 60");
    jp.add(label05);
    jp.add(label15);
    jp.add(label25);
    jp.add(label35);
    jp.add(label45);
    jp.add(label55);
    jp.add(label65);

    //Row 6Labels
    JLabel label06 =new JLabel("Label 00");
    JLabel label16 =new JLabel("Label 10");
    JLabel label26 =new JLabel("Label 20");
    JLabel label36 =new JLabel("Label 30");
    JLabel label46 =new JLabel("Label 40");
    JLabel label56 =new JLabel("Label 50");
    JLabel label66 =new JLabel("Label 60");
    jp.add(label06);
    jp.add(label16);
    jp.add(label26);
    jp.add(label36);
    jp.add(label46);
    jp.add(label56);
    jp.add(label66);



    jf.add(jp);
}



/**
 * Main method of GUI
 */

public static void main(String[] args)
{
    GUI gui = new GUI();
}
}


如LuxxMiner所建议,将jf.setVisible(true);移动到public GUI()构造函数的末尾。 这为我解决了问题。