3

I have to do a Java application to make a triangle. I have to ask to the user the length of the sides. I don´t have a problem with the algorithm. I know what I have to do, but I have some difficulties with the code.

I can´t show the Text field on the panel, I search on Internet but I can´t find what´s wrong (I am still learning ), here is my code, I hope that someone will find the problem.

    package proyecto_marco1;
        import java.awt.*;
        import javax.swing.*;
        import java.awt.event.*;
        import java.awt.Container;
        import java.awt.Graphics;
        import java.awt.Color;
        import javax.swing.JTextField;



    public class Proyecto_marco1 extends JFrame implements ActionListener{
    int n;
    public JTextField textField;
    public JTextArea textArea;
    JButton b=new JButton("probar");
    JTextField t1=new JTextField(20);//object JTextFile
    JTextField t2=new JTextField(20);
    JTextField t3=new JTextField(20);

    public static void main(String[] args) {// Main class
        // TODO code application logic here
        Proyecto_marco1 m=new Proyecto_marco1();
        m.setSize(500,200);
        m.setVisible(true);

    }
    public void actionPerformed(ActionEvent o1){

        Graphics g=getGraphics();
        g.setColor(Color.blue);
        g.drawLine(50, 50, 100, 75);
        g.setColor(Color.red);
        g.drawLine(50, 50, 10, 100);
        g.setColor(Color.CYAN);
        g.drawLine(10, 100, 100, 75);

    }
    Proyecto_marco1 (){//Class constructor

        super(" Ejemplo para visualizar un boton");
        Container c=getContentPane();
        c.setLayout(null);
        c.add(b);
        b.setBounds(100, 100, 100, 20);
        b.addActionListener(this);
        c.add(t1);
        c.setBounds(400, 200, 100, 20);//size of Text field number one


    }   

}

`

rjdkolb
  • 10,377
  • 11
  • 69
  • 89
  • 1
    OMG, your [naming convention](https://en.wikipedia.org/wiki/Naming_convention_(programming)). Try to use better names for variables. It's hard to read your code because of `g`, `c`. And for the question, don't use [`null` layout](https://stackoverflow.com/questions/21242626/whats-wrong-with-the-null-layout-in-java) use layout manager. – Blasanka Aug 26 '17 at 04:01

1 Answers1

2

If you use null layout manager, you have to set bounds to every component you add: t1.setBounds(300, 100, 100, 20);
A much better practice would be to use layout managers.

c0der
  • 18,467
  • 6
  • 33
  • 65