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

ArrayAdapter and retrofit 2.0
Selecting a date using custom calenderview
Securing a class file in Android Project
caesercipher( unable to work if method has to cycle through alphabets)
Need to put semicolon at the end of last line
Microsoft JDBC driver URL with Kerberos Authentication and ServerSpn
matlab java class gives nullpointer exception
Java writing/editing a properties file
WorldWind support for right click context menu
Unsupported major.minor version 52.0 Servlets
LineChart creation error with MPAndroid Chart, unable to start activity
Does DisplayTag PaginatedList work with external sorting?
Encoding and Decoding bin file
How to make my own List<T> implementation sortable?
RxJava infinite retry with new observable
Object rotation and movement in current facing direction; Java

Categories

HOME
ngrx
drivers
objectgears
filtering
google-oauth
amortized-analysis
google-project-tango
django-imagekit
constraint-programming
directx
cross-validation
node-pdfkit
jplayer
visual-studio-cordova
quickfix
hapi
lombok
dosgi
http-status-code-504
flux
textfield
visjs
object-detection
css-animations
mustache.php
jndi
dynamic-featured-image
bootstrap-duallistbox
pingfederate
traffic
spring-mybatis
quote
fabric8
media-queries
semantic-versioning
jspresso
google-qpx-express-api
calibre
perlin-noise
framemaker
repo
bower-install
jmonkeyengine
web-mining
particles.js
outlook-api
unixodbc
wso2carbon
avconv
rainbowtable
dropbox-php
powercli
git-diff
svmlight
nodebb
dtexec
akka-cluster
sqlbulkcopy
orthogonal
database-optimization
file-writing
phishing
plottable.js
bstr
jwplayer7
lua-5.1
freedesktop.org
spim
sailfish-os
sniffer
formatjs
block-device
tt-news
android-nested-fragment
rdtsc
preferences
issuu
bitcoinj
responsive-slides
valuechangelistener
hippomocks
didselectrowatindexpath
tidy
batterylevel
rabl
pushbackinputstream
sharp-repository
simba
isnullorempty
spring-portlet-mvc
subscript
table-footer
recent-documents
imac
mongomapper
remember-me
suppress
javap
uimenucontroller
noscript
ugc
windows-live-messenger
3gp
rtml
document-conversion

Resources

Mobile Apps Dev
Database Users
javascript
java
csharp
php
android
MS Developer
developer works
python
ios
c
html
jquery
RDBMS discuss
Cloud Virtualization
Database Dev&Adm
javascript
java
csharp
php
python
android
jquery
ruby
ios
html
Mobile App
Mobile App
Mobile App