gpt4 book ai didi

java - JPanel 没有出现在 JFrame 上

转载 作者:行者123 更新时间:2023-11-29 03:01:47 26 4
gpt4 key购买 nike

我已经为我的骰子游戏创建了一个基本的用户界面。但是我的面板没有出现在 JFrame 中。请帮我修一下。我是 Java Swing 的新手。

package View;

import java.awt.BorderLayout;
import java.awt.Container;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import javax.swing.JButton;
import javax.swing.JCheckBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;

/**
*
* @author Akila
*/
public class MainUi extends JFrame {
public MainUi(){
initComponents();
}

public void initComponents() {
setTitle("Dice Game");
setVisible(true);
setSize(500, 400);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setResizable(true);
Container c = getContentPane();

// *******First Row Components*******
JPanel firstRow = new JPanel();

JLabel pc = new JLabel("PC Score");
JLabel user = new JLabel("User Score");
JLabel pcScore = new JLabel();
JLabel userScore = new JLabel();


// *******Second Row Components*******
JPanel secondRow = new JPanel();

JLabel pcFirstDice = new JLabel();
JLabel pcSecondDice = new JLabel();
JLabel pcThirdDice = new JLabel();
JLabel pcFourthDice = new JLabel();
JLabel pcFifthDice = new JLabel();

JCheckBox checkPcFirstDice = new JCheckBox();
JCheckBox checkPcSecondDice = new JCheckBox();
JCheckBox checkPcThirdDice = new JCheckBox();
JCheckBox checkPcFourthDice = new JCheckBox();
JCheckBox checkPcFifthDice = new JCheckBox();

// *******Third Row Components*******
JPanel thirdRow = new JPanel();

JLabel userFirstDice = new JLabel();
JLabel userSecondDice = new JLabel();
JLabel userThirdDice = new JLabel();
JLabel userFourthDice = new JLabel();
JLabel userFifthDice = new JLabel();

JCheckBox checkUserFirstDice = new JCheckBox();
JCheckBox checkUserSecondDice = new JCheckBox();
JCheckBox checkUserThirdDice = new JCheckBox();
JCheckBox checkUserFourthDice = new JCheckBox();
JCheckBox checkUserFifthDice = new JCheckBox();

// *******Fourth Row Components*******
JPanel fourthRow = new JPanel();

JButton throwDice = new JButton("Throw");
JButton updateScore = new JButton("Update Score");

// *******First Row GridBag Layout*******
firstRow.setSize(400, 100);
firstRow.setLayout(new GridBagLayout());
GridBagConstraints first = new GridBagConstraints();
first.weightx = 1;
first.weighty = 0.25;

// Add First Row Components
first.gridx = 1;
first.gridy = 0;
firstRow.add(pcScore,first);
c.add(firstRow,BorderLayout.NORTH);
}
}

最佳答案

这里有两个问题:

  1. 您忘记将行添加到框架中:

        c.add(secondRow);
    c.add(thirdRow);
    c.add(fourthRow)
    c.add(firstRow);
  2. 您忘记将您的内容添加到您的行(第一行示例):

    //        *******First Row Components*******
    JPanel firstRow = new JPanel();

    JLabel pc = new JLabel("PC Score");
    JLabel user = new JLabel("User Score");
    JLabel pcScore = new JLabel();
    JLabel userScore = new JLabel();

    firstRow.add(pc);
    firstRow.add(user);
    firstRow.add(pcScore);
    firstRow.add(userScore);

希望对您有所帮助!

关于java - JPanel 没有出现在 JFrame 上,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34432522/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com