I was just writing an application and i was not able to set location and size of button to desired values. whenever i open code the button comes at same location and with same size
Here is my code
public class main_class {
public static void main(String args[]){
Main_page mp = new Main_page();
mp.start();
}
}
Second file is:
import javax.swing.*;
import java.awt.*;
public class Main_page extends JPanel {
private static final long serialVersionUID = 1L;
public void start(){
JPanel panel1 = new JPanel();
panel1.setBackground(Color.pink);
JButton Button1 = new JButton("Programmer");
Button1.setSize(10, 100);
Button1.setLocation(200,500);
panel1.add(Button1);
JFrame frame1 = new JFrame("Main Window");
frame1.setSize(700,500);
frame1.setContentPane(panel1);
frame1.setResizable(false);
frame1.setVisible(true);
}
}
what is the problem.
What is the problem?
I'm so glad you asked that question. It's not just a matter of getting a Swing application to work. You must get the Swing application to work correctly so that you can add more functionality to your Swing application without your Swing application breaking.
I added a call to SwingUtilities invokeLater in the main method. This call ensures that the Swing components are defined and used on the Event Dispatch thread (EDT).
I changed the name of your class to MainPage to conform to Java standards for naming classes.
I implemented Runnable to make the invokeLater method parameter easier to define.
I removed the extends of JPanel. You use Swing components. The only reason to extend a Swing component is when you want to override one of the component methods. The only reason you extend any Java class is when you want to override one of the class methods.
I changed the name of your MainPage method to run.
I set a layout (FlowLayout) for your JPanel. You must always use a layout manager for your Swing components.
I made button1 lowercase. Java field names are lowercase, so you can easily differentiate them from class names.
I added a call to the JFrame setDefaultCloseOperation to your run method. Without this call, your Swing application will not stop executing when you close the window. After a while, you'll have dozens of copies of your Swing application running, with no easy way to stop them.
I added a call to the JFrame pack to let the JFrame expand or contract to fit the components, rather than be a fixed size.
Here's the revised code. I put the main method in the MainPage class to make it easier to paste the code.
package com.ggl.testing;
import java.awt.Color;
import java.awt.FlowLayout;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;
public class MainPage implements Runnable {
#Override
public void run() {
JPanel panel1 = new JPanel();
panel1.setBackground(Color.pink);
panel1.setLayout(new FlowLayout());
JButton button1 = new JButton("Programmer");
panel1.add(button1);
JFrame frame1 = new JFrame("Main Window");
frame1.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame1.add(panel1);
frame1.pack();
frame1.setVisible(true);
}
public static void main(String[] args) {
SwingUtilities.invokeLater(new MainPage());
}
}
if you want to set the size and location manually then you can use panel1.setLayout(null); as people have said in the comments this isn't recommended.
Related
I am using swing in Java and I need to create a JButton and put it in a JPanel.
I reed tutorials and I did this:
public void crearNuevaMiga(String nombre)
{
JButton nuevo = new JButton(nombre);
this.MigasDePan.add(nuevo);
nuevo.setVisible(true);
nuevo.setLocation(new Point(migaX, migaY));
System.out.println(nuevo.getLocation().x + " "+ nuevo.getLocation().y);
migaX = migaX-avanceMigas;
}
I do that and when I call the function, I cant see the button. I put a button with the designer of NetBeans and get X and Y Location. Then, in the variables migaX and migaY I put that X and Y Location, so the button need to be in the same position, but it is not there.
Anyone knows why? Maybe putting the location in that way is not correct?
Thanks for your time!
EDIT: MigasDePan is my JPanel
Here's a simple example of putting a JButton in a JPanel, and putting the JPanel in a JFrame. I created this code without using any GUI builder.
I called the SwingUtilities invokeLater method in the main method to put the creation and use of the Swing components on the Event Dispatch thread. Oracle and I insist that you start every Swing application on the Event Dispatch thread.
I used a JFrame. You must call the JFrame methods in the order they are called in the run method.
I used a JPanel. I put the JButton in the middle of the JPanel, since it's the only component on the JPanel.
I used a Swing layout, the Border Layout. Different Swing layouts are used to create different Swing component layouts.
Here's the short, self-contained, runnable code.
package com.ggl.testing;
import java.awt.BorderLayout;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;
public class MyButton implements Runnable {
public static void main(String[] args) {
SwingUtilities.invokeLater(new MyButton());
}
#Override
public void run() {
JFrame frame = new JFrame("My Button");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.add(createMainPanel());
frame.pack();
frame.setVisible(true);
}
private JPanel createMainPanel() {
JPanel panel = new JPanel();
panel.setLayout(new BorderLayout());
JButton myButton = new JButton("My Button");
panel.add(myButton);
return panel;
}
}
By default components have a size of (0, 0) so there is nothing to paint.
When you dynamically add a button to a visible GUI you need to invoke the layout manager so the components size/location can be determined by the layout manager.
The basic code is:
panel.add(...);
panel.revalidate();
panel.repaint();
I couldn't find the answer anywhere else online, so I came here. I apologize in advance if the mistake in my code is very obvious; I'm still quite new to java swing. Here's what's going on: I have created a JButton named toggleElevators, and I want it to change text when clicked. I have already created an ActionListener and added it to toggleElevators. All I want right now is for the JButton to change text when clicked from Click me to Clicked.
First, here's a picture of what the JFrame looks like when executed:
NOTE: There is a third class, but it is purely for drawing the picture on the left. It has nothing to do with the GridLayout or the JButton.
Run class (created frame and adds toggleElevators JButton:
import java.awt.Color;
import java.awt.EventQueue;
import java.awt.GridLayout;
import javax.swing.JFrame;
public class Run extends Input{
Input i = new Input();
public static void main(String[] args) {
new Run();
}
public Run() {
EventQueue.invokeLater(new Runnable() {
#Override
public void run() {
JFrame frame = new JFrame("Elevators");
frame.setLayout(new GridLayout(0, 3));
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.add(new Elevators(Color.MAGENTA, true));
frame.add(new Elevators(Color.ORANGE, false));
frame.setSize(800,600);
frame.setResizable(false);
frame.getContentPane().add(toggleElevators); //adds toggleElevators button to JFrame
i.addButtonListeners(); //calls method defined in Input class, which adds the ActionListener to the toggleElevators button
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
});
}
}
Input class (creates toggleElevators JButton and its ActionListener):
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
public class Input {
JButton toggleElevators = new JButton("Click me.");
public void addButtonListeners() {
toggleElevators.addActionListener(new toggleElevatorsListener());
}
class toggleElevatorsListener implements ActionListener {
public void actionPerformed (ActionEvent event) {
toggleElevators.setText("Clicked.");
System.out.println("ActionListener called."); //I know the ActionListener is not being called because this line is not being printed out in the console
}
}
}
Your Run class extends Input, but also HAS an Input named i. You're adding this.toggleElevators to the frame, but you're adding a listener to i.toggleElevators.
Remove the i field from your class. I would also forget completely about defining and extending an Input class. It doesn't serve any purpose, and seems to confuse more than help you.
You create a new Input in your Run class, while the Run class also extends Input.
When you call i.addButtonListeners(); the action listeners are added on the toggleElevators from i and not on the toggleElevators you inherited from the Input class.
Try addButtonListeners().
Your Run class extends Input. Therefore it has its own toggleElevators which is the one it sets in the frame. However, i has is own toggleElevators where it sets the event listeners. So they are not set on the one in the frame but on one that never gets used.
You can simply delete the i object. As Run extends Input, it can call the method directly, and then the listener will be added to its own toggleElevators.
First of all, this is a more specific question than it seems to be. To start off: I am currently doing a small application with a rather small GUI, so I decided to make a GUI class, and initialize my whole GUI in this constructor.
This would look like this:
public class GUI extends JFrame{
public GUI{
//Initialize GUI here, including its Frames, Panels, Buttons etc.
}
}
How can I now access the GUIs frame etc. from an external class? If I would create an object of the GUI class, I would simply duplicate my GUI window. I did not come across any other ideas than making the frame, panel and so on static.
I'm somewhat lost right now. Also I'm pretty sure that I am not thinking the right way into this case, but I need someone to point me to the right direction. If someone could help me out, I would be very thankful.
First of all, using static is the worst solution possible, even if your GUI class is a singleton (buf if it is, at least it will work fine).
Why don't you simply create getters and/or setters ? And finally, it is usually not normal that external classes need to access the components of another graphic class. You should wonder if your design is the most fitted for your needs.
Here's a simple GUI to change the background color of a JPanel with a JButton. Generally, this is how you construct a Swing GUI.
package com.ggl.testing;
import java.awt.Color;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.BoxLayout;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;
public class ChangeDemo implements Runnable {
private boolean isYellow;
private JFrame frame;
public static void main(String[] args) {
SwingUtilities.invokeLater(new ChangeDemo());
}
#Override
public void run() {
frame = new JFrame("Change Background Demo");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JPanel mainPanel = new JPanel();
mainPanel.setLayout(new BoxLayout(mainPanel, BoxLayout.PAGE_AXIS));
JPanel namePanel = new JPanel();
JLabel nameLabel = new JLabel(
"Click the button to change the background color");
nameLabel.setAlignmentX(JLabel.CENTER_ALIGNMENT);
namePanel.add(nameLabel);
mainPanel.add(namePanel);
final JPanel buttonPanel = new JPanel();
buttonPanel.setLayout(new FlowLayout());
buttonPanel.setBackground(Color.YELLOW);
isYellow = true;
JButton changeButton = new JButton("Change Color");
changeButton.addActionListener(new ActionListener() {
#Override
public void actionPerformed(ActionEvent event) {
isYellow = !isYellow;
if (isYellow) buttonPanel.setBackground(Color.YELLOW);
else buttonPanel.setBackground(Color.RED);
}
});
buttonPanel.add(changeButton);
mainPanel.add(buttonPanel);
frame.add(mainPanel);
frame.pack();
frame.setLocationByPlatform(true);
frame.setVisible(true);
}
}
You don't access the Swing components of the GUI from other classes. You create other classes to hold the values of the GUI.
Generally, you use the model / view / controller pattern to construct a Swing GUI. That way, you can focus on one part of the GUI at a time.
Take a look at my article, Java Swing File Browser, to see how the MVC pattern works with a typical Swing GUI.
You don't need to make it static or to create a new JFrame object every time.
Have a look at this simple code :
class UseJFrame {
public static void main(String...args) {
Scanner sc = new Scanner(System.in);
JFrame frame = new GUI();
frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
System.out.println("Press E to exit");
String ip;
while(true) {
System.out.println("Show GUI (Y/N/E)? : ");
ip = sc.nextLine();
if(ip.equalsIgnoreCase("y") {
frame.setVisible(true);
} else if(ip.equalsIgnoreCase("n") {
frame.setVisible(false);
} else { // E or any other input
frame.dispose();
}
}
}
}
Note : Don't make GUI visible through constructor or it will show window at the very starting of creation of JFrame object.
If you want to use the same JFrame object at other places too then pool architecture would be better approach.
I would like some clarity on what is exactly happening here. Say I have these three methods and I continually hit the button again and again. Is this causing some kind of memory leak or chain of pointers that I am unaware of? My understand is that when a method ends any variables local to that method are cleaned up. This would include that "pointer" to the new JFrame then correct?
Again assume the user is clicking the button on each frame.
public class driver {
public static void main(String[] args) {
// TODO Auto-generated method stub
parentFrame pF = new parentFrame();
}
}
-
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
public class parentFrame extends JFrame {
private JFrame frame;
private JButton button;
public parentFrame() {
frame = new JFrame("Parent Frame");
frame.setSize(400, 400);
button = new JButton();
frame.add(button);
button.addActionListener(new buttonPress());
frame.setVisible(true);
frame.setDefaultCloseOperation(EXIT_ON_CLOSE);
}
public void createChild() {
#SuppressWarnings("unused")
childFrame cF = new childFrame(); //The default constructor will display the frame
frame.dispose(); //How?
}
class buttonPress implements ActionListener {
#Override
public void actionPerformed(ActionEvent arg0) {
createChild();
}
}
}
-
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
public class childFrame extends JFrame {
private JFrame frame;
private JButton button;
public childFrame() {
frame = new JFrame("Child Frame");
frame.setSize(400, 400);
button = new JButton();
frame.add(button);
button.addActionListener(new buttonPress());
frame.setVisible(true);
frame.setDefaultCloseOperation(EXIT_ON_CLOSE);
}
public void createParent() {
parentFrame pF = new parentFrame(); //The default constructor will display the frame
frame.dispose(); //How?
}
class buttonPress implements ActionListener {
#Override
public void actionPerformed(ActionEvent arg0) {
createParent();
}
}
}
I'm not sure what you mean by "I don't have to call super()", but if you want to avoid confusion, then either extend JFrame or use a variable in your class of type JFrame. You have both, and create the variable reference in the constructor, so that when JFrame frame = new parentFrame(); is executed, two JFrames are created: the one on which you have the new operator, and the one in the constructor of that object.
As for cleanup, when you execute setVisible(true); on a JFrame, it seems obvious to me that the JFrame reference gets put into the Swing system, and so the variable in your method (whether local or instance (or static)) is no longer the only reference. I think of dispose() as an instruction to the Swing framework that the code is done with this variable, clean up after it and remove all references to it. If you don't do that before you lose your own reference to the variable (e.g., if it were a local variable and you didn't do that before you exited the method), then you would lose your chance to call dispose. I suppose you could still get a reference from Swing somehow, and then call dispose on it.
You don't say whether you have evidence of a memory leak or or just trying to understand this code.
I have read your question, the answers and the comments. I advice you to review OOP basics to link your objects better. About the comment:
How can I create a new Frame in the parentFrame and then dispose of that frame but the child frame remains alive. Is control passed to the child frame?
Each JFrame is an independent instance of the JFrame object. You don't need to manually pass control. What you should do is define closing behaviour for each JFrame: A main frame on close will close the whole program (EXIT_ON_CLOSE), secondary frames can have other behaviour (HIDE_ON_CLOSE or DISPOSE_ON_CLOSE). You define this with:
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
I should advice you to go through the java tutorials and make sure you understand well about classes and instances before diving into swing. Start manually will help you understand better (I saw they told you this already but I agree on it). Keep on!
Sorry for the messed up title 'cause I don't really know what title I should use xD Anyways here's the problem. So I got this simple game that starts with a Jframe that contains a single button which is if pressed, changes the contents of it to the content of another class that contains the game itself. The problem is that the controls doesn't work right away. (controls, I mean when you press space, the character in the game must shoot but he doesn't because of this problem) It doesn't work right away I mean you need to switch to another window and then go back to your game window and the controls will work. Is there anything I can do with my game so that I won't need to switch windows for the controls to work?
Here's the code of the Jframe:
package rtype;
import javax.swing.JButton;
import javax.swing.JFrame;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.*;
public class Rtype extends JFrame implements ActionListener{
private static final long serialVersionUID = 1L;
public Rtype() {
setSize(1020, 440);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setLocationRelativeTo(null);
setTitle("PROJECT JAEGER");
setResizable(false);
JButton startButton = new JButton("START GAME");//The JButton name.
add(startButton);//Add the button to the JFrame.
startButton.addActionListener(this);//Reads the action.
setVisible(true);
}
public static void main(String[] args) {
new Rtype();
}
public void actionPerformed(ActionEvent i) {
getContentPane().removeAll();
add(new Board());
System.out.println("The Button Works!");//what the button says when clicked.
revalidate();
setVisible(true);
}
}
the Board in the add(new Board()); is the class of the game.
Since you've not provided the Board class, I would assume from your description that you are using one or more KeyListeners.
This is a common problem with KeyListener. The component it is registered to must not only be focusable, but it must have current focus.
Instead of using KeyListener, you should be using the Key Bindings API which has mechanisms overcomes these limitations