📄 problems.html
字号:
</head><body onload="load()"> <div id=TopBar> <div id=TopBar_tr> <div id=TopBar_tl> <div id=TopBar_br> <div id=TopBar_bl> <div id=TopBar_right> <a target="_blank" href="http://java.sun.com/javase/6/download.jsp">Download the JDK</a> <br> <a href="../../search.html" target="_blank">Search the Tutorials</a> <br> <a href="javascript:toggleLeft()" id="ToggleLeft">Hide the TOC</a> </div> </div> </div> </div> </div> </div> <div class=PrintHeaders> <b>Trail:</b> Creating a GUI with JFC/Swing <br><b>Lesson:</b> Performing Custom Painting </div> <div id=LeftBar class=LeftBar_shown> <div id=Contents> <div class="linkLESSON"><a href="index.html">Performing Custom Painting</a></div><div class="linkAHEAD"><a href="concepts.html">How Swing Components Are Displayed</a></div><div class="linkAHEAD"><a href="concepts2.html">Introduction to Painting Concepts</a></div><div class="linkAHEAD"><a href="practice.html">Implementing a Custom Component</a></div><div class="linkAHEAD"><a href="summary.html">Summary</a></div><div class="nolinkAHEAD">Solving Common Painting Problems</div></div> </div> <div id=MainFlow class=MainFlow_indented> <span id=BreadCrumbs> <a href=../../index.html target=_top>Home Page</a> > <a href=../index.html target=_top>Creating a GUI with JFC/Swing</a> > <a href=index.html target=_top>Performing Custom Painting</a> </span> <div class=NavBit> <a target=_top href=summary.html>« Previous</a> • <a target=_top href=../TOC.html>Trail</a> • <a target=_top href=../QandE/questions-ch6.html>Next »</a> </div> <div id=PageTitle>Solving Common Painting Problems</div> <blockquote><strong>Problem:</strong>I don't know where to put my painting code.<ul><li>Painting code belongs in the <code>paintComponent</code> method of any component descended from <code>JComponent</code>. See <a href="concepts.html">How Swing Components Are Displayed</a> for details.</ul><strong>Problem:</strong>The stuff I paint doesn't show up.<ul><li>Check whether your component is showing up at all.<a class="TutorialLink" target="_top" href="../components/problems.html">Solving Common Component Problems</a> should help you with this.<li>Check whether <code>repaint</code> is invoked on your component whenever its appearance needs to be updated.</ul><strong>Problem:</strong>The background of my applet shows up, but the foreground stuffdoesn't show up.<ul><li>Did you make the mistake of performing paintingdirectly in a <code>JApplet</code> subclass?If so, then your contents will be covered by the content pane that is automatically created for every <code>JApplet</code> instance.Instead, create another class that performs the painting and then add that class to the <code>JApplet</code>'s content pane. See <a href="concepts.html">How Swing Components Are Displayed</a> for more information on how painting in Swing works.</ul><strong>Problem:</strong>My component's foreground shows up,but its background is invisible.The result is thatone or more components directly behind my component are unexpectedly visible.<ul><li>Make sure your component is opaque.<code>JPanel</code>s, for example, are opaque by defaultin many but not all look and feels.To make components such as <code>JLabel</code>s and GTK+ <code>JPanel</code>s opaque,you must invoke <code>setOpaque(true)</code> on them.<li>If your custom component extends <code>JPanel</code>or a more specialized <code>JComponent</code> descendant,then you can paint the background by invoking<code>super.paintComponent</code> before painting the contents of your component. <li>You can paintthe background yourself using this code at the top of a custom component's<code>paintComponent</code> method:<blockquote><pre>g.setColor(getBackground());g.fillRect(0, 0, getWidth(), getHeight());g.setColor(getForeground());</pre></blockquote></ul><p><strong>Problem:</strong>I used <code>setBackground</code>to set my component's background color,but it seemed to have no effect.<ul><li> Most likely, your component isn't painting its background, either because it's not opaque or your custom painting code doesn't paint the background. If you set the background color for a <code>JLabel</code>, for example, you must also invoke <code>setOpaque(true)</code> on the label to make the label's background be painted. For more help, see the preceding problem.</ul><p><strong>Problem:</strong>I'm using the exact same code as a tutorial example,but it doesn't work.Why?<ul><li> Is the code executed in the exact same method as the tutorial example? For example, if the tutorial example has the code in the example's <code>paintComponent</code> method, then this method might be the only place where the code is guaranteed to work.</ul><strong>Problem:</strong>How do I paint thick lines? patterns?<ul><li> The Java<sup><font size=-2>TM</font></sup> 2D API provides extensive support for implementing line widths and styles, as well as patterns for use in filling and stroking shapes. See the <a class="TutorialLink" target="_top" href="../../2d/index.html">2D Graphics</a> trail for more information on using the Java 2D API.</ul><strong>Problem:</strong>The edges of a particular component look odd.<ul><li> Because components often update their borders to reflect component state, you generally should avoid invoking <code>setBorder</code> except on <code>JPanel</code>s and custom subclasses of <code>JComponent</code>.<li> Is the component painted by a look and feel such as GTK+ or Windows XP that uses UI-painted borders instead of <code>Border</code> objects? If so, don't invoke <code>setBorder</code> on the component.<li> Does the component have custom painting code? If so, does the painting code take the component's insets into account?</ul><strong>Problem:</strong>Visual artifacts appear in my GUI.<ul><li> If you set the background color of a component, be sure the color has no transparency if the component is supposed to be opaque.<li> Use the <code>setOpaque</code> method to set component opacity if necessary. For example, the content pane must be opaque, but components with transparent backgrounds must not be opaque.<li> Make sure your custom component fills its painting area completely if it's opaque.</ul><strong>Problem:</strong>The performance of my custom painting code is poor.<ul><li> If you can paint part of your component, use the <code>getClip</code> or <code>getClipBounds</code> method of <code>Graphics</code> to determine which area you need to paint. The less you paint, the faster it will be.<li> If only part of your component needs to be updated, make paint requests using a version of <code>repaint</code> that specifies the painting region. An example of doing this is in the <code>updateSize</code> method in<a class="SourceLink" target="_blank" href="examples/SelectionDemo.java"><code>SelectionDemo.java</code></a>.<li> For help on choosing efficient painting techniques, look for the string "performance" in the <a class="OutsideLink" target="_blank" href="http://java.sun.com/products/java-media/2D/">Java 2D API home page</a>.</ul><strong>Problem:</strong>The same transforms applied toseemingly identical <code>Graphics</code> objectssometimes have slightly different effects.<ul><li> Because the Swing painting code sets the transform (using the <code>Graphics</code> method <code>translate</code>) before invoking <code>paintComponent</code>, any transforms that you apply have a cumulative effect. This doesn't matter when doing a simple translation, but a more complex <code>AffineTransform</code>, for example, might have unexpected results.</ul><p>If you don't see your problem in this list, see<a class="TutorialLink" target="_top" href="../components/problems.html">Solving Common Component Problems</a> and<a class="TutorialLink" target="_top" href="../layout/problems.html">Solving Common Layout Problems</a>. </blockquote> <div class=NavBit> <a target=_top href=summary.html>« Previous</a> • <a target=_top href=../TOC.html>Trail</a> • <a target=_top href=../QandE/questions-ch6.html>Next »</a> </div> </div> <div id=Footer><div id=TagNotes> Problems with the examples? Try <a target="_blank" href=../../information/run-examples.html>Compiling and Running the Examples: FAQs</a>. <br> Complaints? Compliments? Suggestions? <a target="_blank" href="http://developer.sun.com/contact/tutorial_feedback.jsp">Give us your feedback</a>.<br><br> <a target="_blank" href="../../information/copyright.html">Copyright</a> 1995-2006 Sun Microsystems, Inc. All rights reserved. <span id=Download></span></div> </div> <div class=PrintHeaders> <b>Previous page:</b> Summary <br><b>Next page:</b> Questions and Exercises: Performing Custom Painting </div> </body></html>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -