gpt4 book ai didi

Java Swing : JPanel only shows up once JFrame is scaled

转载 作者:行者123 更新时间:2023-11-30 04:09:51 25 4
gpt4 key购买 nike

我正在开发一款迷你二十一点游戏,我刚进入 GUI,就遇到了一个小问题,我在一个 JFrame 中有 2 个 JPanel,但一旦我运行 main 方法,只会出现一个,并且上面会出现一个点我指定第二个面板位置的区域。一旦我用鼠标向上或向下或向任何方向缩放 JFrame,JPanel 就会出现在其正确的位置。我对如何解决这个问题感到有点困惑,因为我尝试了很多不同的方法但无法解决。任何帮助是极大的赞赏。代码如下:

<

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();
}

}

最佳答案

您需要从 GUI() 构造函数中删除 setVisible(true);。而是将其放在 main() 函数和 SwingUtilities.invokeLater() 中,如下所示:

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

Swing gui更新和渲染任务应该在EDT(事件调度线程)中执行。上面的调用就是为我们做的。

关于Java Swing : JPanel only shows up once JFrame is scaled,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19895177/

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