0

I create this class to draw the line and extend it as a component.

class MyCanvas extends JComponent {

    public void paint(Graphics g)
    {
        g.drawLine(30, 20, 80, 90);
    }
} 

...

frame.getContentPane().add(new MyCanvas());

After drawing, when adding text fields to frame, it disappears.

frstVectorField = new JTextField("");
frstVectorField.setBounds(600, 50, 160, 30);

frame.add(frstVectorField);
frame.setLayout(null);.
  • Avoid the use of `null layout`, [here's why](https://stackoverflow.com/a/42521097/2180785). What do you mean by "move up the line"? For better help sooner post a proper [mre] that shows how your GUI look like and if / when resized how should the componenents behave. – Frakcool Jul 07 '20 at 18:23

1 Answers1

0

The default content pane is a JPanel with BorderLayout layout manager. When you call method add(Component), of class JPanel, the Component gets placed in the CENTER area of the JPanel. Hence a subsequent call to method add(Component) will replace the Component that was previously added, since the CENTER area can only hold one Component.

I recommend the tutorial Creating a GUI With JFC/Swing

Abra
  • 19,142
  • 7
  • 29
  • 41