-1

I am working on a program for my school to use. It is like match.com, but for a public school. Yes there has been permission from the school to do this.

This is the first time I have ever used swing and I am having an issue with adding my JPanel to my JFrame so that I can see and use the button.

    private void Framing()
    {
    JPanel Panel = new JPanel(); 
    Panel.setLayout(new BorderLayout());
    JFrame Frame = new JFrame("Warning");
    Frame.setUndecorated(true);
    JButton OK = new JButton("EXIT");
    OK.addActionListener((ActionEvent event) -> {System.exit(0);});
    OK.setBounds(100,100,100,100);
    Panel.add(OK, BorderLayout.CENTER);
    Frame.getContentPane().add(Panel, BorderLayout.CENTER);
    Panel.setLocation((Frame.getWidth()-Panel.getWidth())/2,0);
    Frame.setExtendedState(JFrame.MAXIMIZED_BOTH);
    Frame.setLocation(600, 300);
    Frame.setResizable(false);
    Frame.setLayout(null);
    Frame.setVisible(true);
}

What is the fastest way to fix the issue with the panel not even showing up? Any solutions are welcomed.

Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
Frosty
  • 1
  • 1
  • "It is like match.com" sounds like this will be a web application. In that case I'd recommend not using Swing but a proper web framework like JSF. If it is meant to be a desktop application then I'd recommend to look into JavaFX, especially since you are starting to learn about the framework anyways. – Thomas Jan 17 '19 at 16:22
  • 1
    Btw, please learn the Java code conventions and use them. This will save you a lot of headaches. As an Example take this line: `Panel.add(OK, BorderLayout.CENTER);`. Here you don't know whether `Panel` is a class and thus `add` is a static method or not. According to the conventions it should be but you defined `JPanel Panel = new JPanel();` - that makes things confusing in the long run. The same goes for `Frame` and `OK`. – Thomas Jan 17 '19 at 16:24
  • You also shouldn't use a null layout. If you want, you can create your own layout. If you really need to use a null layout, the bounds must be set. Actually, you're not changing the size of your JPanel, it's leaved to 0x0. – Snowy_1803 Jan 17 '19 at 17:11
  • 1) Java GUIs have to work on different OS', screen size, screen resolution etc. using different PLAFs in different locales. As such, they are not conducive to pixel perfect layout. Instead use layout managers, or [combinations of them](http://stackoverflow.com/a/5630271/418556) along with layout padding and borders for [white space](http://stackoverflow.com/a/17874718/418556). 2) For better help sooner, [edit] to add a [MCVE] or [Short, Self Contained, Correct Example](http://www.sscce.org/). .. – Andrew Thompson Jan 17 '19 at 18:02
  • .. 3) Provide ASCII art or a simple drawing of the *intended* layout of the GUI at minimum size, and if resizable, with more width and height - to show how the extra space should be used. – Andrew Thompson Jan 17 '19 at 18:02
  • 2
    just remove the line with `setLayout(null)` - by default `JFrame` comes with a `BorderLayout` which is good to start with (and you are already adding the panel with `BorderLayout.CENTER`) [assuming that this method i being correctly called...] – user85421 Jan 17 '19 at 18:15
  • 1
    have a look at the official [tutorial](https://docs.oracle.com/javase/tutorial/uiswing/layout/visual.html) about Layout Managers (also check the others sections of that trail) – user85421 Jan 17 '19 at 18:29
  • BTW - is the button the only component that will appear in this GUI? Should it be centered (that's simple using a `GridBagLayout`)? Provide ASCII art or a simple drawing of the *intended* layout of the GUI at minimum size, and if resizable, with more width and height - to show how the extra space should be used. – Andrew Thompson Jan 17 '19 at 19:00

1 Answers1

0

Since you are a new Swing programmer, I'll try to explain with below sample code. Here I have taken your code and done few changes. See my comments in the code.

With these changes, now the program works and shows a window with a big button. When user clicks the button program exits.

import javax.swing.*;
import java.awt.BorderLayout;
import java.awt.event.ActionEvent;

public class SwingTest
{
  private void Framing() //Better method name would be "showFrame()"
  {
    JPanel Panel = new JPanel(); //Better variable name would be "panel"
    Panel.setLayout(new BorderLayout());
    JFrame Frame = new JFrame("Warning"); //Better variable name would be "frame"
    Frame.setUndecorated(true);
    JButton OK = new JButton("EXIT"); //Better variable name would be "exitButton"
    OK.addActionListener((ActionEvent event) -> {System.exit(0);});

    //Not necessary. Layout manager will handle this.
    //OK.setBounds(100,100,100,100);

    Panel.add(OK, BorderLayout.CENTER);
    Frame.getContentPane().add(Panel, BorderLayout.CENTER);

    //Not necessary. Layout manager will handle this.
    //Panel.setLocation((Frame.getWidth()-Panel.getWidth())/2,0);

    Frame.setExtendedState(JFrame.MAXIMIZED_BOTH);
    Frame.setLocation(600, 300);
    Frame.setResizable(false);

    //This is the main problem. You should avoid this.
    //Frame.setLayout(null);

    Frame.setVisible(true);
  }

  public static void main(String[] args)
  {
    new SwingTest().Framing();
  }
}
Prasad Karunagoda
  • 2,048
  • 2
  • 12
  • 16