I wrote a game with a lot of classes,and it works fine.But it is too exhausting to change the values of the variables from source code every time I want to test something.So I researched how can I enter the values of the variable without using window.The behaviour I want is that when I run the program a panel will be openned and it will ask me the values of variables.I tried to use JInternal frame,eclipse creates a default JInternal Frame for you.
import java.awt.EventQueue;
import javax.swing.JInternalFrame;
public class Test extends JInternalFrame {
/**
* Launch the application.
*/
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
BoardConstructor frame = new BoardConstructor();
frame.setTitle("Monopoly");
frame.setSize(1500,750);
frame.setVisible(true);
frame.setDefaultCloseOperation(EXIT_ON_CLOSE);
frame.getContentPane().add(new MyShape());
Test frame1 = new Test();
frame1.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
public Test() {
setBounds(100, 100, 450, 300);
}}
I wrote my main function into this which simply creates my game.Now what I think should work is that this class should access the variables in other classes and change them but I have no idea how it works in reality.How can I accomplish that?
If you try live debugging in Eclipse than if you have a pause feature, you can easily change the variable values in the code. If that's not what you're looking for let me know
Related
Before anyone asks, I have tried using setAlwaysOnTop(false). Here is a repeatable example.
import javax.swing.JFrame;
public class SOQ_20200913
{
public SOQ_20200913()
{
JFrame frame = new JFrame("SOQ_20200913");
//for simplicity's sake, you could also comment these 2 lines - they don't seem to help or hurt the situation
frame.setLocation(200, 200);
frame.setSize(300, 300);
frame.setAlwaysOnTop(false);
frame.setVisible(true);
}
public static void main(String[] args)
{
SOQ_20200913 stackOverflowQuestion = new SOQ_20200913();
}
}
After I run, I click away and try to click on my code, and then my web browser, but the JFrame always remains on top.
Am I missing something? Is there some other field I should be setting here?
I am asking this question due to mKorbel's second comment on this question. I have been using this keyword to call local variables, local components of that class (example Buttons) and methods e.t.c. I am not sure what is wrong with using the keyword the way I have been doing it.
don't to use this.whatever in Swing, Java, (in MCV make me some sence), use local variables insted of.
Question: What is wrong with using this.XXX in Swing?
Sample Code
public class SwingTest extends JFrame {
// variables
private String st = "You clicked me";
SwingTest() {
this.initUI();
}
private void initUI() {
this.setTitle("Swing Test");
this.setSize(200, 200);
this.setLayout(new BorderLayout());
this.setVisible(true);
this.clickMe = new JButton("Click"); // What is wrong with using this
// ref here?
this.add(clickMe);
this.clickMe.addActionListener((ActionEvent) -> {
JOptionPane.showMessageDialog(this, this.st);// What is wrong with
// using this ref
// here?
});
}
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
#Override
public void run() {
SwingTest swingTest = new SwingTest();
}
});
}
// components
JButton clickMe;
}
The button clickMe does not need to be a field. It can be a local variable JButton clickMe inside the dialog defining method/constructor. As normally many GUI components are created and added to the window, it cleans up the class, keeps declarations close to usage, and leaves the life-time as short as possible (on changing window contents).
(The talk about this is merely to point out the fieldness of clickMe.)
Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 8 years ago.
Improve this question
In my program when a button is clicked the JFrame I'm using should close. However the dispose() method doesn't seems to work. Could anyone help me?
public class SetUp extends JFrame implements ActionListener, Checker {
JFrame frame= new JFrame("Set Up");
public mmSetUp() {
setSize(500, 300);
setLayout(new BorderLayout());
setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
setResizable(false);
setLocationRelativeTo(null);
setVisible(true);
pack();
}
public void actionPerformed(ActionEvent e) {
if (e.getSource() == Submit) {
windowclose();
}
}
public void windowclose() {
frame.dispose();
}
}
Well after checking your code it's probably because of your class extends JFrame and you're adding all your elements to this extended frame.
But you also create a JFrame, this isn't a good practice, 1st of all see The use of multiple JFrames, Good / Bad Practice about using multiple frames.
Now going back into your code, let's see about it, as I said before, you're extending a JFrame on your class like this (I'll refer to this frame as frame1)
public class SetUp extends JFrame implements ActionListener, Checker {
But you also create a JFrame here (I'll refer to this frame frame2):
JFrame frame= new JFrame("Set Up");
You are trying to dispose the frame2, it's actually happening that, but if you want to dispose frame1 (which is the one which has all your elements, i.e. the one you're working with), then on this method, you should change:
public void windowclose() {
frame.dispose();
}
for this one:
public void windowclose() {
this.dispose();
}
Also I don't recommend you to extend from JFrame class it's better to create objects from it like you did.
But if you do so, then you should change your code as follows:
public mmSetUp() {
frame.setSize(500, 300);
frame.setLayout(new BorderLayout());
frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
frame.setResizable(false);
frame.setLocationRelativeTo(null);
frame.setVisible(true);
frame.pack();
}
public void actionPerformed(ActionEvent e) {
if (e.getSource() == Submit) {
windowclose();
}
}
public void windowclose() {
frame.dispose();
}
I'm not 100% sure this works or compiles, since I'm not at my PC atm, so I can't create a full example, but at 1st view that's what is happening on your class. In 3 more hours or so, I can post a compilable example.
But please check it
You have 2 JFrames.
One, that is being extended.
Two, the one called frame.
First of all, you have only made one mistake.
Change windowclose() to:
public void windowclose() {
dispose(); // Removed frame part
}
What you have done is, completely set up the extended JFrame from the start and left frame alone so its idle.
When windowclose() is called, it disposes the idle frame which essentially is useless as you have not even modified anything except for the title.
After you have changed windowclose() the code should work, now you can get rid of
JFrame frame= new JFrame("Set Up");
But if you want to keep everything neater, do as Frakcool said and remove extends JFrame
I think you want to call frame.setVisible( false ) instead.
Edit: your program has several logic errors. Try this version instead:
public class WindowCloseTest
{
public static void main( String[] args )
{
new SetUp().mmSetUp();
}
}
class SetUp implements ActionListener {
JFrame frame= new JFrame("Set Up");
public void mmSetUp() {
frame.setSize(500, 300);
JButton b = new JButton( "Submit");
b.addActionListener(this);
frame.add( b, BorderLayout.SOUTH );
frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
frame.setLocationRelativeTo(null);
frame.pack();
frame.setVisible(true);
}
public void actionPerformed(ActionEvent e) {
System.out.println( e );
if (e.getActionCommand() == "Submit") {
windowclose();
}
}
public void windowclose() {
frame.dispose();
}
}
I agree with #user2338547, there are some code you didn't show us that might cause the problem, I code my version of the use for dispose method, hope it can give you some idea of how to use the method
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JFrame;
import javax.swing.JButton;
import javax.swing.JPanel;
public class GuiTest extends JFrame{
static GuiTest frame=new GuiTest("test");
private JPanel panel=new JPanel();
private JButton button=new JButton("close");
private Button buttonListener=new Button();
public GuiTest(String title){
super(title);
add(panel);
panel.add(button);
button.addActionListener(buttonListener);
}
public static void main(String[] args){
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(100,100);
frame.setVisible(true);
}
class Button implements ActionListener{
public void actionPerformed(ActionEvent e){
frame.dispose();
}
}
}
I am developing a tool for my laptop. I want to disable minimize button in the JFrame. I have already disabled maximize and close button.
Here is the code to disable maximize and close button:
JFrame frame = new JFrame();
frame.setResizable(false); //Disable the Resize Button
// Disable the Close button
frame.setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);
Please, tell me how to disable minimize button.
Generally, you can't, what you can do is use a JDialog instead of JFrame
As #MadProgrammer said (+1 to him), this is definitely not a good idea you'd rather want to
use a JDialog and call setDefaultCloseOperation(JDialog.DO_NOTHING_ON_CLOSE); to make sure it cannot be closed.
You could also use a JWindow (+1 to #M. M.) or call setUndecorated(true); on your JFrame instance.
Alternatively you may want to add your own WindowAdapater to make the JFrame un-minimizable etc by overriding windowIconified(..) and calling setState(JFrame.NORMAL); from within the method:
//necessary imports
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
public class Test {
/**
* Default constructor for Test.class
*/
public Test() {
initComponents();
}
public static void main(String[] args) {
/**
* Create GUI and components on Event-Dispatch-Thread
*/
javax.swing.SwingUtilities.invokeLater(new Runnable() {
#Override
public void run() {
Test test = new Test();
}
});
}
private final JFrame frame = new JFrame();
/**
* Initialize GUI and components (including ActionListeners etc)
*/
private void initComponents() {
frame.setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);
frame.setResizable(false);
frame.addWindowListener(getWindowAdapter());
//pack frame (size JFrame to match preferred sizes of added components and set visible
frame.pack();
frame.setVisible(true);
}
private WindowAdapter getWindowAdapter() {
return new WindowAdapter() {
#Override
public void windowClosing(WindowEvent we) {//overrode to show message
super.windowClosing(we);
JOptionPane.showMessageDialog(frame, "Cant Exit");
}
#Override
public void windowIconified(WindowEvent we) {
frame.setState(JFrame.NORMAL);
JOptionPane.showMessageDialog(frame, "Cant Minimize");
}
};
}
}
If you don't want to allow any user action use JWindow.
You may try to change your JFrame type to UTILITY. Then you will not see both minimize btn and maximize btn in your program.
I would recommend you to use jframe.setUndecorated(true) as you are not using any of the window events and do not want the application to be resized. Use the MotionPanel that I've made, if you would like to move the panel.
I'm developing Java Desktop Application in NetBeans IDE. I want to show the login screen in JFrame with small size. Then after getting login i want to extend JFrame to full screen with other panels.The problem is its showing once properly and from the next time it used to show the login screen with full screen.How can i avoid it? I'm placing different panels on the same frame.
While login
this.getFrame().setExtendedState(Frame.NORMAL);
this.getFrame().setSize(360, 233);
this.getFrame().setResizable(false);
After login
this.getFrame().setExtendedState(Frame.MAXIMIZED_BOTH);
I want to show the login screen in JFrame with small size. Then after getting login I want to extend JFrame to full screen with other panels.
Show the frame at the full size, and make it the owner of a modal JDialog or JOptionPane that shows the login details. If the login fails and the user chooses to cancel instead of try again, exit the app.
If I design a new JDialog for login then how can I show it initially?
JFrame f = this.getFrame();
JDialog loginDialog = new JDialog(f,"Login",true);
loginDialog.add(loginPanel);
loginDialog.pack();
f.setExtendedState(Frame.MAXIMIZED_BOTH)
f.setVisible(true);
loginDialog.setLocationRelativeTo(f);
loginDialog.setVisible(true);
Edit after chat;
According to scenario; External desktop application hold, remember and
set frames size's to last settings. So inner panel must get external
main frame from desktop application and set size and location settings
after application runs after internal code runs.
There is no more things I can do about codes without having whole project :)
Previous answers;
For an alternative, you may use JDialog to login
else next time when you show login screen, reverse what you do when setting fullscreen.
Some code samples helps us to answer your question better.
Edit 2:
he next time before login screen did you use;
this.getFrame().setExtendedState(Frame.NORMAL);
Edit 3: Code Sample
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import javax.swing.JFrame;
public class MyFrame extends JFrame implements MouseListener {
/**
* #param args
*/
public static void main(String[] args) {
MyFrame frame = new MyFrame();
frame.setVisible(true);
frame.setSize(200, 200);
frame.setExtendedState(JFrame.MAXIMIZED_BOTH);
frame.addMouseListener(frame);
}
#Override
public void mouseClicked(MouseEvent e) {
if(this.getExtendedState() == JFrame.MAXIMIZED_BOTH){
this.setExtendedState(JFrame.NORMAL);
}
else{
this.setExtendedState(JFrame.MAXIMIZED_BOTH);
}
}
#Override
public void mousePressed(MouseEvent e) {
}
#Override
public void mouseReleased(MouseEvent e) {
}
#Override
public void mouseEntered(MouseEvent e) {
// TODO Auto-generated method stub
}
#Override
public void mouseExited(MouseEvent e) {
// TODO Auto-generated method stub
}
}
Put in your constructor, after the initComponent() function a simple piece's code
initComponents();/*Function automated*/
setMinimumSize(new Dimension(700,400).getSize());
setExtendedState(MAXIMIZED_BOTH);/*To see your application starts maximized!*/