java


JLayeredPane Not Appearing


I am trying to implement the following JLayeredPane into my code but it doesn't seem to appear - even though it works perfectly in another class. Is there a way to make it work in my main class or else call the other class for it to work. The bgPanel class has the board image and token images with a JLayeredPane and I want for what appears in that to appear on the main JFrame that already contains the rest of the GUI.
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));
}
Main Monopoly Class:
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) {
}
});
}}
Other Class with board image and tokens:
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);
}
}

Related Links

work with integer array in java [duplicate]
Temp file won't delete on exit - why is this?
On screen text validation using selenium webdriver
JavaFX adding new window signature
Java generic: why type erasure won't allow check instance of generic [duplicate]
Apache Commons Compress unable to add hard link in tar
AmazonS3Builder produces valid AmazonS3 object but a null is ultimately returned
Control if a 3x3 matrix is in an N x N Matrix with N>=3
Added component-scan to a legacy Spring application
Android SQLite - Wrong column when drop and re-create table with same table name
Opening new view inside ViewPager
what is difference between thread.sleep(9000l) and thread.sleep(9000)
Why is my PVector which should remain constant being modified in this sketch
How to Compare two String for a particular value in Java [duplicate]
How to prevent Spring from instantiating a bean from a third-party library without excluding the whole autoconfiguration? [duplicate]
Tomcat 8 java.lang.LinkageError: loader constraint violation

Categories

HOME
osgi
asp.net-core
zeromq
oracle11g
hashmap
objectgears
xmpp
paw-app
jsrender
electronics
baqend
installshield
row
gz
messages
hapi
jsprit
netflix
openrefine
pythonanywhere
jprofiler
custom-wordpress-pages
java-3d
tar
wijmo
p-value
xpages-ssjs
django-storage
facebook-access-token
user-interaction
brunch
saas
webkitspeechrecognition
directx-10
bcd
fog
lxd
form-data
atl
bower-install
io-redirection
suricata
disassembling
android-browser
flashair
apple-news
google-datalayer
outlook-2013
appcompat
apache-fop
strptime
promela
dandelion
sage-one
knockout-components
media-player
epson
linode
dwscript
redundancy
sonarlint-vs
spring-android
angular-cache
gridpane
python-stackless
rvest
lib.web.mvc
metaclass
retina
iis-arr
tableau-online
responsive-images
splash
appfabric-cache
remobjects
sorl-thumbnail
gui-test-framework
codeigniter-routing
asp.net-web-api-odata
heisenbug
expected-exception
valuechangelistener
meteor-velocity
multiprocessor
gridfs
ruby-datamapper
dbconnection
easy-install
jplaton
hungarian-algorithm
comaddin
distutils
first-responder
automount
appconkit
nsmanagedobject
removeclass
coercion
f#-powerpack
recent-documents
dbal
nintendo-ds
ctp4
document-conversion

Resources

Database Users
RDBMS discuss
Database Dev&Adm
javascript
java
csharp
php
android
javascript
java
csharp
php
python
android
jquery
ruby
ios
html
Mobile App
Mobile App
Mobile App