私はミニ ブラックジャック ゲームに取り組んでいましたが、GUI にたどり着いたところ、JFrame 内に 2 つの JPanel があり、main メソッドを実行すると 1 つだけが表示され、その領域にドットが表示されるという小さな問題が発生しました。 2 番目のパネルの位置を指定しました。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();
}
}