GUI Animation: Slider Value between Action and Change classes? - java

My GUI frame comes up now but the slider value (speed) I get from my slider doesn't appear in my ActionLlistener class where I need use it as a delay in my timer. How do I bring that value over? The point is to run 12 images, like frames, at a speed that is determined by the value they slide on. Like if they slide to 12, there will be 12 milliseconds between each image.
import javax.swing.*;
import java.awt.event.*;
import javax.swing.event.*;
import java.awt.*;
public class SliderGUI {
public static JSlider slider = new JSlider(JSlider.HORIZONTAL, 0, 100, 1);
public static JLabel label = new JLabel ();
public static JPanel panel = new JPanel();
public static int delay;
public static int speed;
public static ImageIcon imageIcon;
public static Timer timer = new Timer (delay, new SliderListener());
public static void main(String[] args) {
JFrame frame = new JFrame("Legend Of Zelda");
panel.setLayout(new GridLayout(5, 5, 5, 25));
slider.setPaintLabels(true);
slider.addChangeListener(new SliderListener());
System.out.println (speed);
timer.addActionListener (new SliderListener());
frame.setVisible(true);
frame.setResizable(true);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
panel.setBorder(BorderFactory.createEmptyBorder(10, 10, 10, 10));
panel.setLayout(new BoxLayout(panel, BoxLayout.PAGE_AXIS));
panel.add(label);
panel.add(slider);
frame.setContentPane(panel);
frame.pack();
}
private static class SliderListener implements ChangeListener, ActionListener {
public void stateChanged(ChangeEvent e) {
speed = slider.getValue();
slider.setMajorTickSpacing(25);
}
public void actionPerformed (ActionEvent e) {
for (int i = 1; i < 13; i++) {
if (i == 12){
i = 1;
}
imageIcon = new ImageIcon(i + ".jpg");
label.setIcon(imageIcon);
}
System.out.println ("Hi");
timer = new Timer(speed, new SliderListener());
timer.start();
}
}
}

Don't rely on static, it will blow up in your face...
You are creating multiple instances SliderListener when you really should be only creating one and applying it to the JSlider (and in your case) the Timer.
Having said that, I'd (personally) separate them...
You're also creating a new Timer each time the ActionListener is called (so like a thousand times a second! So after 1 second, you could have 1001 Timers running!) all of which are going to be calling SliderListener at the same time, and because it's all linked via global variables...face in explosion...
But I didn't actually see any where you were stating the Timer to start with...
import java.awt.Dimension;
import java.awt.EventQueue;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.imageio.ImageIO;
import javax.swing.BorderFactory;
import javax.swing.BoxLayout;
import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JSlider;
import javax.swing.Timer;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;
public class SliderGUI {
public static void main(String[] args) {
new SliderGUI();
}
public SliderGUI() {
EventQueue.invokeLater(new Runnable() {
#Override
public void run() {
try {
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
} catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
ex.printStackTrace();
}
JFrame frame = new JFrame("Testing");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.add(new TestPane());
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
});
}
public class TestPane extends JPanel {
public JSlider slider = new JSlider(JSlider.HORIZONTAL, 10, 100, 10);
public JLabel label;
public int delay;
public int speed;
public ImageIcon imageIcon;
public Timer timer;
public TestPane() {
setLayout(new GridLayout(5, 5, 5, 25));
slider.setPaintLabels(true);
label = new JLabel();
try {
BufferedImage frameImage = ImageIO.read(getClass().getResource("/Run-0.png"));
label.setIcon(new ImageIcon(frameImage));
} catch (IOException ex) {
ex.printStackTrace();
}
SliderListener sliderListener = new SliderListener();
timer = new Timer(delay, sliderListener);
slider.addChangeListener(sliderListener);
System.out.println(speed);
timer.addActionListener(sliderListener);
timer.start();
setBorder(BorderFactory.createEmptyBorder(10, 10, 10, 10));
setLayout(new BoxLayout(this, BoxLayout.PAGE_AXIS));
add(label);
add(slider);
}
private class SliderListener implements ChangeListener, ActionListener {
public void stateChanged(ChangeEvent e) {
int value = slider.getValue();
timer.setDelay(value);
}
private int frame = 0;
#Override
public void actionPerformed(ActionEvent e) {
System.out.println("Tick " + ((Timer) e.getSource()).getDelay());
try {
BufferedImage frameImage = ImageIO.read(getClass().getResource("/Run-" + frame + ".png"));
label.setIcon(new ImageIcon(frameImage));
} catch (IOException exp) {
exp.printStackTrace();
}
frame++;
if (frame > 11) {
frame = 0;
}
}
}
}
}
Also, remember, a Timer is like a loop, each time the ActionListener is called, you should treat it as an iteration of a loop and update the state accordingly...

Related

How to make a border around Jbutton thicker?

I have the following JButton on the GUI interface I'm building.
I want to make the border around the button more thicker so it will stand out from the background. Is it possible to do this in Java?
You could simply use a LineBorder
JButton btn = ...;
btn.setBorder(BorderFactory.createLineBorder(Color.BLACK, 4));
Take a look at How to Use Borders for more details and ideas
Updating the border state based on the model state
import java.awt.Color;
import java.awt.EventQueue;
import java.awt.GridBagLayout;
import javax.swing.BorderFactory;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;
import javax.swing.border.Border;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;
public class Test {
public static void main(String[] args) {
new Test();
}
public Test() {
EventQueue.invokeLater(new Runnable() {
#Override
public void run() {
try {
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
} catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
ex.printStackTrace();
}
JFrame frame = new JFrame("Testing");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.add(new TestPane());
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
});
}
public static class TestPane extends JPanel {
protected static final Border NORMAL_BORDER = BorderFactory.createLineBorder(Color.BLACK, 4);
protected static final Border ROLLOVER_BORDER = BorderFactory.createLineBorder(Color.RED, 4);
public TestPane() {
JButton btn = new JButton("Click me!");
btn.setContentAreaFilled(false);
btn.setBorder(NORMAL_BORDER);
btn.getModel().addChangeListener(new ChangeListener() {
#Override
public void stateChanged(ChangeEvent e) {
if (btn.getModel().isRollover()) {
btn.setBorder(ROLLOVER_BORDER);
} else {
btn.setBorder(NORMAL_BORDER);
}
}
});
setLayout(new GridBagLayout());
add(btn);
}
}
}
Create a Border first -
Border border = new LineBorder(Color.WHITE, 13);
Then create a JButton and set the Border -
JButton button = new JButton("Button Name");
button.setBorder(border);
Hope it will Help.
Thanks a lot.

switching between a gif and a png on a JLabel

I'm trying to make a Russian Roulette game and I'm trying to change the JLabel to the gif of the revolver spinning. However, I can change it from the gif but not back to the picture of the revolver. Or is there a way to play the gif only once and have it stop then change it to another gif?
import java.awt.Image;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
public class RussianRoulette extends JFrame {
private int Chamber;
private int BulletPos;
private boolean ButtonToggle;
JButton Fire = new JButton("Fire");
JButton Spin = new JButton("Spin");
JLabel Gun = new JLabel();
public RussianRoulette() {
ButtonToggle = true;
Chamber = (int) (Math.random() * 6 + 1);
BulletPos = 0;
JFrame frame = new JFrame();
frame.getContentPane().setLayout(null);
Gun.setBounds(0, 0, 500, 375);
Fire.setBounds(25, 375, 100, 100);
Spin.setBounds(350, 375, 100, 100);
ImageIcon imgThisImg = new ImageIcon("Revolver2.png");
Gun.setIcon(imgThisImg);
Spin.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent ae) {
spinGun();
sleep(600);
}
});
Fire.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent ae) {
fireGun();
}
});
frame.setDefaultCloseOperation(frame.EXIT_ON_CLOSE);
frame.add(Gun);
frame.add(Fire);
frame.add(Spin);
frame.setResizable(true);
frame.setSize(500, 500);
Image im = Toolkit.getDefaultToolkit().getImage("icon.png");
frame.setIconImage(im);
frame.setVisible(true);
frame.setResizable(false);
frame.setTitle("Russian Roulette");
}
public void spinGun() {
ImageIcon imgThisImg = new ImageIcon("SpinRevolver.gif");
Gun.setIcon(imgThisImg);
sleep(600);
AudioPlayer player3 = new AudioPlayer("Spin.wav");
player3.play();
BulletPos = (int) (Math.random() * 6 + 1);
}
public void fireGun() {
if (ButtonToggle == false) {
AudioPlayer player5 = new AudioPlayer("Click.wav");
player5.play();
}
if (Chamber == BulletPos && ButtonToggle == true) {
AudioPlayer player2 = new AudioPlayer("Shot.wav");
player2.play();
ButtonToggle = false;
} else {
Chamber++;
CheckNum();
AudioPlayer player = new AudioPlayer("Click.wav");
player.play();
}
}
public void CheckNum() {
if (Chamber > 6) {
Chamber = 1;
}
}
public void reload()
{
//play reload animation
ButtonToggle = true;
}
public void sleep(int ammount)
{
try {
Thread.sleep(ammount);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
So, based on feedback, you want the animation to start running on a button click and stop automatically after a given period of time...
Probably the simplest solution would be to use a javax.swing.Timer, otherwise you'll need to set yourself up as some kind of ImageObserver and interrupt the various events coming from the image...like I said, simpler...
import java.awt.BorderLayout;
import java.awt.EventQueue;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.Timer;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;
public class SpiningLabel {
public static void main(String[] args) {
new SpiningLabel();
}
public SpiningLabel() {
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 {
private ImageIcon spin;
private ImageIcon still;
private JLabel label;
private Timer timer;
private JButton button;
public TestPane() {
spin = new ImageIcon("spin.gif");
still = new ImageIcon("still.png");
label = new JLabel(still);
button = new JButton("Allons-y!");
button.addActionListener(new ActionListener() {
#Override
public void actionPerformed(ActionEvent e) {
label.setIcon(spin);
button.setEnabled(false);
timer.restart();
}
});
timer = new Timer(2000, new ActionListener() {
#Override
public void actionPerformed(ActionEvent e) {
label.setIcon(still);
button.setEnabled(true);
}
});
timer.setRepeats(false);
setLayout(new BorderLayout());
add(label);
add(button, BorderLayout.SOUTH);
}
}
}

Dynamic JLable Text

I'm a super Java noob and need some help. I am trying to write a code that shows the word "on" on the JPanel for 5 (or however long I pass into y variable) seconds then changes the word to "off" on the same JPanel. Think of a stoplight that shows green for a period of time then goes to red. The code I have written below opens up two separate JFrames to display the different words. Any help or ideas would be greatly appreciated.
import javax.swing.*;
public class practice extends JFrame implements Runnable {
int x;
int y;
JLabel show = new JLabel("on");
JLabel show2 = new JLabel("off");
boolean yes;
public practice(boolean on, int x){
x=y;
yes = on;
setTitle("Stoplight");
setSize(500, 500);
setResizable(true);
setVisible(true);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
public void test(){
try {
Thread.sleep(y);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
if (yes == true){
add(show2);
}else if (yes == false)
add(show);
}
public void run() {
test();
}
public static void main (String[] args){
Thread t1 = new Thread(new practice(true, 50000));
Thread t2 = new Thread(new practice(false, 0));
t1.start();
t2.start();
}
}
You need remove the label 'on' of panel before add the label 'off' with the method remove(jcomponent)
As has already been hinted, you should use a javax.swing.Timer, which will allow you to schedule a callback after a specified period of time.
Unless you have a particular need, it's simpler to change the text of the label to have to remove the old label and add a new one (IMHO)
For example...
import java.awt.BorderLayout;
import java.awt.EventQueue;
import java.awt.GridBagLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.Timer;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;
import javax.swing.border.EmptyBorder;
public class DynamicLabel {
public static void main(String[] args) {
new DynamicLabel();
}
public DynamicLabel() {
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(5000));
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
});
}
public class TestPane extends JPanel {
private JLabel label;
public TestPane(int delay) {
setLayout(new GridBagLayout());
setBorder(new EmptyBorder(8, 8, 8, 8));
label = new JLabel("On");
add(label);
Timer timer = new Timer(delay, new ActionListener() {
#Override
public void actionPerformed(ActionEvent e) {
label.setText("Off");
}
});
timer.setRepeats(false);
timer.start();
}
}
}

Java timer that starts when you click space, ends after clicking space again

I am trying to program a Rubiks cube timer. Once you click space I want the timer to countdown from 15 seconds, once the 15 seconds is over, start counting up from 0. When you are done solving the cube you click space again, stopping the timer (I want the timer to count to the nearest hundredth). Here is what I have now:
import java.awt.BorderLayout;
import java.awt.EventQueue;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import javax.swing.JLabel;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
public class CubeMain extends JFrame {
private static final long serialVersionUID = 1L;
private JPanel contentPane;
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
CubeMain frame = new CubeMain();
frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
public CubeMain() {
setTitle("Cube Timer");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setBounds(100, 100, 600, 490);
contentPane = new JPanel();
final JLabel Timerlbl = new JLabel("");
Timerlbl.setBounds(269, 219, 46, 14);
contentPane.add(Timerlbl);
addKeyListener(new KeyAdapter() {
#Override
public void keyReleased(KeyEvent e) {
Timerlbl.setText("Label Change");
}
});
contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
setContentPane(contentPane);
contentPane.setLayout(null);
}
}
So you know you need some kind of timer. The timer you want for a Swing program is a javax.swing.Timer. This is the basic constructor
Timer(int delay, ActionListener listener);
Where, delay is the delay time between fired actions, and the listener is what listens for those timer action events being fired each interval. A basic implementation would me something like this
public TimerPanel() {
Timer timer = new Timer(1000, new ActionListener(){
public void actionPerformed(ActionEvent e) {
// do something
}
});
timer.start();
}
What you could do is have a count variable that you increment and use to set the timerLabel. Then just set your key binding for the SPACE to timer.start() or timer.stop()
Take a look at this, it has the timer. It's pretty much what you need
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.AbstractAction;
import javax.swing.Action;
import javax.swing.ActionMap;
import javax.swing.InputMap;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.KeyStroke;
import javax.swing.SwingUtilities;
import javax.swing.Timer;
import javax.swing.border.EmptyBorder;
public class TimerPanel{
double count = 15.00;
boolean reverse = true;
boolean started = false;
private JLabel timerLabel = new JLabel(String.format("%.2f", count));
private Timer timer;
public TimerPanel() {
timerLabel.setHorizontalAlignment(JLabel.CENTER);
timerLabel.setBorder(new EmptyBorder(20, 20, 20, 20));
JFrame frame = new JFrame();
frame.add(timerLabel);
frame.pack();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setLocationByPlatform(true);
frame.setVisible(true);
timer = new Timer(10, new ActionListener(){
public void actionPerformed(ActionEvent e) {
if (reverse && count > 0) {
count -= 0.01;
timerLabel.setText(String.format("%.2f", count));
if (count <= 0) {
reverse = false;
}
}
if (!reverse){
count += 0.01;
timerLabel.setText(String.format("%.2f", count));
}
}
});
Action spaceAction = new AbstractAction() {
public void actionPerformed(ActionEvent e) {
if (!started) {
timer.start();
started = true;
} else {
timer.stop();
count = 15.00;
started = false;
reverse = true;
}
}
};
InputMap inputMap = timerLabel.getInputMap(JPanel.WHEN_IN_FOCUSED_WINDOW);
ActionMap actionMap = timerLabel.getActionMap();
inputMap.put(KeyStroke.getKeyStroke("SPACE"), "spaceAction");
actionMap.put("spaceAction", spaceAction);
}
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable(){
public void run() {
new TimerPanel();
}
});
}
}

JLayeredPane formatting issue

I have a problem with my JLayeredPane, I am probably doing something incredibly simple but I cannot wrap my head around it. The problem i have is that all the components are merged together and have not order. Could you please rectify this as I have no idea. The order I am trying to do is have a layout like this
output
label1 (behind)
input (in Front)
import java.awt.BorderLayout;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.io.IOException;
import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JTextArea;
import javax.swing.JTextField;
public class window extends JFrame implements KeyListener {
/**
*
*/
private static final long serialVersionUID = 7092006413113558324L;
private static int NewSize;
public static String MainInput;
public static JLabel label1 = new JLabel();
public static JTextField input = new JTextField(10);
public static JTextArea output = new JTextArea(main.Winx, NewSize);
public window() {
super("Satine. /InDev-01/");
JLabel label1;
NewSize = main.Winy - 20;
setLayout(new BorderLayout());
output.setToolTipText("");
add(input, BorderLayout.PAGE_END);
add(output, BorderLayout.CENTER);
input.addKeyListener(this);
input.requestFocus();
ImageIcon icon = new ImageIcon("C:\\Users\\" + System.getProperty("user.name") + "\\AppData\\Roaming\\.Satine\\img\\textbox.png", "This is the desc");
label1 = new JLabel(icon);
add(label1, BorderLayout.PAGE_END);
}
public void keyPressed(KeyEvent e) {
int key = e.getKeyCode();
if (key == KeyEvent.VK_ENTER) {
try {
MainMenu.start();
} catch (IOException e1) {
System.out.print(e1.getCause());
}
}
}
#Override
public void keyReleased(KeyEvent e) {
}
#Override
public void keyTyped(KeyEvent e) {
}
}
And the main class.
import java.awt.Container;
import java.io.IOException;
import javax.swing.JFrame;
import javax.swing.JLayeredPane;
public class main {
public static int Winx, Winy;
private static JLayeredPane lpane = new JLayeredPane();
public static void main(String[] args) throws IOException{
Winx = window.WIDTH;
Winy = window.HEIGHT;
window Mth= new window();
Mth.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
Mth.setSize(1280,720);
Mth.setVisible(true);
lpane.add(window.label1);
lpane.add(window.input);
lpane.add(window.output);
lpane.setLayer(window.label1, 2, -1);
lpane.setLayer(window.input, 1, 0);
lpane.setLayer(window.output, 3, 0);
Mth.pack();
}
}
Thank you for your time and I don't expect the code to be written for me, all I want is tips on where I am going wrong.
I recommend that you not use JLayeredPane as the overall layout of your GUI. Use BoxLayout or BorderLayout, and then use the JLayeredPane only where you need layering. Also, when adding components to the JLayeredPane, use the add method that takes a Component and an Integer. Don't call add(...) and then setLayer(...).
Edit: it's ok to use setLayer(...) as you're doing. I've never used this before, but per the API, it's one way to set the layer.
e.g.,
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.GridBagLayout;
import java.awt.image.BufferedImage;
import java.io.IOException;
import java.net.MalformedURLException;
import java.net.URL;
import javax.imageio.ImageIO;
import javax.swing.*;
public class LayeredPaneFun extends JPanel {
public static final String IMAGE_PATH = "http://duke.kenai.com/" +
"misc/Bullfight.jpg";
public LayeredPaneFun() {
try {
BufferedImage img = ImageIO.read(new URL(IMAGE_PATH));
ImageIcon icon = new ImageIcon(img);
JLabel backgrndLabel = new JLabel(icon);
backgrndLabel.setSize(backgrndLabel.getPreferredSize());
JPanel forgroundPanel = new JPanel(new GridBagLayout());
forgroundPanel.setOpaque(false);
JLabel fooLabel = new JLabel("Foo");
fooLabel.setFont(fooLabel.getFont().deriveFont(Font.BOLD, 32));
fooLabel.setForeground(Color.cyan);
forgroundPanel.add(fooLabel);
forgroundPanel.add(Box.createRigidArea(new Dimension(50, 50)));
forgroundPanel.add(new JButton("bar"));
forgroundPanel.add(Box.createRigidArea(new Dimension(50, 50)));
forgroundPanel.add(new JTextField(10));
forgroundPanel.setSize(backgrndLabel.getPreferredSize());
JLayeredPane layeredPane = new JLayeredPane();
layeredPane.setPreferredSize(backgrndLabel.getPreferredSize());
layeredPane.add(backgrndLabel, JLayeredPane.DEFAULT_LAYER);
layeredPane.add(forgroundPanel, JLayeredPane.PALETTE_LAYER);
setLayout(new BoxLayout(this, BoxLayout.PAGE_AXIS));
add(new JScrollPane(new JTextArea("Output", 10, 40)));
add(layeredPane);
} catch (MalformedURLException e) {
e.printStackTrace();
System.exit(1);
} catch (IOException e) {
e.printStackTrace();
System.exit(1);
}
}
private static void createAndShowGui() {
JFrame frame = new JFrame("LayeredPaneFun");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.getContentPane().add(new LayeredPaneFun());
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
public void run() {
createAndShowGui();
}
});
}
}

Categories