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

时间:2013-12-20 03:01:27

标签: java swing

我正在为我的摇滚,纸张,剪刀游戏添加播放器名称输入,据我所知,我可以使用带有JTextField的不同卡片,创建一个字符串,然后用它来替换“播放器1 /球员2.“唯一的问题是,当我将TextField添加到面板时,然后将其添加到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笔记本电脑上再次测试过,没问题。我开始认为问题出在我的桌面上。我的编译器是Dr. Java。

3 个答案:

答案 0 :(得分:3)

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

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

    }
}

答案 1 :(得分:1)

了解您是如何创建CardLayout

    cardPanel.add(pname, "pname");
    cardPanel.add(panel1, "player1");
    cardPanel.add(panel2r, "player2r");
    cardPanel.add(panel2p, "player2p");
    cardPanel.add(panel2s, "player2s");
    cardPanel.add(finish, "finish");
  • 首先,您要添加pname,其中只包含两个文本字段

见这里

JPanel pname = new JPanel();
JTextField p1name = new JTextField(20);
JTextField p2name = new JTextField(20);
pname.add(p1name);
pname.add(p2name);

自首次展示pnamecardLayout.show(cardPanel, "pname");后,您将看到的是文本字段。并且该面板没有按钮或监听器可以将卡布局推进到不同的面板

我真的不想进入每个组件的所有分组。但想想你想要在每个面板上的内容,并将这些组件添加到不同的面板,然后将每个组件添加到组布局。请记住,每个视图中都需要一个能够显示不同视图的按钮。

我建议甚至将代码中的所有组件从一个面板分组,以使哪个组件进入哪个标签更易读。我不想详细讨论分组的原因是因为你的代码很难阅读1)因为组件遍布整个地方而2)组件名称非常相似。

编辑:看一下这个例子。你可以运行它。它可能无法为您提供您正在寻找的确切功能,但您可以将其与您的功能进行比较。

import java.awt.BorderLayout;
import java.awt.CardLayout;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.SwingUtilities;

public class PeeskilletsRPS extends JPanel{

    // buttons
    JButton P1Rock = new JButton("Rock");
    JButton P1Paper = new JButton("Paper");
    JButton P1Scissors = new JButton("Scissors");
    JButton P2Rock = new JButton("Rock");
    JButton P2Paper = new JButton("Paper");
    JButton P2Scissors = new JButton("Scissors");
    JButton jbtPlay = new JButton("Play");

    // panels
    JPanel namesPanel;
    JPanel player1Panel;
    JPanel player2Panel;

    // finish panel
    JPanel finishPanel;
    JButton playAgain;
    JLabel result;

    // CardLayout and main Panel
    CardLayout cardLayout;
    JPanel cardPanel;

    String player1Choice;
    String player2Choice;
    String status;

    String player2Name;
    String player1Name;


    public PeeskilletsRPS(){
        // names panel
        JPanel p1 = new JPanel(new GridLayout(4,1));
        p1.add(new JLabel("Player 1 Name: "));
        final JTextField jtfname1 = new JTextField(15);
        p1.add(jtfname1);
        p1.add(new JLabel("Player 2 Name: "));
        final JTextField jtfname2 = new JTextField(15);
        p1.add(jtfname2);
        namesPanel = new JPanel(new BorderLayout());
        namesPanel.add(p1, BorderLayout.CENTER);
        namesPanel.add(jbtPlay, BorderLayout.SOUTH);

        // player 1 panel
        JPanel player1Buttons = new JPanel();
        player1Buttons.add(P1Rock);
        player1Buttons.add(P1Paper);
        player1Buttons.add(P1Scissors);
        player1Panel = new JPanel(new BorderLayout());
        player1Panel.add(player1Buttons, BorderLayout.SOUTH);
        final JLabel jlblPlayer1 = new JLabel("  ");
        jlblPlayer1.setHorizontalAlignment(JLabel.CENTER);
        player1Panel.add(jlblPlayer1, BorderLayout.CENTER);

        // player 2 panel
        JPanel player2Buttons = new JPanel();
        player2Buttons.add(P2Rock);
        player2Buttons.add(P2Paper);
        player2Buttons.add(P2Scissors);
        player2Panel = new JPanel(new BorderLayout());
        player2Panel.add(player2Buttons, BorderLayout.SOUTH);
        final JLabel jlblPlayer2 = new JLabel("  ");
        jlblPlayer2.setHorizontalAlignment(JLabel.CENTER);
        player2Panel.add(jlblPlayer2, BorderLayout.CENTER);

        // finish panel
        result = new JLabel("  ");
        playAgain = new JButton("Play Agian");
        finishPanel = new JPanel(new BorderLayout());
        finishPanel.add(playAgain, BorderLayout.SOUTH);
        finishPanel.add(result, BorderLayout.CENTER);
        result.setHorizontalAlignment(JLabel.CENTER);

        // create card layout
        cardLayout = new CardLayout();
        cardPanel = new JPanel(cardLayout);
        cardPanel.add(namesPanel, "names");
        cardPanel.add(player1Panel, "player1");
        cardPanel.add(player2Panel, "player2");
        cardPanel.add(finishPanel, "finish");

        // add cardPanel to main panel
        add(cardPanel);

        P1Rock.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {

                player1Choice = "Rock";
                cardLayout.show(cardPanel, "player2");
            }
        });
        P1Paper.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                player1Choice = "Paper";
                cardLayout.show(cardPanel, "player2");
            }
        });
        P1Scissors.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                player1Choice = "Scissors";
                cardLayout.show(cardPanel, "player2");
            }
        });

        P2Rock.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                player2Choice = "Rock";
                checkWinner();
                result.setText(status);
                cardLayout.show(cardPanel, "finish");
            }
        });

        P2Paper.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                player2Choice = "Paper";
                checkWinner();
                result.setText(status);
                cardLayout.show(cardPanel, "finish");
            }
        });

        P2Scissors.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                player2Choice = "Scissors";
                checkWinner();
                result.setText(status);
                cardLayout.show(cardPanel, "finish");
            }
        });

        jbtPlay.addActionListener(new ActionListener(){
            public void actionPerformed(ActionEvent e) {
                player1Name = jtfname1.getText();
                player2Name = jtfname2.getText();
                jlblPlayer1.setText(player1Name + " choose: ");
                jlblPlayer2.setText(player2Name + " choose: ");
                cardLayout.show(cardPanel, "player1");

            }
        });

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

    private void checkWinner(){
        if (player1Choice.equals(player2Choice)) 
            status = "It's a Tie!";
        else if ("Rock".equals(player1Choice) && "Paper".equals(player2Choice))
            status = player2Name + " wins. Paper beats Rock!";
        else if ("Rock".equals(player1Choice) && "Scissors".equals(player2Choice))
            status = player1Name + " wins. Rock beats Scissors!";
        else if ("Paper".equals(player1Choice) && "Rock".equals(player2Choice))
            status = player1Name + " wins. Paper beats Rock!";
        else if ("Paper".equals(player1Choice) && "Scissors".equals(player2Choice))
            status = player2Name + " wins. Scissors beats Paper!";
        else if ("Scissors".equals(player1Choice) && "Paper".equals(player2Choice))
            status = player1Name + " wins. Scissors beats Paper!";
        else if ("Scissors".equals(player1Choice) && "Rock".equals(player2Choice))
            status = player2Name + " wins. Rock beats Scissors!";   
    }

    public static void main(String[] args) {
        SwingUtilities.invokeLater(new Runnable(){
            public void run(){
                JFrame frame = new JFrame("Peeskillet's Rock, Paper, Scissors");
                frame.add(new PeeskilletsRPS());
                frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                frame.setLocationRelativeTo(null);
                frame.pack();
                frame.setVisible(true);
            }
        });
    }
}

enter image description here enter image description here enter image description here enter image description here

答案 2 :(得分:0)

你应该调用pack()然后重新验证()。在你的情况下:

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");
pack();
revalidate();