Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<blockquote> <p>What are the problems with settings bounds on non-resizable containers?</p> </blockquote> <p>Suppose you adjust the bounds to look good on your platform. Suppose the user's platform has a font with different, say larger, <code>FontMetrics</code>. This example is somewhat contrived, but you get the idea. If you change the bounds of a non-resizable container, be sure any text is visible regardless of the host platform's default font.</p> <p><img src="https://i.stack.imgur.com/fGcUG.png" alt="image"></p> <pre><code>import java.awt.BorderLayout; import java.awt.EventQueue; import javax.swing.JFrame; import javax.swing.JLabel; import javax.swing.JPanel; import javax.swing.SwingUtilities; /** * @see http://stackoverflow.com/a/12532237/230513 */ public class Evil extends JPanel { private static final String s = "Tomorrow's winning lottery numbers: 42, "; private JLabel label = new JLabel(s + "3, 1, 4, 1, 5, 9", JLabel.LEFT); public Evil() { this.add(label); } private void display() { JFrame f = new JFrame("Evil"); f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); f.add(this, BorderLayout.WEST); f.pack(); int w = SwingUtilities.computeStringWidth( label.getFontMetrics(label.getFont()), s); int h = f.getHeight(); f.setSize(w, h); f.setResizable(false); f.setLocationRelativeTo(null); f.setVisible(true); } public static void main(String[] args) { EventQueue.invokeLater(new Runnable() { @Override public void run() { new Evil().display(); } }); } } </code></pre>
 

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