Reputation: 31
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
}
}
`
Upvotes: 3
Views: 50
Reputation: 18792
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.
Upvotes: 2