Why doesnt my JButton always pop up on a GUI? - java

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);

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);
}
}

Java Multiple Screens create/use

I have not yet created a GUI in Java, thus I'm wondering how I can create and use multiple Windows. I want to work with the following Windows:
Splash Screen
Login Window (Create and Load Game)
Main Window
The Main Window contains three buttons:
If Button1 is clicked, then show Window1.
If Button2 is clicked, then show Window2.
If Button3 is clicked, then show Window3.
How can I do this? Thanks for your help.
Just to get you started, here is an example:
private void run() {
JFrame main = createMain();
populateMain(main.getContentPane());
main.setVisible(true);
}
private JFrame createMain() {
JFrame result = new JFrame();
result.setTitle("Main");
result.setSize(400, 300);
result.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
return result;
}
private void populateMain(Container contentPane) {
contentPane.setLayout(new GridLayout(1, 3));
contentPane.add(button1());
contentPane.add(button2());
contentPane.add(button3());
}
private JButton button1() {
JButton result = new JButton("button1");
result.addActionListener(e -> displayWindow1());
return result;
}
private void displayWindow1() {
JFrame frame = new JFrame();
frame.setTitle("Window 1");
frame.setSize(400, 300);
frame.setVisible(true);
}
private JButton button2() {
JButton result = new JButton("button2");
result.addActionListener(e -> displayWindow2());
return result;
}
private void displayWindow2() {
JFrame frame = new JFrame();
frame.setTitle("Window 2");
frame.setSize(400, 300);
frame.setVisible(true);
}
private JButton button3() {
JButton result = new JButton("button3");
result.addActionListener(e -> displayWindow3());
return result;
}
private void displayWindow3() {
JFrame frame = new JFrame();
frame.setTitle("Window 3");
frame.setSize(400, 300);
frame.setVisible(true);
}
When you execute the run() method, this will display the main window. The main window contains three buttons, added by the method populateMain(). When you click any of the three buttons, a new window is created and displayed. You can also click each button multiple times. When you close the main window, the application is terminated. This is because of the following line:
result.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
I hope this helps you to get started. However this is just a simple example using Java's GUI framework called Swing. If you want to create a game you might want to use another GUI framework.

Java Swing blank screen instead of window displayed

I'm following through a book called "The JFC Swing Tutorial (Second Edition)" and I'm pretty much at the start I have followed this code and it should be displaying the button and the label in the content pane, but All im getting is a blank screen. any ideas?
Thanks.
import java.awt.GridLayout;
import javax.swing.*;
public class m extends JFrame
{
void UserFrame()
{
//JFrame.setDefaultLookAndFeelDecorated(true);
JFrame frame = new JFrame("Hellow You");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JPanel jp = new JPanel(new GridLayout(0,1));
//makes label
JLabel label = new JLabel("Sup ");
//adds to the frames content pane a label
frame.getContentPane().add(label);
JButton button = new JButton("Hai");
frame.getContentPane().add(button);
jp.add(button);
jp.add(label);
jp.setBorder(BorderFactory.createEmptyBorder(30,30,10,30));
//pack set the window to what it needs AKA to display all components
frame.pack();
//frame.setSize(250, 250);
//shows window
frame.setVisible(true);
}
public static void main(String[] args)
{
final m window = new m();
SwingUtilities.invokeLater(new Runnable()
{
public void run()
{
window.UserFrame();
}
});
}
}
Simply add
frame.add(jp);
just before
frame.pack();
What's happening here? You correctly add all your widgets to a JPane, but you basically threw that JPane away and didn't use it anywhere.
This will be sufficient just to get it to work properly.
If you want to do it correctly, you should also remove frame.getContentPane().add(label); and frame.getContentPane().add(button); (Thank you #dic19 for noting that!). These will not work the way you used it.

JFrame stuck, i can't move across sreen

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

JWindow does not receive events in Java 7 for Mac

See the sample code below. It simply creates a button and adds it to a window. But when *menu_item3* is selected, the ActionListener doesn't receive the event. This error only occurs on Java 7 for Mac. If I run this same code in Windows, it works fine. When I run this same code on Java 6 for Mac, it works fine. If I use a JFrame instead of JWindow, it works fine. I do not want to use a JFrame because I do not want to display the window title bar and border.
Any ideas?
public class SandBox {
public static JFrame frame = new JFrame();
public static JPopupMenu menu = new JPopupMenu();
public static JLabel button = new JLabel();
public static void main(String[] args) {
JFrame window = new JFrame();
JPanel panel = new JPanel();
JMenuItem menu_item1 = new JMenuItem("Item1");
JMenuItem menu_item2 = new JMenuItem("Item2");
JMenuItem menu_item3 = new JMenuItem("Item3");
menu.add(menu_item1);
menu.add(menu_item2);
menu.add(menu_item3);
menu.setEnabled(true);
button.setText("Button");
button.setBorder(BorderFactory.createMatteBorder(4, 4, 4, 4, new Color(255,0,0)));
button.setSize(100, 24);
button.add(menu);
button.setVisible(true);
button.setEnabled(true);
panel.add(button);
panel.setVisible(true);
window.add(panel);
window.setVisible(true);
window.setLocation(100, 100);
window.setAlwaysOnTop(true);
window.setFocusable(true);
window.setFocusableWindowState(true);
window.pack();
frame.setVisible(false);
button.addMouseListener(new MouseAdapter() {
public void mouseClicked(MouseEvent e) {
menu.show(button, 0, 0);
}
});
menu_item3.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
JOptionPane.showMessageDialog(frame, "Eggs are not supposed to be green.");
}
});
}
}
I have submitted a bug with Oracle. Still waiting for their response whether the bug will be officially filed. I will update this answer when I do hear something.
In the mean time, I did find a viable workaround. I use a JFrame instead of a JWindow. I was unaware you can remove the window title and borders of a JFrame using the method setUndecorated(). Also be aware that this method can only be called while the frame isn't displayable.

Categories