0
GUI
을 구현하려고합니다. 불행히도 나는 menuBar가있는 프레임 만 얻습니다. 내가 정의한 패널과 그 내용은 나타나지 않습니다.Java - JPanel 및/또는 해당 내용이 표시되지 않습니다.
패널과 그 내용이 표시되지 않는 이유와 내가 뭘 잘못했는지 (대부분 initialiseLeftPanel()
방법)에 대한 조언을 주시면 감사하겠습니다.
내가 지금까지 시도하는 것 :
이 setVisible(true);
의 위치를 변경 일부 revalidate()
's을 (를) 포함;
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);
}
}
귀하의 조언은 제가 추가해야했던 약간의 변화와 함께 저에게 효과적이었습니다. 또한 그것을 나타 내기 위해 panel.setBounds (width, height) 명령어를 포함해야했습니다. 정말 고마워. – Nickels
좋아요, 답을 확인하는 것을 잊지 마세요. 그러면 다음 사용자에게 도움이 될 것입니다. – BaptisteL