gpt4 book ai didi

java - 为什么我的卡片布局在添加 JTextField 后是空白的?

转载 作者:行者123 更新时间:2023-11-29 03:26:38 27 4
gpt4 key购买 nike

我正在将玩家名称输入添加到我的剪刀石头布游戏中,据我所知,我可以使用带有 JTextField 的不同卡片,创建一个字符串,然后用它来替换“Player 1/玩家 2。”唯一的问题是,当我将我的 TextFields 添加到面板,然后将其添加到 cardLayout,最后添加到框架时,一切都是空白的。这是示例代码。

final CardLayout cardLayout = new CardLayout();
final JPanel cardPanel = new JPanel(cardLayout);

JPanel pname = new JPanel();

JTextField p1name = new JTextField(20);
JTextField p2name = new JTextField(20);

pname.add(p1name);
pname.add(p2name);

cardPanel.add(pname, "pname");

add(cardPanel);

cardLayout.show(cardPanel, "pname");

我已经尝试了我在网上找到的一切。我正在运行 JRE 7,上次我检查时,它在 JRE 6 上运行良好(由于我一直在搞乱它,现在可能已经改变了。)任何帮助表示赞赏。谢谢!

完整代码,缩进不佳,(努力保持整洁)

import javax.swing.JFrame;
import javax.swing.JPanel;

import javax.swing.JLabel;
import javax.swing.JButton;
import javax.swing.JTextField;

import java.awt.CardLayout;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.awt.GridLayout;

public class RPS extends JFrame {

public static void main(String[] args) {
new RPS();
}
public RPS() {
super("Rock, Paper, Scissors");

setSize(300, 300);
setResizable(false);
setDefaultCloseOperation(EXIT_ON_CLOSE);
setVisible(true);

//Adds all of the components.

final CardLayout cardLayout = new CardLayout();
final JPanel cardPanel = new JPanel(cardLayout);
JPanel pname = new JPanel();
JPanel panel1 = new JPanel();
JPanel panel2r = new JPanel();
JPanel panel2p = new JPanel();
JPanel panel2s = new JPanel();
JPanel finish = new JPanel();

final JButton P1Rock = new JButton("Rock");
final JButton P1Paper = new JButton("Paper");
final JButton P1Scissors = new JButton("Scissors");

final JButton P2Rockr = new JButton("Rock");
final JButton P2Paperr = new JButton("Paper");
final JButton P2Scissorsr = new JButton("Scissors");

final JButton P2Rockp = new JButton("Rock");
final JButton P2Paperp = new JButton("Paper");
final JButton P2Scissorsp = new JButton("Scissors");

final JButton P2Rocks = new JButton("Rock");
final JButton P2Papers = new JButton("Paper");
final JButton P2Scissorss = new JButton("Scissors");

final JButton playAgain = new JButton("Play again?");

final JLabel statusLabel = new JLabel(" ");

JTextField p1name = new JTextField(20);
JTextField p2name = new JTextField(20);

JLabel P1turn = new JLabel("It is Player 1's turn. Choose:");
JLabel P2turnr = new JLabel("It is Player 2's turn. Choose:");
JLabel P2turnp = new JLabel("It is Player 2's turn. Choose:");
JLabel P2turns = new JLabel("It is Player 2's turn. Choose:");

//Sets up and adds all of the panels.

pname.add(p1name);
pname.add(p2name);

panel1.add(P1turn);
panel1.add(P1Rock);
panel1.add(P1Paper);
panel1.add(P1Scissors);
panel1.setLayout(new GridLayout(4,1));

panel2r.add(P2turnr);
panel2r.add(P2Rockr);
panel2r.add(P2Paperr);
panel2r.add(P2Scissorsr);
panel2r.setLayout(new GridLayout(4,1));

panel2p.add(P2turnp);
panel2p.add(P2Rockp);
panel2p.add(P2Paperp);
panel2p.add(P2Scissorsp);
panel2p.setLayout(new GridLayout(4,1));

panel2s.add(P2turns);
panel2s.add(P2Rocks);
panel2s.add(P2Papers);
panel2s.add(P2Scissorss);
panel2s.setLayout(new GridLayout(4,1));

finish.add(statusLabel);
finish.add(playAgain);
finish.setLayout(new GridLayout(2,1));

cardPanel.add(pname, "pname");
cardPanel.add(panel1, "player1");
cardPanel.add(panel2r, "player2r");
cardPanel.add(panel2p, "player2p");
cardPanel.add(panel2s, "player2s");
cardPanel.add(finish, "finish");

add(cardPanel);

cardLayout.show(cardPanel, "pname");

//Determines player 1's choice.

P1Rock.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
cardLayout.show(cardPanel, "player2r");
}
});

P1Paper.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
cardLayout.show(cardPanel, "player2p");
}
});

P1Scissors.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
cardLayout.show(cardPanel, "player2s");
}
});

playAgain.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
cardLayout.show(cardPanel, "player1");
}
});

//Determines player 2's choice and prints a result.

P2Rockr.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
statusLabel.setText("It's a tie!");
cardLayout.show(cardPanel, "finish");
}
});

P2Paperr.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
statusLabel.setText("Player 2 wins! Paper covers Rock.");
cardLayout.show(cardPanel, "finish");
}
});

P2Scissorsr.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
statusLabel.setText("Player 1 wins! Rock crushes Scissors.");
cardLayout.show(cardPanel, "finish");
}
});

P2Rockp.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
statusLabel.setText("Player 1 wins! Paper covers Rock!");
cardLayout.show(cardPanel, "finish");
}
});

P2Paperp.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
statusLabel.setText("It's a tie!");
cardLayout.show(cardPanel, "finish");
}
});

P2Scissorsp.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
statusLabel.setText("Player 2 wins! Scissors cut paper.");
cardLayout.show(cardPanel, "finish");
}
});

P2Rocks.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
statusLabel.setText("Player 2 wins! Rock crushes Scissors.");
cardLayout.show(cardPanel, "finish");
}
});

P2Papers.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
statusLabel.setText("Player 1 wins! Scissors cut Paper.");
cardLayout.show(cardPanel, "finish");
}
});

P2Scissorss.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
statusLabel.setText("It's a tie!");
cardLayout.show(cardPanel, "finish");
}
});

}
}

编辑:刚刚在我的 JRE 6 笔记本电脑上再次测试,没问题。我开始认为问题出在我的桌面上。我的编译器是 Java 博士。

最佳答案

对我来说效果很好,但也许您正在做的事情没有向我们展示...

Cards

import java.awt.BorderLayout;
import java.awt.CardLayout;
import java.awt.Dimension;
import java.awt.EventQueue;
import java.awt.Graphics;
import java.awt.Graphics2D;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;

public class TestCardLayout100 {

public static void main(String[] args) {
new TestCardLayout100();
}

public TestCardLayout100() {
EventQueue.invokeLater(new Runnable() {
@Override
public void run() {
try {
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
} catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
}

JFrame frame = new JFrame("Testing");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setLayout(new BorderLayout());
frame.add(new TestPane());
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
});
}

public class TestPane extends JPanel {

public TestPane() {
final CardLayout cardLayout = new CardLayout();
final JPanel cardPanel = new JPanel(cardLayout);

JPanel pname = new JPanel();

JTextField p1name = new JTextField(20);
JTextField p2name = new JTextField(20);

pname.add(p1name);
pname.add(p2name);

cardPanel.add(pname, "pname");

add(cardPanel);

cardLayout.show(cardPanel, "pname");
}

}
}

关于java - 为什么我的卡片布局在添加 JTextField 后是空白的?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20695615/

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