1

我正在开发一个迷你二十一点游戏,我刚进入 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(); }

}

4

1 回答 1

2

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

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

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

于 2013-11-10T21:15:56.883 回答