大家好,我对此有点问题。我创建了一个 JPanel 并向其中添加了组件,然后将 JPanel 添加到容器中。现在,当我从 main 调用这个类时,会弹出一个窗口,但它只显示 JPanel 的第一个组件。为什么它只显示第一项而不是全部?谢谢。
注意:此代码不完整,我只是想弄清楚为什么我的组件在继续其他事情之前没有显示,请解决组件问题。
import java.awt.BorderLayout;
import java.awt.CardLayout;
import java.awt.Container;
import java.awt.FlowLayout;
import java.awt.event.*;
import javax.swing.*;
public class Player extends JFrame implements ActionListener
{
private CardLayout playerCard;
private JPanel cardPanel;
public String player1;
public String player2;
// Constructor:
public Player()
{
setTitle("Game");
setSize(300,200);
setLocation(10,200);
Container contentPane = getContentPane();
contentPane.setLayout(new FlowLayout());
//set up the panel
cardPanel = new JPanel();
playerCard = new CardLayout();
cardPanel.setLayout(playerCard);
//get player one name
JLabel p1Name = new JLabel("Player 1 Name:");
JTextField oneName = new JTextField();
//get the name for player 2
JLabel p2Name = new JLabel("Player 2 Name:");
JTextField twoName = new JTextField();
//the button to start the game
JButton start = new JButton("Start");
//add the components << Why is only the first component shown??
cardPanel.add(start);
cardPanel.add(p1Name);
cardPanel.add(oneName);
cardPanel.add(p2Name);
cardPanel.add(twoName);
contentPane.add("startCard",cardPanel);
}
@Override
public void actionPerformed(ActionEvent arg0)
{
// TODO Auto-generated method stub
}
}