Can't dispose of jframe window?
I'm trying to dispose of the difficulty window after any one of the difficulty button's are clicked but it won't happen. I've tried .dispose
and frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
but i can't get it. Is it just placement or more?
import java.awt.FlowLayout;
import java.awt.event.*;
import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JTextField;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.GridLayout;
public class Game extends JFrame{
public static JFrame frame = new JFrame();
private JLabel lab;
public static void main(String args) {
Game difficulty = new Game();
difficulty.setSize(350,105);
difficulty.setTitle("Difficulty.");
difficulty.setVisible(true);
difficulty.setLocationRelativeTo(null);
/**Game sudoku = new Game();
sudoku.setSize(900, 900);
sudoku.setVisible(false);*/
}
public Game(){
setLayout(new FlowLayout());
lab = new JLabel("Please select your difficulty.");
add(lab);
JButton easy;
easy = new JButton("Easy");
add(easy);
easy.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e)
{
//Execute when button is pressed
System.out.println("You clicked the button");
JFrame.dispose();
}
});
JButton medium;
medium = new JButton("Medium");
add(medium);
JButton hard;
hard = new JButton("Hard");
add(hard);
JButton evil;
evil = new JButton("Evil!");
add(evil);
}
}
java swing jframe jbutton dispose
add a comment |
I'm trying to dispose of the difficulty window after any one of the difficulty button's are clicked but it won't happen. I've tried .dispose
and frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
but i can't get it. Is it just placement or more?
import java.awt.FlowLayout;
import java.awt.event.*;
import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JTextField;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.GridLayout;
public class Game extends JFrame{
public static JFrame frame = new JFrame();
private JLabel lab;
public static void main(String args) {
Game difficulty = new Game();
difficulty.setSize(350,105);
difficulty.setTitle("Difficulty.");
difficulty.setVisible(true);
difficulty.setLocationRelativeTo(null);
/**Game sudoku = new Game();
sudoku.setSize(900, 900);
sudoku.setVisible(false);*/
}
public Game(){
setLayout(new FlowLayout());
lab = new JLabel("Please select your difficulty.");
add(lab);
JButton easy;
easy = new JButton("Easy");
add(easy);
easy.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e)
{
//Execute when button is pressed
System.out.println("You clicked the button");
JFrame.dispose();
}
});
JButton medium;
medium = new JButton("Medium");
add(medium);
JButton hard;
hard = new JButton("Hard");
add(hard);
JButton evil;
evil = new JButton("Evil!");
add(evil);
}
}
java swing jframe jbutton dispose
add a comment |
I'm trying to dispose of the difficulty window after any one of the difficulty button's are clicked but it won't happen. I've tried .dispose
and frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
but i can't get it. Is it just placement or more?
import java.awt.FlowLayout;
import java.awt.event.*;
import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JTextField;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.GridLayout;
public class Game extends JFrame{
public static JFrame frame = new JFrame();
private JLabel lab;
public static void main(String args) {
Game difficulty = new Game();
difficulty.setSize(350,105);
difficulty.setTitle("Difficulty.");
difficulty.setVisible(true);
difficulty.setLocationRelativeTo(null);
/**Game sudoku = new Game();
sudoku.setSize(900, 900);
sudoku.setVisible(false);*/
}
public Game(){
setLayout(new FlowLayout());
lab = new JLabel("Please select your difficulty.");
add(lab);
JButton easy;
easy = new JButton("Easy");
add(easy);
easy.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e)
{
//Execute when button is pressed
System.out.println("You clicked the button");
JFrame.dispose();
}
});
JButton medium;
medium = new JButton("Medium");
add(medium);
JButton hard;
hard = new JButton("Hard");
add(hard);
JButton evil;
evil = new JButton("Evil!");
add(evil);
}
}
java swing jframe jbutton dispose
I'm trying to dispose of the difficulty window after any one of the difficulty button's are clicked but it won't happen. I've tried .dispose
and frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
but i can't get it. Is it just placement or more?
import java.awt.FlowLayout;
import java.awt.event.*;
import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JTextField;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.GridLayout;
public class Game extends JFrame{
public static JFrame frame = new JFrame();
private JLabel lab;
public static void main(String args) {
Game difficulty = new Game();
difficulty.setSize(350,105);
difficulty.setTitle("Difficulty.");
difficulty.setVisible(true);
difficulty.setLocationRelativeTo(null);
/**Game sudoku = new Game();
sudoku.setSize(900, 900);
sudoku.setVisible(false);*/
}
public Game(){
setLayout(new FlowLayout());
lab = new JLabel("Please select your difficulty.");
add(lab);
JButton easy;
easy = new JButton("Easy");
add(easy);
easy.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e)
{
//Execute when button is pressed
System.out.println("You clicked the button");
JFrame.dispose();
}
});
JButton medium;
medium = new JButton("Medium");
add(medium);
JButton hard;
hard = new JButton("Hard");
add(hard);
JButton evil;
evil = new JButton("Evil!");
add(evil);
}
}
java swing jframe jbutton dispose
java swing jframe jbutton dispose
edited Apr 8 '14 at 19:16
Bonifacio2
2,13332338
2,13332338
asked Apr 8 '14 at 18:48
user3512387user3512387
2415
2415
add a comment |
add a comment |
6 Answers
6
active
oldest
votes
dispose()
method is not a static, so it'll not work by calling it directly from JFrame
class
JFrame.dispose();
try to do :
dispose();
Or to dispose the frame
object you have created
frame.dispose();
Read more about JFrame
add a comment |
First of all you're extending JFrame and creating an object of JFrame, if I'm not wrong, this shouldn't be done.
public class Game extends JFrame{
public static JFrame frame = new JFrame();
And as @Salah said, JFrame is not static, so it should be:
public JFrame frame = new JFrame();
To solve your problem, you're disposing a new JFrame (yes, you have 3 JFrames in one class, instead of 1, which is what you want), with: JFrame.dispose();
if you already created an object or you're extending JFrame, you can:
this.dispose(); //For the extended JFrame
or
frame.dispose(); //For the object you created
add a comment |
Try setting the jFrame to invisible before disposing it:
public void disposeJFrame(JFrame frame){
frame.setVisible(false);
frame.dispose();
}
add a comment |
I had the same problem:
this.dispose();
solved my problem.
add a comment |
If you're wanting to close the whole program, you can use System.exit(0);
add a comment |
Instead JFrame.dispose();
, use frame.dispose()
or JFrame.this.dispose();
add a comment |
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f22945712%2fcant-dispose-of-jframe-window%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
6 Answers
6
active
oldest
votes
6 Answers
6
active
oldest
votes
active
oldest
votes
active
oldest
votes
dispose()
method is not a static, so it'll not work by calling it directly from JFrame
class
JFrame.dispose();
try to do :
dispose();
Or to dispose the frame
object you have created
frame.dispose();
Read more about JFrame
add a comment |
dispose()
method is not a static, so it'll not work by calling it directly from JFrame
class
JFrame.dispose();
try to do :
dispose();
Or to dispose the frame
object you have created
frame.dispose();
Read more about JFrame
add a comment |
dispose()
method is not a static, so it'll not work by calling it directly from JFrame
class
JFrame.dispose();
try to do :
dispose();
Or to dispose the frame
object you have created
frame.dispose();
Read more about JFrame
dispose()
method is not a static, so it'll not work by calling it directly from JFrame
class
JFrame.dispose();
try to do :
dispose();
Or to dispose the frame
object you have created
frame.dispose();
Read more about JFrame
edited Apr 8 '14 at 18:57
answered Apr 8 '14 at 18:51
SalahSalah
7,24921534
7,24921534
add a comment |
add a comment |
First of all you're extending JFrame and creating an object of JFrame, if I'm not wrong, this shouldn't be done.
public class Game extends JFrame{
public static JFrame frame = new JFrame();
And as @Salah said, JFrame is not static, so it should be:
public JFrame frame = new JFrame();
To solve your problem, you're disposing a new JFrame (yes, you have 3 JFrames in one class, instead of 1, which is what you want), with: JFrame.dispose();
if you already created an object or you're extending JFrame, you can:
this.dispose(); //For the extended JFrame
or
frame.dispose(); //For the object you created
add a comment |
First of all you're extending JFrame and creating an object of JFrame, if I'm not wrong, this shouldn't be done.
public class Game extends JFrame{
public static JFrame frame = new JFrame();
And as @Salah said, JFrame is not static, so it should be:
public JFrame frame = new JFrame();
To solve your problem, you're disposing a new JFrame (yes, you have 3 JFrames in one class, instead of 1, which is what you want), with: JFrame.dispose();
if you already created an object or you're extending JFrame, you can:
this.dispose(); //For the extended JFrame
or
frame.dispose(); //For the object you created
add a comment |
First of all you're extending JFrame and creating an object of JFrame, if I'm not wrong, this shouldn't be done.
public class Game extends JFrame{
public static JFrame frame = new JFrame();
And as @Salah said, JFrame is not static, so it should be:
public JFrame frame = new JFrame();
To solve your problem, you're disposing a new JFrame (yes, you have 3 JFrames in one class, instead of 1, which is what you want), with: JFrame.dispose();
if you already created an object or you're extending JFrame, you can:
this.dispose(); //For the extended JFrame
or
frame.dispose(); //For the object you created
First of all you're extending JFrame and creating an object of JFrame, if I'm not wrong, this shouldn't be done.
public class Game extends JFrame{
public static JFrame frame = new JFrame();
And as @Salah said, JFrame is not static, so it should be:
public JFrame frame = new JFrame();
To solve your problem, you're disposing a new JFrame (yes, you have 3 JFrames in one class, instead of 1, which is what you want), with: JFrame.dispose();
if you already created an object or you're extending JFrame, you can:
this.dispose(); //For the extended JFrame
or
frame.dispose(); //For the object you created
edited May 23 '17 at 12:10
Community♦
11
11
answered Apr 8 '14 at 18:58
FrakcoolFrakcool
7,72252557
7,72252557
add a comment |
add a comment |
Try setting the jFrame to invisible before disposing it:
public void disposeJFrame(JFrame frame){
frame.setVisible(false);
frame.dispose();
}
add a comment |
Try setting the jFrame to invisible before disposing it:
public void disposeJFrame(JFrame frame){
frame.setVisible(false);
frame.dispose();
}
add a comment |
Try setting the jFrame to invisible before disposing it:
public void disposeJFrame(JFrame frame){
frame.setVisible(false);
frame.dispose();
}
Try setting the jFrame to invisible before disposing it:
public void disposeJFrame(JFrame frame){
frame.setVisible(false);
frame.dispose();
}
answered Apr 8 '14 at 18:54
Roach TenantRoach Tenant
1
1
add a comment |
add a comment |
I had the same problem:
this.dispose();
solved my problem.
add a comment |
I had the same problem:
this.dispose();
solved my problem.
add a comment |
I had the same problem:
this.dispose();
solved my problem.
I had the same problem:
this.dispose();
solved my problem.
answered Sep 30 '18 at 8:05
besartmbesartm
8116
8116
add a comment |
add a comment |
If you're wanting to close the whole program, you can use System.exit(0);
add a comment |
If you're wanting to close the whole program, you can use System.exit(0);
add a comment |
If you're wanting to close the whole program, you can use System.exit(0);
If you're wanting to close the whole program, you can use System.exit(0);
answered Apr 8 '14 at 18:52
waco001waco001
391110
391110
add a comment |
add a comment |
Instead JFrame.dispose();
, use frame.dispose()
or JFrame.this.dispose();
add a comment |
Instead JFrame.dispose();
, use frame.dispose()
or JFrame.this.dispose();
add a comment |
Instead JFrame.dispose();
, use frame.dispose()
or JFrame.this.dispose();
Instead JFrame.dispose();
, use frame.dispose()
or JFrame.this.dispose();
answered Apr 8 '14 at 18:56
eliaselias
9,16033056
9,16033056
add a comment |
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f22945712%2fcant-dispose-of-jframe-window%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown