[英]Java - JPanel and/ or its contents not showing
目前,我正在尝试实现GUI
。 不幸的是,我只有一个带有menuBar的框架。 我定义的面板及其内容未显示。
对于为什么不显示面板及其内容以及我做错了什么,我将不胜感激(最有可能在initialiseLeftPanel()
方法中)。
到目前为止我尝试过的是:
更改了setVisible(true);
的位置setVisible(true);
包括一些revalidate()
;
public class Codebreakerz {
private static GameRenderer renderer;
public static void main(String[] args) {
renderer = new GameRenderer();
}
public class GameRenderer extends JFrame {
private final String TITLE = "Codebreakerz";
private Image ICON = getToolkit().getImage("res/confused.png");
private final int WIDTH = 900;
private final int HEIGHT = 800;
private final int ROUNDS = 12;
private final Color BACKGROUND = Color.lightGray;
private JPanel left;
private JPanel right;
private JLabel attemptsLabel;
private JLabel correctLabel;
private JLabel rightNumbLabel;
private JLabel[] roundLabels;
public GameRenderer() {
initialiseWindow();
initialiseMenu()
initialiseLabels();
initialiseLeftPanel();
}
private void initialiseWindow() {
setTitle(TITLE);
setIconImage(ICON);
setSize(WIDTH, HEIGHT);
setLocationRelativeTo(null);
setResizable(false);
setLayout(null);
setVisible(true);
}
private void initialiseMenu() {
JMenuBar menuBar = new JMenuBar();
JMenu file = new JMenu("Menu");
JMenuItem newGame = new JMenuItem("Neues Spiel");
JMenuItem close = new JMenuItem("Schließen");
file.add(newGame);
file.add(close);
menuBar.add(file);
setJMenuBar(menuBar);
}
private void initialiseLabels() {
attemptsLabel = new JLabel("0");
correctLabel = new JLabel("0");
rightNumbLabel = new JLabel("0");
roundLabels = new JLabel[ROUNDS];
for(int i=0; i<ROUNDS; i++) {
roundLabels[i] = new JLabel();
}
}
private void initialiseLeftPanel() {
left = new JPanel();
left.setLayout(null); // Tried other stuff aswell
JLabel heading = new JLabel("Codebreakerz");
JLabel tryNr = new JLabel("Anzahl Versuche: ");
JLabel correct = new JLabel("Richtig: ");
JLabel correctNumb = new JLabel("Richtige Nummer an falscher Stelle: ");
left.add(heading);
left.add(tryNr);
left.add(correct);
left.add(correctNumb);
add(left);
}
}
如果您left.setLayout(null);
行left.setLayout(null);
在您的方法中initialiseLeftPanel()
和setLayout(null);
在您的方法initialiseWindow()
它将起作用。
如果要显示面板,则必须具有布局。 默认情况下,它使用FlowLayout
,但是在这里您将其替换为null。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.