Reputation: 205
I have created a basic UI for my Dice Game. But my panels are not appearing in the JFrame
. Please help me fix it. I'm new to 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);
}
}
Upvotes: 1
Views: 85
Reputation: 2253
The problem is you are not adding any components to JPanel
you just create the components and add empty panel to Container
.
Here is a code snippet example of adding first row components to JPanel
:
// *******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);
You might also want to add all other panel rows to Container
since you are only adding first row to it.
Upvotes: 2
Reputation: 1651
You got two problems over here:
You forgot to add your rows to your Frame:
c.add(secondRow);
c.add(thirdRow);
c.add(fourthRow)
c.add(firstRow);
You Forgot to add your content to your Rows(example for row one):
// *******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);
Hope that helps!
Upvotes: 2