-3

So I am working on this small program when I ran in to this problem.Its mostly about location set up with GridBagLayout it wont show me the text for the JLable.Another problem is that every ones in a will my progress that was working stops working and later comes back.Any idea what it is?I would also like some one too help me with the location problem I want to place the JTextField in the bottom left corner any help?I cant go around this problem and no info online to specifically help me.So maybe you can help me.Hers my code so far...

package Main;

import javax.swing.*;

import java.awt.*;
import java.awt.event.*;


public class code extends JFrame{

    public static JTextField consol;
    public static String title = "Metal-Lock:The Start";
    public static Dimension size = new Dimension(650, 550);
    public static JPanel panel;
    public static JButton enter;
    public static JLabel output;

    public static void main(String args[]) {
        code frame = new code();
    }
        public code() {
            setTitle(title);
            setSize(size);
            setResizable(true);
            setLocationRelativeTo(null);
            setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            setVisible(true);

        //  VISITOR LIST
            CONSOL();
            P1();
            P2();
}
//******************************************************************************************************************************

        public void CONSOL() {
                consol = new JTextField(30);

                consol.addActionListener(new ActionListener() {
                    public void actionPerformed(ActionEvent e){
                        String input = consol.getText(); 
                        output.setText(input); 
                    }});
                final JButton enter = new JButton("Enter");
                 enter.addActionListener(new ActionListener() {
                    public void actionPerformed(ActionEvent e){
                        String input = consol.getText(); 
                        output.setText(input);
                        panel.add(consol);
                        add(panel);

                        panel.add(enter);
                        add(panel);

                        output = new JLabel();
                        panel.add(output);
                        add(panel);
                    }
                });      
                }

//******************************************************************************************************************************
       public void P1() {

       }

//******************************************************************************************************************************
       public static JLabel grid1;
       public static JLabel grid2;
       public static JLabel grid3;
       public static JLabel grid4;
       public static JLabel grid5;

       public void P2() {

           JPanel panel = new JPanel(new GridBagLayout());
           GridBagConstraints R = new GridBagConstraints();

       JLabel grid1 = new JLabel ("Hello");  panel.add(grid1, R);
       R.gridx = 0; R.gridy = 0;
       JLabel grid2 = new JLabel ("Hello");  panel.add(grid2, R);
       R.gridx = 0; R.gridy = 0;
       JLabel grid3 = new JLabel ("Hello");  panel.add(grid3, R);
       R.gridx = 0; R.gridy = 0;
       JLabel grid4 = new JLabel ("Hello");  panel.add(grid4, R);
       R.gridx = 0; R.gridy = 0;
       JLabel grid5 = new JLabel ("Hello");  panel.add(grid5, R);
       R.gridx = 0; R.gridy = 0;

       }
}
Unihedron
  • 10,902
  • 13
  • 62
  • 72
user3024659
  • 17
  • 1
  • 2
  • where do you add components in container?? – subash Nov 23 '13 at 10:52
  • If you are using Eclipse, you could use an UI Editor. For Instance WindowBuilder. After that you can learn from the generated code too. – Gábor Csikós Nov 23 '13 at 11:03
  • TY you Gábor Csikós for showing me to the window builder but now I have to make the enter button work so that it will show what I typed in. Hers the like to my new question! http://stackoverflow.com/questions/20169052/press-on-button-equals-print-what-ever-is-on-jtextfiled-on-the-lable – user3024659 Nov 23 '13 at 23:10
  • unrelated: please learn java naming conventions and stick to them – kleopatra Dec 02 '13 at 16:11

1 Answers1

0

Just went really quickly through your code.

  1. Change these lines:

    GridBagConstraints R = new GridBagConstraints();
    R.gridx = 0; R.gridy = 0;    
    JLabel grid1 = new JLabel ("Hello");
    //important to set these R values BEFORE you ad grid1.
    panel.add(grid1, R);
    

    Change this in all lines there...

  2. You are adding 6 Labels all to the location gridx=0 and gridy=0, which is wrong. Imagine it like an excel tabel, you are inserting every label to field 1.

    If you want to add fields do it like this

    E.g.

x  y 
z

//a.gridx=0; a.gridy=0;
//y.gridx=1; y.gridy=0;
//z.gridx=0; z.gridy=1;

x coordinates are horizontal.
y are vertical starting from the top left corner.
  1. Read this article, it's really good:

Unihedron
  • 10,902
  • 13
  • 62
  • 72
Colin
  • 101
  • 8