Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>The proper (better to say, easier, much less error prone) way to do it has been shown by David Kroukamp. As for your original question, modify the original code like this:</p> <pre><code>line1.setBounds(350+insets.left,75+insets.top,size.width+50,size.height+20); line1.setBorder(BorderFactory.createEtchedBorder()); // add this line </code></pre> <p>Your line starts at point 200,300 but the panel you're drawing it in has width of 50+10 and height of 20+10 - on my computer at least - which means that the line is outside of the <code>drawingLine</code> panel and that's why it doesn't get drawn. To verify that modify the line in your original <code>drawingLine.paintComponent</code> like this:</p> <pre><code>Line2D line = new Line2D.Double(0, 0, 1000, 1000); </code></pre> <p>and you will see the following result:</p> <p><img src="https://i.stack.imgur.com/Sg71n.png" alt="enter image description here"></p> <p>This line in your original code:</p> <pre><code>pane.setLayout(null); </code></pre> <p>is pretty clear - you are using no layout manager, or in other words you chose to use <a href="http://docs.oracle.com/javase/tutorial/uiswing/layout/none.html" rel="nofollow noreferrer">absolute positioning</a>. This means that the coordinates, widths and heights of components must be precalculated and set by you. But if you make a mistake somewhere (as your example nicely shows) it'll be hard to detect. Not to mention, for example if you want to handle window resizing. That's why <a href="http://docs.oracle.com/javase/tutorial/uiswing/layout/visual.html" rel="nofollow noreferrer">LayoutManagers</a> exist.</p> <p>I recommend reading also:</p> <ul> <li><a href="http://docs.oracle.com/javase/tutorial/uiswing/layout/using.html" rel="nofollow noreferrer">Using Layout Managers</a></li> <li><a href="http://docs.oracle.com/javase/tutorial/uiswing/layout/problems.html" rel="nofollow noreferrer">Solving Common Layout Problems</a></li> </ul>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload