switching between a gif and a png on a JLabel - java

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);
}
}
}

Related

how to make a jscrollpane scroll from the start to the end?

I'm trying to make a jscorllpane automatic scroll from the start to the end. I use set value method for verticalbar but it's doesn't work. How can i make it scroll ?
Here is my code: When i run this code it just jump to the end of the jscorllpane instead of scorlling step by step
import java.awt.event.*;
import javax.swing.*;
import java.awt.BorderLayout;
import java.awt.Component;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
class JScrollPaneToTopAction implements ActionListener {
JScrollPane scrollPane;
public JScrollPaneToTopAction(JScrollPane scrollPane) {
if (scrollPane == null) {
throw new IllegalArgumentException("JScrollPaneToTopAction: null JScrollPane");
}
this.scrollPane = scrollPane;
}
public void actionPerformed(ActionEvent actionEvent) {
run();
}
public void run() {
JScrollBar verticalBar = scrollPane.getVerticalScrollBar();
int i = verticalBar.getMinimum();
while (i < verticalBar.getMaximum()) {
verticalBar.setValue(verticalBar.getMinimum()+i);
i += 50;
try {
Thread.sleep(100);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
scrollPane.getParent().invalidate();
scrollPane.repaint();
scrollPane.invalidate();
System.out.println("changing " + i);
}
}
}
public class Draft20 {
public static void main(String args[]) {
JFrame frame = new JFrame("Tabbed Pane Sample");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JTextArea tr = new JTextArea();
JLabel label = new JLabel("Label");
label.setPreferredSize(new Dimension(1000, 1000));
JScrollPane jScrollPane = new JScrollPane(label);
JButton bn = new JButton("Move");
bn.addActionListener(new JScrollPaneToTopAction(jScrollPane));
frame.add(bn, BorderLayout.SOUTH);
frame.add(jScrollPane, BorderLayout.CENTER);
frame.setSize(400, 150);
frame.setVisible(true);
}
}
I think the problem is your calls to repaint are queued for computation by EDT, but EDT is busy executing your actionPerformed. EDT will execute your repaint requests only after your run() completes. That's why you can see only the final result.
I'm not sure I can make it clear... check this code, it works for me:
import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JScrollBar;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.SwingUtilities;
class JScrollPaneToTopAction implements ActionListener, Runnable {
JScrollPane scrollPane;
public JScrollPaneToTopAction(JScrollPane scrollPane) {
if (scrollPane == null) {
throw new IllegalArgumentException("JScrollPaneToTopAction: null JScrollPane");
}
this.scrollPane = scrollPane;
}
#Override
public void actionPerformed(ActionEvent actionEvent) {
new Thread(this).start();
}
#Override
public void run() {
JScrollBar verticalBar = scrollPane.getVerticalScrollBar();
int i = verticalBar.getMinimum();
while (i < verticalBar.getMaximum()) {
verticalBar.setValue(verticalBar.getMinimum() + i);
i += 50;
try {
Thread.sleep(100);
}
catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
SwingUtilities.invokeLater(new Runnable() {
#Override
public void run() {
scrollPane.getParent().invalidate();
scrollPane.repaint();
scrollPane.invalidate();
}
});
System.out.println("changing " + i);
}
}
}
public class Draft20 {
public static void main(String args[]) {
JFrame frame = new JFrame("Tabbed Pane Sample");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JTextArea tr = new JTextArea();
JLabel label = new JLabel("Label");
label.setPreferredSize(new Dimension(1000, 1000));
JScrollPane jScrollPane = new JScrollPane(label);
JButton bn = new JButton("Move");
bn.addActionListener(new JScrollPaneToTopAction(jScrollPane));
frame.add(bn, BorderLayout.SOUTH);
frame.add(jScrollPane, BorderLayout.CENTER);
frame.setSize(400, 150);
frame.setVisible(true);
}
}
I hope it helps.

Simple Java: How to make string replace and wait then replace again?

I am making something and there will be a "Calculating" page on Java on an Applet! so what i want it to do is first drawstring and display "Calculating." then after a second it replaces that string and says "Calculating.." then again replace that string with "Calculating..." and loop that about 5 times. Is there any simple way of doing this??
I want it to display it on the applet!
You either want to use a Swing Timer or SwingWorker. See How to use Swing Timers and Worker Threads and SwingWorker for more details.
For example...
import java.awt.Dimension;
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;
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 {
private JLabel label;
private static final String DOTS = "...";
private static final String TEXT = "Calculating";
private int counter;
public TestPane() {
setLayout(new GridBagLayout());
label = new JLabel(getText());
add(label);
Timer timer = new Timer(1000, new ActionListener() {
#Override
public void actionPerformed(ActionEvent e) {
counter++;
if (counter > 3) {
counter = 0;
}
label.setText(getText());
}
});
timer.start();
}
protected String getText() {
String sufix = DOTS.substring(0, counter);
sufix = String.format("%-3s", sufix);
return TEXT + sufix;
}
#Override
public Dimension getPreferredSize() {
return new Dimension(200, 200);
}
}
}
Adding this to an applet is about as easy as adding to a JFrame

GUI Animation: Slider Value between Action and Change classes?

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...

How do I wait for a timer to stop before executing code?

I have a Timer that is used in a class that extends JPanel for an animation, and an ActionListener listens to it and makes actionPerformed run, which does repainting and stops the timer when needed. But the method that starts the timer, animatePanel, continues executing as the timer is running, which is what I do NOT want. I want it to wait until the timer has stopped to return.
The Timer is initialized in the class's constructor like this:
timer = new Timer(5, taskPerformer);
And this is what it does. I have something call animatePanel():
private ActionListener taskPerformer = new ActionListener() {
public void actionPerformed(ActionEvent evt) {
...
if (some conditions){
...
timer.stop();
...
return;
}
...
}
};
private void animatePanel() {
...
timer.start();
System.out.println("Timer stopped."); //always executes before the timer has stopped :(
//then returns and lets the rest of my program run while the timer is still going, which is BAD
}
The timer works fine except that in some cases, animatePanel() will return too soon and let the rest of my program run, causing problems.
You CAN NOT do this from within the context of the Event Dispatching Thread, doing so will make you application hang!
The timer must be started in a separate Thread. This then lets you take advantage of the thread monitoring API.
import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.EventQueue;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.logging.Level;
import java.util.logging.Logger;
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 WaitForTimer {
public static void main(String[] args) {
new WaitForTimer();
}
public WaitForTimer() {
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 static class TestPane extends JPanel {
protected static final Object WAIT_FOR = new Object();
private Timer timer;
private int tickCount = 0;
private JLabel ticks;
private JButton start;
public TestPane() {
timer = new Timer(250, new ActionListener() {
#Override
public void actionPerformed(ActionEvent e) {
tickCount++;
if (tickCount > 10) {
tickCount = 0;
timer.stop();
synchronized (WAIT_FOR) {
WAIT_FOR.notifyAll();
}
start.setEnabled(true);
}
ticks.setText(String.valueOf(tickCount));
}
});
timer.setRepeats(true);
timer.setCoalesce(true);
ticks = new JLabel("...");
start = new JButton("Start");
setLayout(new GridBagLayout());
GridBagConstraints gbc = new GridBagConstraints();
gbc.gridwidth = GridBagConstraints.REMAINDER;
add(ticks, gbc);
add(start, gbc);
start.addActionListener(new ActionListener() {
#Override
public void actionPerformed(ActionEvent e) {
start.setEnabled(false);
new Thread(new Runnable() {
#Override
public void run() {
System.out.println("Starting timer...");
timer.start();
synchronized (WAIT_FOR) {
try {
WAIT_FOR.wait();
} catch (InterruptedException ex) {
}
}
System.out.println("Timer finished...");
}
}).start();
}
});
}
#Override
public Dimension getPreferredSize() {
return new Dimension(200, 200);
}
}
}

how to minimize a panel inside a container

I have three buttons close,min and max.
When i want to max it then it will take the shape of the main container and overlaps all the panel and when i close it then only that panel gets affected. But when i hit the min button it gets minimized to the task bar which i do not want.
I want it inside conatiner like that of internalFrame when you click the minimize the button then it gets minimized inside the main frame.
here is the code
package Project;
import java.awt.Color;
import java.awt.Frame;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import javax.swing.JButton;
import javax.swing.JComponent;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;
import javax.swing.border.LineBorder;
public class MinPanel {
public MinPanel() {
createAndShowGui();
}
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
#Override
public void run() {
new MinPanel();
}
});
}
private void createAndShowGui() {
JFrame frame = new JFrame();
frame.setSize(300, 500);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
final JPanel mainPanel = new JPanel(new GridLayout(2, 2));
for (int i = 0; i < 4; i++) {
final int num = i;
OmniPanel op = new OmniPanel(mainPanel, frame) {
#Override
public JPanel createPanel() {
JPanel p = createSimplePanelInterface();
p.add(new JLabel("Panel " + (num + 1)));
return p;
}
#Override
void toPanel() {
super.toPanel();
System.out.println("Frame requested to be brought to panel");
}
};
mainPanel.add(op.getPanel());
}
frame.add(mainPanel);
//frame.pack();
frame.setVisible(true);
}
}
abstract class OmniPanel {
protected JFrame frame;
protected JPanel panel;
boolean maximized = false;
private final JComponent owner;
private final JFrame ownerFrame;
public OmniPanel(JComponent owner, JFrame con) {
this.owner = owner;
initOmniPanel();
this.ownerFrame = con;
}
private void initOmniPanel() {
panel = createPanel();
createFrame();
frame.addWindowListener(new WindowAdapter() {
#Override
public void windowDeiconified(WindowEvent we) {
super.windowDeiconified(we);
toPanel();
}
});
}
public JPanel getPanel() {
return panel;
}
public JFrame getFrame() {
return frame;
}
public boolean goFrame() {
frame.add(panel);
frame.pack();
frame.setState(JFrame.ICONIFIED);
frame.setVisible(true);
return true;
}
protected void createFrame() {
frame = new JFrame();
frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
}
void toPanel() {
frame.remove(panel);
frame.dispose();
owner.add(panel);
owner.revalidate();
owner.repaint();
}
public JPanel createSimplePanelInterface() {
JPanel p = new JPanel();
JButton close = new JButton("X");
JButton minimize = new JButton("_");
JButton maximize = new JButton("[]");
p.add(close);
p.add(minimize);
p.add(maximize);
close.addActionListener(new ActionListener() {
#Override
public void actionPerformed(ActionEvent ae) {
if (maximized) {
maximized = false;
ownerFrame.setGlassPane(new JComponent() {
});
ownerFrame.revalidate();
ownerFrame.repaint();
} else {
removePanelFromOwner();
getFrame().dispose();
}
}
});
minimize.addActionListener(new ActionListener() {
#Override
public void actionPerformed(ActionEvent ae) {
if (maximized) {
maximized = false;
ownerFrame.setGlassPane(new JComponent() {
});
owner.add(panel);
owner.revalidate();
owner.repaint();
ownerFrame.revalidate();
ownerFrame.repaint();
} else {
removePanelFromOwner();
goFrame();
frame.setState(Frame.ICONIFIED);
}
}
});
maximize.addActionListener(new ActionListener() {
#Override
public void actionPerformed(ActionEvent ae) {
if (maximized) {
return;
}
maximized = true;
removePanelFromOwner();
ownerFrame.setGlassPane(panel);
ownerFrame.revalidate();
ownerFrame.repaint();
panel.setVisible(true);//
}
});
p.setBorder(new LineBorder(Color.black));
return p;
}
private void removePanelFromOwner() {
owner.remove(getPanel());
owner.revalidate();
owner.repaint();
}
abstract JPanel createPanel();
}
I would suggest you to create an internalframe and then add buttons.If you do not want to add buttons then you make internalframe arguments true
import java.awt.Dimension;
import java.awt.GridLayout;
import java.awt.HeadlessException;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.beans.PropertyVetoException;
import javax.swing.JButton;
import javax.swing.JDesktopPane;
import javax.swing.JFrame;
import javax.swing.JInternalFrame;
import javax.swing.SwingUtilities;
import javax.swing.plaf.basic.BasicInternalFrameTitlePane;
import javax.swing.plaf.basic.BasicInternalFrameUI;
public class MinPanel {
public MinPanel() throws HeadlessException, PropertyVetoException {
createAndShowGUI();
}
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
#Override
public void run() {
try {
new MinPanel();
} catch (HeadlessException ex) {
} catch (PropertyVetoException ex) {
}
}
});
}
private void createAndShowGUI() throws HeadlessException, PropertyVetoException {
JFrame frame = new JFrame();
frame.setResizable(true);
frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
final JDesktopPane jdp = new JDesktopPane() {
#Override
public Dimension getPreferredSize() {
return new Dimension(400, 400);
}
};
frame.setContentPane(jdp);
frame.pack();
createAndAddInternalFrame(jdp, 0, 0);
createAndAddInternalFrame(jdp, 200, 0);
frame.setVisible(true);
}
private void createAndAddInternalFrame(final JDesktopPane jdp, int x, int y) throws PropertyVetoException {
final JInternalFrame jInternalFrame = new JInternalFrame("Test1", false, false, false, false);
jInternalFrame.setLocation(x, y);
jInternalFrame.setLayout(new GridLayout(2, 2));
jInternalFrame.setSize(200, 200);//testing
JButton jb = new JButton("min");
jInternalFrame.add(jb);
jb.addActionListener(new ActionListener() {
#Override
public void actionPerformed(ActionEvent ae) {
try {
jInternalFrame.setIcon(true);
} catch (PropertyVetoException ex) {
}
}
});
BasicInternalFrameTitlePane titlePane = (BasicInternalFrameTitlePane) ((BasicInternalFrameUI) jInternalFrame.getUI()).getNorthPane();
jInternalFrame.remove(titlePane);
jInternalFrame.setVisible(true);
jdp.add(jInternalFrame);
}
}

Categories