JFrame stuck, i can't move across sreen - java

I made a frame with some labels and a button on it, created an executable ear and now when i start it i can't move the application on my screen. Sometimes it works, sometimes it doesn't. I just can't drag the thing..
JFrame frame = new JFrame("Name");
frame.setPreferredSize(new Dimension(260, 160));
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JButton exitButton = new JButton("Exit");
exitButton.addActionListener(new ActionListener() {
#Override
public void actionPerformed(ActionEvent arg0) {
System.exit(0);
}
});
frame.add(exitButton, BorderLayout.SOUTH);
frame.add(vertBox, BorderLayout.CENTER);
frame.setVisible(true);
frame.pack();
frame.setLocationRelativeTo(null);
Any ideas what's wrong? It's really annoying that i can't move it.
P.S. There is a code part that i skipped, where i made the labels and added them

Perhaps (in the code you didn't show) you were disabling your frame through
frame.setEnabled(false);
PS: I know it has been 2 years since you asked the question, but it might help someone else

Related

How to let the user stop a java application

I have an application which mainly uses a bot to press keys when a condition is met. I am used to working with applets so I am wondering about how the user stops the application. I did it by using the task manager but that is not very user friendly, is it?
So how can I make I UI for a standalone java application? Or is there an other way to give the user the opportunity to stop the application?
You need to add a button to the GUI, set an action listener to it and in the addActionListener call the System.exit() method..
Example:
JButton showDialogButton = new JButton("Exit the app");
showDialogButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
System.exit(0);
}
});
Do you have a JFrame?
If not Create a new JFrame, JPanel, and JButton with:
JFrame frame = new JFrame("title");
JPanel panel = new JPanel();
JButton button = new JButton("Close");
//You need this for the screen to show.
frame.setSize(width,height);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setResizable(false);
frame.setLocationRelativeTo(null);
//add the panel to the frame and the button to the panel
frame.add(panel);
panel.add(button);
button.addActionListener(this);//the class needs to implements ActionListener
frame.setVisible(true);
//this is the actionperformed method which will run if the button is clicked.
public void actionPerformed(ActionEvent e) {
if(e.getSource==button){//If you have more than one button.
System.exit(0);
}
}

The JButton is not responding, why?

I'm trying to make button show a message when pressed, it's not working. Can anyone tell me what I missed?
In the end I have the KeyListener and the if for JOptionPane, but the website is not letting me post it (I'm new to this).
Anyway, it would be really nice if someone could tell me what I'm doing wrong, thanks.
public javalearning(){
FlowLayout f = new FlowLayout();
setLayout(f);
this.setSize(200,200);
JFrame j = new JFrame();
this.setTitle("this is a tittle");
JButton button = new JButton();
button.setText("Button");
this.add(button);
JButton button2 = new JButton();
button2.setText("Button2");
this.add(button2);
this.setVisible(true);
}
please follow example in this code and you will be fine. If at the end of the day you are unable to resolve it. you can write back. i believe this will help you.
import javax.swing.*;
import java.awt.event.*;
public class ChangeButtonLabel{
JButton button;
public static void main(String[] args){
ChangeButtonLabel cl = new ChangeButtonLabel();
}
public ChangeButtonLabel(){
JFrame frame = new JFrame("This is a Frame");
button = new JButton("Button");
button.addActionListener(new MyAction());
frame.add(button);
frame.setSize(400, 400);
frame.setVisible(true);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
public class MyAction implements ActionListener{
public void actionPerformed(ActionEvent e){
String text = (String)e.getActionCommand();
if (text.equals("Button2")){
button.setText("I am Sectona");
}
else{
button.setText("Click Me");
}
}
}
}
You state:
In the end I have the KeyListener and the if for JOptionPane,
As the tutorial that I've linked to in my comment will explain, you don't use KeyListeners with JButtons but rather ActionListeners.
e.g.,
myButton.addActionListener(new ActionListener() {
#Override
public void actionPerformed(ActionEvent evt) {
System.out.println("Button pressed");
}
});
You state:
but the website is not letting me post it (I'm new to this).
This site will let you post any reasonable amount of code. If you're having problems posting it, tell us the specifics of what's wrong, and maybe we can help you. Again, if you're trying to post code as an image, don't. It should be text that is formatted as code, not an image. But most important, don't keep us in the dark, or we can't help you.
like Hovercraft said, you will need to set ImagIcon(String image_name)
The code below will help you in embedding image on JButton. Give me a shout if you still find it difficult to integrate
import javax.swing.*;
import java.awt.*;
public class IconButton{
public static void main(String[] args){
JFrame frame = new JFrame("Icon on button");
JButton button = new JButton("Image button fro Sectona");
Icon imgicon = new ImageIcon("sectona.gif");
JPanel panel = new JPanel();
button.setIcon(imgicon);
panel.add(button);
frame.add(panel, BorderLayout.NORTH);
frame.setSize(400, 400);
frame.setVisible(true);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
}

Closing a JFrame using a button in eclipse

I'm a rookie when it comes to programming. We have this project about a log-in profile account. I just started doing it; and I came across a specific problem. I want to close a frame using a button.
buttonenter.setText("Enter");
buttonenter.addActionListener(new ActionListener (){
public void actionPerformed (ActionEvent ae){
}
});
I tried placing my frame.dispose();, set.Visible(false) etc. but i just got an error. I don't quite get. I really appreciate the help! Thank you!
Here's a simple example of what you're trying to do. What errors are you receiving?
private void initialize() {
frame = new JFrame();
frame.setBounds(100, 100, 450, 300);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JButton btnClose = new JButton("CLOSE");
btnClose.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
frame.setVisible(false);
}
});
frame.getContentPane().add(btnClose, BorderLayout.NORTH);
}
from JButtons ActionListener you can to call
JFrame#dispose(); (terminating current JVM)
JFrame#setVisible(false); (hide JFrame)
System.exit(0); (terminating current JVM)
and/or with (another standard ways are)
setDefaultCloseOperation();
add WindowListener and override proper event windowClosing();
when you close any objects by false visible, actually you just hide that object whereas the object stays in the memory yet.
you better use the
frame.dispatchEvent(new windowEvent(frame,windowEvent.window_closeing)); method

Closing a swing application once code is complete

This is no doubt a simple question for expert 'swingers' but after some research I've not come up with anything.
I have code in this form:
public static void main(Strings[] args) {
final JFrame frame = new JFrame("Symbol Formatter");
final JButton button = new JButton("Begin");
final JFileChooser filechoose = new JFileChooser();
button.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent ae) {
filechoose.showOpenDialog(frame);
filename = filechoose.getSelectedFile();
doSomething(filename);
button.setEnabled(false);
frame.setVisible(false); //edit for Andrew Thompson
}
});
JPanel panel = new JPanel();
panel.add(button);
frame.add(panel);
frame.pack();
frame.setSize(250, 75);
frame.setLocationByPlatform(true);
frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
frame.setVisible(true);
}
I want to close the swing application once doSomething(filename) is completed. At the moment, the original frame remains where it is and the application only terminates when the frame is closed manually. How can I close once doSomething(filename) has run once.
I'm sure this is a simple question and just requires changing setDefaultCloseOperation however, the Jframe documentation hasn't helped me much.
Also, is there a way to add a title to the showOpenDialog? So that is has something like "Choose library file" as the title
You could use:
frame.dispose()
Call..
frame.setVisible(false);
I believe if you change the default close operation to
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
Calling dispose should terminate the application & exit the JVM.
If that fails, as has been mentioned, call System.exit(0) will certainly terminate te JVM
For the file chooser dialog title, see the javadoc.

Why doesnt my JButton always pop up on a GUI?

So im making a game, and i want a jbutton to pop up in the window and when you click it you can log in. the problem is that when i start the game, it doesnt always pop up, which is kind of annoying. the only think would be the problem is the order im doing things. here is the code:
public static void createWindow() {
ImagePanel panel = new ImagePanel(
new ImageIcon(backgroundFile).getImage()); //used for the background
JButton login = new JButton(new AbstractAction("Login") {
public void actionPerformed(ActionEvent e) {
Login.createWindow();
}
});
login.setBounds(300, 300, 100, 100);
frame.getContentPane().add(panel); //sets the background to a pic
frame.setJMenuBar(MenuBar.menuBarCreator()); creates the menu bar
frame.pack();
frame.setResizable(false);
frame.setTitle("*Game Title* Beta 0.0.1 ADMINISTRATOR VERSION");
frame.setSize(ImagePanel.img.getWidth(null),
ImagePanel.img.getHeight(null));
frame.setLocation(Monitor.setLocationHeight(),
Monitor.setLocationWidth());
frame.setVisible(true);
frame.setDefaultCloseOperation(EXIT_ON_CLOSE);
StreamingLineSound.start(soundFile); //starts a music file
frame.add(login);
}
any help would be fantastic. so basically all i want is an idea why it doesnt pop up all the time. thanks
Code line frame.setVisible(true); must be last line in the public static void createWindow() {, because you display JFrame and thenafter add frame.add(login);

Categories