user1810925
user1810925

Reputation: 63

Java Swing : JPanel only shows up once JFrame is scaled

I was working on a mini blackjack game and I ust got to the GUI and Im running into a small problem where I have 2 JPanels within a JFrame and only one would appear once I run the main method and a dot would appear over the area where I specified the location of the second panel . Once I scale the JFrame up or down or in any direction with the mouse , the JPanel appears at its correct location . Im kinda stumped on how to fix that problem , because I tried a bunch of different things and couldn't figure it out. Any help is greatly appreciated. Here's the code:

<

import java.awt.BorderLayout; import java.awt.FlowLayout; import java.awt.GridBagLayout; import java.awt.GridBagConstraints; import java.awt.Color; import java.awt.GridLayout; import java.awt.Insets; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import javax.swing.BorderFactory; import javax.swing.JFrame ; import javax.swing.JButton ; import javax.swing.JLabel; import javax.swing.JPanel ; import javax.swing.JTextArea; import java.awt.Dimension; import javax.swing.Box; import javax.swing.BoxLayout; public class GUI extends JFrame implements ActionListener { public GUI(){ //Constructor for BlackJack ///Frame///////////////////////////////////////////// super("BlackJack"); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); setBackground(Color.WHITE); setLayout(new GridBagLayout()); setSize(1051,550); setResizable(true); GridBagConstraints gbc = new GridBagConstraints(); GridBagConstraints gbc2 = new GridBagConstraints(); gbc.insets = new Insets(10,10,10,10); gbc2.insets= new Insets(10,10,10,10); // Creating all the necessary panels. JPanel scores = new JPanel(new GridBagLayout()); JPanel dealer = new JPanel(); JPanel player = new JPanel(); JPanel bettingArea = new JPanel(new GridBagLayout()); JPanel messages = new JPanel(); messages.setLayout(new BoxLayout(messages,BoxLayout.PAGE_AXIS)); //Adding the panels to the Frame. gbc2.anchor=GridBagConstraints.NORTHEAST; add(scores,gbc2); gbc2.anchor=GridBagConstraints.LAST_LINE_START; add(bettingArea,gbc2); gbc2.anchor=GridBagConstraints.LAST_LINE_END; add(messages,gbc2); // Setting borders for each Panel. scores.setBorder(BorderFactory.createLineBorder(Color.DARK_GRAY, 3)); bettingArea.setBorder(BorderFactory.createLineBorder(Color.DARK_GRAY, 3)); messages.setBorder(BorderFactory.createLineBorder(Color.BLUE, 3)); ////////End of Frame///////////////////////////////////////////////// //score's Interface ////////////////////////////// ///////////////////////////////////////////////// JButton hit = new JButton("Hit"); JButton check = new JButton("Check"); JLabel plrScore = new JLabel("Player Score"); JLabel dlrScore = new JLabel("Dealer Score"); // Setting Different Border Colors for the Player and Dealer plrScore.setBorder(BorderFactory.createLineBorder(Color.BLUE)); dlrScore.setBorder(BorderFactory.createLineBorder(Color.BLACK)); // Setting up the GridBagConstraints. gbc.gridx=0; gbc.gridy=0; scores.add(plrScore,gbc); gbc.gridx=1; gbc.gridy=0; scores.add(dlrScore,gbc); gbc.gridx=0; gbc.gridy=1; scores.add(hit,gbc); gbc.gridx=1; gbc.gridy=1; scores.add(check,gbc); //bettingArea's Interface ////////////////////////// /////////////////////////////////////////////////// JButton increaseBet = new JButton("Increase"); JButton decreaseBet = new JButton("Decrease"); JTextArea playerBet = new JTextArea("Player Bet"); JTextArea playerCash = new JTextArea("Player Cash"); // Setting Different Borders for the Objects playerBet.setBorder(BorderFactory.createRaisedSoftBevelBorder()); playerCash.setBorder(BorderFactory.createRaisedSoftBevelBorder()); // Setting up the GridBagConstraints. gbc.gridx=0; gbc.gridy=0; bettingArea.add(increaseBet,gbc); gbc.gridx=1; gbc.gridy=0; bettingArea.add(playerBet,gbc); gbc.gridx=0; gbc.gridy=1; bettingArea.add(decreaseBet,gbc); gbc.gridx=1; gbc.gridy=1; bettingArea.add(playerCash,gbc); ////messages Interface///////////////////// JTextArea msg1 = new JTextArea("Message 1"); JTextArea msg2 = new JTextArea("Message 2"); JTextArea msg3 = new JTextArea("Message 3"); JTextArea msg4 = new JTextArea("Message 4"); messages.add(msg1); messages.add(Box.createRigidArea( new Dimension(0,2))); messages.add(msg2); messages.add(Box.createRigidArea( new Dimension(0,2))); messages.add(msg3); messages.add(Box.createRigidArea( new Dimension(0,2))); messages.add(msg4); //Visibility of Panels & JFrame. scores.setVisible(true); bettingArea.setVisible(true); messages.setVisible(true); setVisible(true); } public void actionPerformed(ActionEvent arg0) { // TODO Auto-generated method stub } // Main Method public static void main ( String args[]){ GUI game = new GUI(); }

}

Upvotes: 1

Views: 237

Answers (1)

Sage
Sage

Reputation: 15418

you need to remove the setVisible(true); from the GUI() constructor. Rather put it inside the main() function and SwingUtilities.invokeLater() as follows:

public static void main ( String args[]){
    SwingUtilities.invokeLater(new Runnable() {
        public void run() {
            new GUI().setVisible(true);
        }
    });
}

Swing gui update and rendering task should be performed in EDT(event dispatch thread). Above call do just that for us.

Upvotes: 2

Related Questions