0

I am creating a GUI using BlueJ - Java, i have made the entry boxes however i cant seem to add a label to go either above each one or to the left. Could anyone help me out and tell me where im going wrong ? My code is below:

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.JFrame;
import javax.swing.JLabel;


public class Log extends JFrame {


    public static void main(String[] args){
        Log frameTabel = new Log();
    }

    JButton Confirm = new JButton("Confirm");
    JPanel panel = new JPanel();
    JLabel label1 = new JLabel("Name", JLabel.RIGHT);
    JTextField FullName = new JTextField(15);
    JTextField Address1line = new JTextField(15);
    JTextField postcode = new JTextField(15);
    JTextField Destination = new JTextField(15);
    JTextField Date = new JTextField(15);
    JTextField MilesTravelling = new JTextField(15);
    JLabel lblMsg = new JLabel ("Name",JLabel.LEFT);

    Log(){
        super("Customer GUI");
        setSize(300,400);
        setLocation(400,250);
        panel.setLayout(null);

        FullName.setBounds(70,30,150,20);
        Address1line.setBounds(70,80,150,20);
        postcode.setBounds(70,130,150,20);
        Destination.setBounds(70,180,150,20);
        Date.setBounds(70,230,150,20);
        MilesTravelling.setBounds(70,280,150,20);
        Confirm.setBounds(105,320,80,20);

        panel.add(lblMsg);
        panel.add(Confirm);
        panel.add(FullName);
        panel.add(Address1line);
        panel.add(postcode);
        panel.add(Destination);
        panel.add(Date);
        panel.add(MilesTravelling);
        getContentPane().add(label1);

        getContentPane().add(panel);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setVisible(true);

    }
}
Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
  • 2
    Don't use null layout. Yse code conventions. Variables in java should not start from capital letter. Move the setSize() just before the setVisible(true) – StanislavL May 02 '14 at 13:28
  • You probably shouldn't be adding your `panel` and `label` seperately to the content pane. I would probably call the `super.add()` instead. – Elliott Frisch May 02 '14 at 13:29
  • 1
    Java GUIs might have to work on a number of platforms, on different screen resolutions & using different PLAFs. As such they are not conducive to exact placement of components. To organize the components for a robust GUI, instead use layout managers, or [combinations of them](http://stackoverflow.com/a/5630271/418556), along with layout padding & borders for [white space](http://stackoverflow.com/q/17874717/418556). – Andrew Thompson May 02 '14 at 14:37
  • This is not your private help desk, but a public Q&A site. Removing code from a Q&A is vandalism as the answers become nonsense. Stop it. – Andrew Thompson May 03 '14 at 07:18

1 Answers1

1

.

getContentPane().add(label1); //JFrames CENTER area
getContentPane().add(panel);
  • then last added JComponent can be visible

.

getContentPane().add(panel);
  • suggestions don't to use NullLayout and Log frameTabel = new Log(); should be wrapped into invokeLater (Swing GUI should be created and intialized on EventDispatchThread), more to see in Oracle tutorial Initial Thread
mKorbel
  • 109,525
  • 20
  • 134
  • 319