不显示JFrame中的组件(Components in JFrame aren't displayed)

编程入门 行业动态 更新时间:2024-10-25 22:29:12
不显示JFrame中的组件(Components in JFrame aren't displayed) java

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

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(); } }

I want to write a GUI for a "4 in a row" game... So I wrote a JFrame with a JPanel, which has a GridLayout. Then I added some JButtons and JLabels but when I run that programm, there is just a empty frame... :( Here is my code:

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()构造函数的末尾。 这解决了我的问题。

As LuxxMiner suggested, move jf.setVisible(true); to the end of the public GUI() constructor. This fixed the problem for me.

更多推荐

本文发布于:2023-08-05 06:50:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1428971.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:组件   JFrame   displayed   Components

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!