How to make a JInternalFrame non-clickable?
Author |
Message |
Blockgen
|
Posted: Fri Jun 07, 2013 8:27 pm Post subject: How to make a JInternalFrame non-clickable? |
|
|
I'm trying to make a game where you have to shoot down a certain object based on certain information. I want to display this information in a JInternalFrame.
However if someone clicks or closes the JInternalFrame, then they cannot return to the game, it will just be frozen. Is there a way to avoid this? |
|
|
|
|
![](images/spacer.gif) |
Sponsor Sponsor
![Sponsor Sponsor](templates/subSilver/images/ranks/stars_rank5.gif)
|
|
![](images/spacer.gif) |
Zren
![](http://compsci.ca/v3/uploads/user_avatars/1110053965512db6185954b.png)
|
Posted: Fri Jun 07, 2013 8:39 pm Post subject: RE:How to make a JInternalFrame non-clickable? |
|
|
Did you look at the documentation for the class? |
|
|
|
|
![](images/spacer.gif) |
Blockgen
|
Posted: Fri Jun 07, 2013 9:02 pm Post subject: Re: How to make a JInternalFrame non-clickable? |
|
|
Just did. The most relevant method is the isSelected method, which i can use in an if statement so that if isSelected == true then I can dispose of it. However I don't know where exactly to put it, since I'm using JLayeredPane as well, and that doesn't give me a name for the JInternalFrame.
Here is my code for reference:
code: | package mi;
import java.awt.*;
import java.awt.Graphics.*;
import javax.swing.*;
import java.awt.event.*;
import java.awt.event.KeyEvent;
import java.util.Random;
public class Game implements KeyListener {
ImageIcon stars = new ImageIcon("resources/background.jpg");
ImageIcon sship = new ImageIcon("resources/ship.png");
ImageIcon life = new ImageIcon("resources/life.png");
ImageIcon bad = new ImageIcon("resources/bad.png");
Font text = new Font("Arial", Font.PLAIN, 18);
JFrame f = new JFrame("Multiplication Nation");
JLayeredPane lp = new JDesktopPane();
static JLabel ship;
int shipx = 100;
int shippos = 2, badpos;
Random random = new Random();
JLabel[] eship, hearts;
static int nolife = -1;
public Game() {
//super ("Multiplication Nation");
f.setResizable(false);
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.setBounds(200, 100, 406, 522);
//f.setLayout(null);
f.setFocusable(true);
f.addKeyListener(this);
Container content = f.getContentPane();
//lp.setBounds(0, 0, 406, 522);
//lp.setOpaque(false);
JLabel background = new JLabel(stars);
background.setSize(400, 500);
//Life
hearts = new JLabel[Menu.heart()];//change to variable for health
int heartx = 360;
for (int i = 0; i != Menu.heart(); i++) //change to variable for health
{
hearts[i] = new JLabel(life);
hearts[i].setBounds(heartx, 10, 30, 27);
heartx -= 35;
lp.add(hearts[i], new Integer(2));
}
//change to variable for health
eship = new JLabel[4];
int badx = 0;
for (int i = 0; i != 4; i++) //change to variable for health
{
eship[i] = new JLabel(bad);
eship[i].setBounds(badx, 50, 100, 100);
badx += 100;
lp.add(eship[i], new Integer(2));
}
JLabel x = new JLabel("X");
badpos = 1 + random.nextInt(4);
int[] xx = {30, 130, 230, 330};
x.setBounds(xx[badpos - 1], 50, 18, 20);
x.setForeground(Color.RED);
//Ship
ship = new JLabel(sship);
ship.setBounds(shipx, 370, 100, 100);
lp.add(background, new Integer(1));
lp.add(ship, new Integer(2));
lp.add(x, new Integer(3));
lp.add(makeLayer("test"),new Integer (2));
content.add(lp,BorderLayout.CENTER);
f.add(lp);
f.show();
}
public void keyPressed(KeyEvent a) {
int keyCode = a.getKeyCode();
if (keyCode == KeyEvent.VK_LEFT && shippos != 1) {
shipx -= 100;
ship.setLocation(shipx, 370);
shippos -= 1;
}
if (keyCode == KeyEvent.VK_RIGHT && shippos != 4) {
shipx += 100;
ship.setLocation(shipx, 370);
shippos += 1;
}
if (keyCode == KeyEvent.VK_SPACE && shippos == badpos) {
eship[badpos - 1].setVisible(false);
} else if (keyCode == KeyEvent.VK_SPACE && shippos != badpos) {
nolife++;
hearts[nolife].setVisible(false);
}
}
public void keyReleased(KeyEvent a) {
}
public void keyTyped(KeyEvent a) {
}
public static void main(String[] args) {
new Game();
}
public static JInternalFrame makeLayer(String label) {
return new InternalFrameMaker(label);
}
}
|
Here's the InternalFrameMaker class
code: | package mi;
import java.awt.*;
import javax.swing.*;
public class InternalFrameMaker extends JInternalFrame {
public InternalFrameMaker(String name) {
getContentPane().add(new JLabel(name), BorderLayout.SOUTH);
setBounds(100, 100, 100, 100);
setResizable(false);
setClosable(true);
setMaximizable(false);
setIconifiable(false);
setTitle(name);
setVisible(true);
}
}
|
|
|
|
|
|
![](images/spacer.gif) |
Zren
![](http://compsci.ca/v3/uploads/user_avatars/1110053965512db6185954b.png)
|
Posted: Fri Jun 07, 2013 9:04 pm Post subject: RE:How to make a JInternalFrame non-clickable? |
|
|
What about setCloseable()? |
|
|
|
|
![](images/spacer.gif) |
Blockgen
|
Posted: Fri Jun 07, 2013 9:12 pm Post subject: Re: How to make a JInternalFrame non-clickable? |
|
|
They'll still be able to click it, which will make the program useless. Even if the JInternalFrame is gone, it still won't work
. |
|
|
|
|
![](images/spacer.gif) |
Blockgen
|
Posted: Fri Jun 07, 2013 9:14 pm Post subject: Re: How to make a JInternalFrame non-clickable? |
|
|
Wow. I just tried the program again, and when I closed the JInternalFrame, the game kept working. And now I tried it again, and it doesn't. |
|
|
|
|
![](images/spacer.gif) |
Blockgen
|
Posted: Fri Jun 07, 2013 9:21 pm Post subject: Re: How to make a JInternalFrame non-clickable? |
|
|
Hmm, so apparently I have to click outside the whole JFrame, and then click it again to make it work. Do you know of a possible way to circumvent this? |
|
|
|
|
![](images/spacer.gif) |
chromium
|
Posted: Sat Jun 08, 2013 9:08 am Post subject: Re: How to make a JInternalFrame non-clickable? |
|
|
.setEnabled(false) |
|
|
|
|
![](images/spacer.gif) |
Sponsor Sponsor
![Sponsor Sponsor](templates/subSilver/images/ranks/stars_rank5.gif)
|
|
![](images/spacer.gif) |
Blockgen
|
Posted: Sat Jun 08, 2013 4:31 pm Post subject: Re: How to make a JInternalFrame non-clickable? |
|
|
I can't do that because I'm extending the JInternalFrame. So if I were to make a button, I'd have no variable to put for it. |
|
|
|
|
![](images/spacer.gif) |
Zren
![](http://compsci.ca/v3/uploads/user_avatars/1110053965512db6185954b.png)
|
Posted: Sat Jun 08, 2013 4:45 pm Post subject: RE:How to make a JInternalFrame non-clickable? |
|
|
I just noticed you put setClosable(true). Don't you want setClosable(false)? |
|
|
|
|
![](images/spacer.gif) |
chromium
|
|
|
|
![](images/spacer.gif) |
Blockgen
|
Posted: Sat Jun 08, 2013 7:20 pm Post subject: Re: How to make a JInternalFrame non-clickable? |
|
|
If I set it to false, then If someone was to click the JInternalFrame, then there would be no way to return to the game. |
|
|
|
|
![](images/spacer.gif) |
Brendon77
|
Posted: Sat Jul 20, 2013 12:19 am Post subject: Re: How to make a JInternalFrame non-clickable? |
|
|
If you know please tell me, what way is best for return the game? |
|
|
|
|
![](images/spacer.gif) |
Zren
![](http://compsci.ca/v3/uploads/user_avatars/1110053965512db6185954b.png)
|
Posted: Sat Jul 20, 2013 11:34 am Post subject: Re: How to make a JInternalFrame non-clickable? |
|
|
Brendon77 @ Sat Jul 20, 2013 12:19 am wrote: If you know please tell me, what way is best for return the game?
Eh? |
|
|
|
|
![](images/spacer.gif) |
nullptr
|
Posted: Sun Jul 21, 2013 9:45 pm Post subject: Re: How to make a JInternalFrame non-clickable? |
|
|
If I'm understanding you correctly, you want an area that can't be clicked or closed, and that's part of the UI. In that case, why not just use a JPanel or a JTextArea? |
|
|
|
|
![](images/spacer.gif) |
|
|