JLayeredPane没有出现

时间:2017-02-14 00:37:08

标签: java swing user-interface jlayeredpane

我正在尝试将以下JLayeredPane实现到我的代码中,但它似乎没有出现 - 即使它在另一个类中完美运行。有没有办法让它在我的主类中工作,或者调用另一个类来使它工作。 bgPanel类有一个带有JLayeredPane的棋盘图像和令牌图像,我希望看到它出现在已经包含其余GUI的主JFrame上。

JLayered Pane:

     JLayeredPane LPane;
     {
     LPane = new JLayeredPane();
     frame.getContentPane().add(LPane);
     JLabel board = new JLabel(new ImageIcon("/Users/gavincoll/Documents/workspace
     /MonopolyStarboy/src/monopoly-board-blank.jpg"));
     board.setBounds(7,50, 700, 700);
     LPane.add(board, new Integer(1));
     }

主要垄断类:

 import java.awt.BorderLayout;
 import java.awt.Color;
 import java.awt.Container;
 import java.awt.EventQueue;
 import java.awt.Graphics;
 import java.awt.Image;

 import javax.swing.JFrame;
 import javax.swing.JLabel;
 import javax.swing.JLayeredPane;
 import javax.swing.JOptionPane;
 import javax.swing.JPanel;
 import javax.swing.JTextField;

 import javax.swing.SwingConstants;

 import javax.swing.ImageIcon;

 import javax.swing.JButton;

 import javax.swing.JTextArea;
 import javax.swing.Timer;

 import java.awt.event.ActionListener;
 import java.awt.event.FocusEvent;
 import java.awt.event.FocusListener;
 import java.awt.event.KeyAdapter;
 import java.awt.event.KeyEvent;
 import java.awt.event.ActionEvent;
 import java.awt.Font;

 import java.awt.*;
 import javax.swing.*;




 public class MonopolyGame {

private JFrame frame;
private JTextField textField;
private JTextArea textArea;
static Graphics g;
private JButton one;
private JButton two;
private JButton three;
private JButton four;
private JButton five;
private JButton six;
private Timer timer1 = null;
private Timer timer2 = null;
private Timer timer3 = null;
private Timer timer4 = null;
private Timer timer5 = null;
private Timer timer6 = null;
int i=0;
/**
 * Launch the application.
 */
public static void main(String[] args) {
    EventQueue.invokeLater(new Runnable() {
        public void run() {

            try {
                MonopolyGame window = new MonopolyGame();
                window.frame.setVisible(true);
            } catch (Exception e) {
                e.printStackTrace();
            }

        }
    });


}

/**
 * Create the application.
 */
public MonopolyGame() {
    initialize();
}

 /**
 * Initialize the contents of the frame.
 */

private void initialize() {
    frame = new JFrame();
    frame.setBounds(0, 0, 1200, 800);
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.getContentPane().setLayout(null);
    frame.setVisible(true);

     JLayeredPane LPane;
     {
     LPane = new JLayeredPane();
     frame.getContentPane().add(LPane);
     JLabel board = new JLabel(new ImageIcon ("/Users/gavincoll/Documents/workspace/MonopolyStarboy/src/monopoly-board-blank.jpg"));
     board.setBounds(7,50, 700, 700);
     LPane.add(board, new Integer(1));
     }
    textField = new JTextField();
    textField.setText("Enter a command:");
    textField.setBounds(720, 680, 350, 70);
    frame.getContentPane().add(textField);
    textField.setColumns(10);
    textField.setBackground(Color.YELLOW);


      textField.addFocusListener(new FocusListener() {
            public void focusGained(FocusEvent e) {
                textField.setText("");
            }

            public void focusLost(FocusEvent e) {
                // nothing
            }
        });


        JButton send = new JButton("Send");
        send.setFont(new Font("Tahoma", Font.BOLD, 15));
        send.setBounds(1068, 680, 85, 70);
        send.setBackground(Color.GREEN);
        frame.getContentPane().add(send);


        send.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent arg0) {

             textArea.append(textField.getText()+ '\n');
             String text = textField.getText();
             String run = "Run";
             textField.setText("");
             if(i==0){
               one.setBackground(Color.RED);

               if(text.equals(run))
           {
               textArea.append("\n\n***Test Running***" + "\n\n");
               textArea.append("***Echo Mode Initialised***" + "\n");
           }

             }        

        }
    });

        KeyAdapter Enter = new KeyAdapter(){
            @Override
            public void keyPressed(KeyEvent e) {
                if(e.getKeyCode() == KeyEvent.VK_ENTER){
                    send.doClick();
                }
            }
        };
        textField.addKeyListener(Enter);

        Container contentPane = frame.getContentPane();
        contentPane.setLayout(null);



 /*    
       JLabel img = new JLabel(new ImageIcon("/Users/gavincoll/Documents/workspace/MonopolyStarboy/src/monopoly-board-blank.jpg"));
        img.setBounds(7,50, 700, 700); // x, y, width, height
        contentPane.add(img); 

      //PLAYER TOKENS

        JLabel tokeoneone = new JLabel( new ImageIcon("/Users/gavincoll/Documents/workspace/MonopolyStarboy/src/TokenRed.jpg"));

        tokeoneone.setBounds(7, 60, 700, 700);
        contentPane.add(tokeoneone); */

   /*     JLayeredPane LPane =new JLayeredPane();
        JLabel first = new JLabel(new ImageIcon ("/Users/gavincoll/Documents/workspace/MonopolyStarboy/src/monopoly-board-blank.jpg"));
        first.setBounds(7,50, 700, 700);
        frame.getContentPane().add(LPane); */


    textArea = new JTextArea();
    textArea.setText("Commands & Info Appear Here" + "\n");
    textArea.setBounds(725, 275, 425, 400);
    textArea.setBackground(Color.LIGHT_GRAY); 
    textArea.setForeground(Color.RED);
    textArea.setEditable(false); 
    textArea.append("Player 1: Red Token" + "\n");
    textArea.append("Player 2: Blue Token" + "\n");
    textArea.append("Player 3: Yellow Token" + "\n");
    textArea.append("Player 4: Green Token" + "\n");
    textArea.append("Player 5: Orange Token" + "\n");
    textArea.append("Player 6: Pink Token" + "\n\n"); 
    textArea.append("Enter 'Run' to Start The Test Program" + "\n\n");


    frame.getContentPane().add(textArea); 



    JLabel label = new JLabel("The Starboy's Monopoly - V1", SwingConstants.CENTER);
    label.setFont(new Font("Tahoma", Font.BOLD, 26));
    label.setForeground(Color.red);
    label.setBackground(Color.yellow);
    label.setBounds(0, 0, 1200, 40);
    label.setOpaque(true);
    frame.add(label);

    JLabel label2 = new JLabel("  Sprint One", SwingConstants.LEFT);
    label2.setFont(new Font("Arial", Font.BOLD, 16));
    label2.setForeground(Color.white);
    label2.setBackground(Color.lightGray);
    label2.setBounds(0, 758, 1200, 20);
    label2.setOpaque(true);
    frame.add(label2);






    one = new JButton("Roll");
    one.setFont(new Font("Tahoma", Font.BOLD, 20));
    one.setBounds(725, 66, 89, 67);
    frame.getContentPane().add(one);

    one.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(final java.awt.event.ActionEvent evt) {
                JOptionPane.showMessageDialog(null, "This button is not valid for this Sprint");
            }
        });

    two = new JButton("Buy");
    two.setFont(new Font("Tahoma", Font.BOLD, 20));
    two.setBounds(860, 66, 89, 67);
    frame.getContentPane().add(two);

    two.addActionListener(new java.awt.event.ActionListener() {
        public void actionPerformed(final java.awt.event.ActionEvent evt) {
            JOptionPane.showMessageDialog(null, "This button is not valid for this Sprint");
        }
    });

    three = new JButton("Sell");
    three.setFont(new Font("Tahoma", Font.BOLD, 20));
    three.setBounds(725, 175, 89, 67);
    frame.getContentPane().add(three);

    three.addActionListener(new java.awt.event.ActionListener() {
        public void actionPerformed(final java.awt.event.ActionEvent evt) {
            JOptionPane.showMessageDialog(null, "This button is not valid for this Sprint");
        }
    });

    four = new JButton("Build");
    four.setFont(new Font("Tahoma", Font.BOLD, 15));
    four.setBounds(1000, 175, 89, 67);
    frame.getContentPane().add(four);

    four.addActionListener(new java.awt.event.ActionListener() {
        public void actionPerformed(final java.awt.event.ActionEvent evt) {
            JOptionPane.showMessageDialog(null, "This button is not valid for this Sprint");
        }
    });

    five = new JButton("Quit");
    five.setFont(new Font("Tahoma", Font.BOLD, 20));
    five.setBounds(860, 175, 89, 67);
    frame.getContentPane().add(five);

    five.addActionListener(new java.awt.event.ActionListener() 
    {
        public void actionPerformed(final java.awt.event.ActionEvent evt) {
            int n = JOptionPane.showConfirmDialog(  
                    null,
                    "Are you sure?" ,
                    "",
                    JOptionPane.YES_NO_OPTION);

          if(n == JOptionPane.YES_OPTION)
          {
              JOptionPane.showMessageDialog(null, "Goodbye!");
              System.exit(0);
          }
          else
          {
              JOptionPane.showMessageDialog(null, "Enjoy the game!");


        }
        }});




    six = new JButton("Music");
    six.setFont(new Font("Tahoma", Font.BOLD, 20));
    six.setBounds(1000,66 , 89, 67);
    frame.getContentPane().add(six);

    six.addActionListener(new java.awt.event.ActionListener() {
        public void actionPerformed(final java.awt.event.ActionEvent evt) {




        }
    });


}}

其他具有棋盘图像和代币的类

import java.awt.*;

import javax.swing.*;

public class bgPanel

{
JFrame frame;
JLayeredPane LPane;



{
     frame = new JFrame();
     LPane =new JLayeredPane();

     frame.getContentPane() .add(LPane);

     JLabel board = new JLabel(new ImageIcon ("/Users/gavincoll/Documents/workspace/MonopolyStarboy/src/monopoly-board-blank.jpg"));

     board.setBounds(7,50, 700, 700);




     JLabel bluetoken = new JLabel(new ImageIcon ("/Users/gavincoll/Documents/workspace/MonopolyStarboy/src/TokenBlue.png"));

     bluetoken.setBounds(620,685, 20, 20);

     JLabel redtoken = new JLabel(new ImageIcon ("/Users/gavincoll/Documents/workspace/MonopolyStarboy/src/TokenRed.png"));

     redtoken.setBounds(645,685, 20, 20);

     JLabel whitetoken = new JLabel(new ImageIcon ("/Users/gavincoll/Documents/workspace/MonopolyStarboy/src/TokenWhite.png"));

     whitetoken.setBounds(675,685, 20, 20);

     JLabel greentoken = new JLabel(new ImageIcon ("/Users/gavincoll/Documents/workspace/MonopolyStarboy/src/TokenGreen.png"));

     greentoken.setBounds(620, 715, 20, 20);

     JLabel pinktoken = new JLabel(new ImageIcon ("/Users/gavincoll/Documents/workspace/MonopolyStarboy/src/TokenPink.png"));

     pinktoken.setBounds(645, 715, 20, 20);

     JLabel yellowtoken = new JLabel(new ImageIcon ("/Users/gavincoll/Documents/workspace/MonopolyStarboy/src/TokenYellow.png"));

     yellowtoken.setBounds(675, 715, 20, 20);


     LPane.add(board, new Integer(1));

     LPane.add(bluetoken, new Integer(2));

     LPane.add(redtoken, new Integer(3));

     LPane.add(whitetoken, new Integer(4));

     LPane.add(greentoken, new Integer (5));

     LPane.add(pinktoken, new Integer (6));

     LPane.add(yellowtoken, new Integer (7));

     frame.setDefaultCloseOperation(frame.EXIT_ON_CLOSE);

     frame.setSize (1200,800) ;

     frame.setVisible(true);

 }

}

0 个答案:

没有答案