-1

I am creating a Label for my GUI like this:

import javax.swing.*;


public class Runner
{
     public static void main(String[] args)
     {


final int FRAME_WIDTH = 516;
final int FRAME_HEIGHT = 700;



final JFrame myFrame = new JFrame();
myFrame.setTitle("Traveling Saleman Problem - GA solution");
myFrame.setSize(FRAME_WIDTH,FRAME_HEIGHT);
final JPanel myPanel = new JPanel();
myPanel.setLayout(null);


final JLabel genLabel = new JLabel("Number of Generations");
genLabel.setLocation(85,515);
myPanel.add(genLabel);


myFrame.add(myPanel);
myFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
myFrame.setVisible(true);
}}

I am adding it to the panel, adding the panel to the frame. I know that I am doing that right because I have buttons and TextFields showing when I run. Is there anything that I am missing from this code?

llamaCaraDara
  • 105
  • 1
  • 2
  • 8
  • You need to `repaint/validate` your panel after adding components. – Maroun May 03 '15 at 15:00
  • Can you post more code that will reproduce the problem? – copeg May 03 '15 at 15:06
  • 3
    1) For better help sooner, post an [MCVE](http://stackoverflow.com/help/mcve) (Minimal Complete Verifiable Example) or [SSCCE](http://www.sscce.org/) (Short, Self Contained, Correct Example). 2) Java GUIs have to work on different OS', screen size, screen resolution etc. 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). – Andrew Thompson May 03 '15 at 15:07
  • I would highly recommend **against** using a null Layout. Instead choose the appropriate [LayoutManager](https://docs.oracle.com/javase/tutorial/uiswing/layout/visual.html) based upon your layout requirements. Where to you wish to position your elements relative to each other? – copeg May 03 '15 at 15:21
  • I used the null layout because I'm hard coding the positions, creating a grid of buttons using an inner for loop. I'm sure that there was a better way to do it, but I have a limited knowledge of swing. I know there is the grid layout, but I am having text fields underneath the grid. Is there a way to add multiple layouts like in the android environment? – llamaCaraDara May 03 '15 at 15:31
  • *"I know there is the grid layout, but I am having text fields underneath the grid. Is there a way to add multiple layouts like in the android environment?"* Yes, and that is exactly what you would do here! In fact, if you follow the link to **combinations of them** in my first comment, there is a complete code example. That fact that you're not aware of that indicates you did *not* follow the link, so -1. – Andrew Thompson May 04 '15 at 02:00
  • I'm sorry andrew. I saw the MCVE and assumed that was all your post was about. That is my bad, and thank you for the example it is a big help!! – llamaCaraDara May 04 '15 at 03:00

3 Answers3

2

You biggest problem is using null layout. It is bound to spell trouble since graphics environments are different from each other. Factors like screen size (and resolution) and L&F must be taken into account, which is impossible with null layout.

Instead, you need to use a LayoutManager that suits your needs. It really depends on the overall GUI. Here I use the JPanel's default FlowLayout:

public class Example {

    public static void main(String[] args) {

        JFrame myFrame = new JFrame("Traveling Saleman Problem - GA solution");
        JPanel myPanel = new JPanel();
        JLabel genLabel = new JLabel("Number of Generations");
        JTextField textField = new JTextField(10);

        myPanel.add(textField);
        myPanel.add(genLabel);

        myFrame.add(myPanel);
        myFrame.pack();
        myFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        myFrame.setVisible(true);
    }
}

Notes:

  • setVisible(true) is the default for a JLabel, so you don't need it.
  • Don't setSize a JFrame, use pack instead.
  • Calling setLocationRelativeTo(null) on the frame between the calls for pack and setVisible will center the frame on the screen.
user1803551
  • 12,965
  • 5
  • 47
  • 74
0

I would advise you to use a GridLayout,it's easy to use and well adapted for your example.

this.setLayout(new GridLayout(1,1));
JLabel text=new JLabel("Try");
myPanel.add(text);
Mxsky
  • 749
  • 9
  • 27
  • How would `GridLayout` suit the OP's needs? They didn't mention they need a gird. – user1803551 May 03 '15 at 15:50
  • Using a grid is convenient in the OP's case,if he only wants to add JLabel in his frame. – Mxsky May 03 '15 at 15:55
  • OP said "*I have buttons and TextFields showing when I run.*", which clearly negates "*only wants to add JLabel*". In any case, `GridLayout` is good when you actually want a *grid* of equal size components. – user1803551 May 03 '15 at 16:08
  • I do want a grid of equal sized components, but I also want text fields and labels underneath the grid. Can you walk me through adding another panel underneath the grid layout panel? – llamaCaraDara May 03 '15 at 16:35
  • I wrote an example for you : `this.add(mypanel1,BorderLayout.NORTH);this.add(mypanel2,BorderLayout.SOUTH);`With "this" your window. – Mxsky May 03 '15 at 18:20
  • @KDowling Only way that you'll get what you want is to post a picture (draw something) of how you want your components to be arranged. – user1803551 May 03 '15 at 18:37
-3
getContentPane().add(genLabel);
mugiwaradz
  • 393
  • 1
  • 3
  • 15