0

I would like to create a simple Blackjack GUI in Java. I know the basics of creating JLabel, JPanel etc. However, I cannot find why my JLabel is not displayed on the screen. Here is my code:

        //Create and set up the window.
        JFrame frame = new JFrame("BlackJack");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        //new border panel
        JPanel gui = new JPanel(new BorderLayout(2,2));

        //create players panel
        JPanel panel2 = new JPanel(new FlowLayout(FlowLayout.LEFT));
        panel2.setPreferredSize(new Dimension(600, 200));
        panel2.setBackground(Color.ORANGE);
        gui.add(panel2, BorderLayout.CENTER);


        //Display the window.
        frame.pack();
        frame.setVisible(true);
        frame.setTitle("BlackJack!");


        //add players name
        String name = JOptionPane.showInputDialog(null, "Name?");
        JLabel playerName = new JLabel(name);
        playerName.setPreferredSize(new Dimension(100, 40));
        playerName.setFont(new Font("sansserif", Font.BOLD, 18));
        panel2.add(playerName);

When I hit compile, what I get is a dialog for name? and then an empty panel. I do not understand why my JLabel is not in the panel since I have added it to my frame. Am I missing something?

4

2 に答える 2

4
if(nloop != 1 || nloop != 2){

This is a tautology.

For each number - it cannot be both 1 and 2 - so the condition (nloop != 1 || nloop != 2) always yields true.

Maybe you wanted && instead of ||?


The while loop: while(run2 = true){ also smells, as pointed in comments (though it doesn't seem to be the issue stated in the question).

于 2012-11-26T19:29:55.413 に答える
0

You need to change

if(nloop != 1 || nloop != 2){

to

if(nloop != 1 && nloop != 2){

Think about it. If you enter 1, your original if condition checks if 1 is unequal to 1 OR 1 is unequal to 2. 1 is obviously != 2, so the bad input code is executed.

Instead, you want your bad input code to run only if the input is not 1 AND not 2.

于 2012-11-26T19:30:04.503 に答える