Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p><a href="http://download.oracle.com/javase/tutorial/uiswing/components/label.html" rel="noreferrer">JLabel</a> is by defalut <code>non-Opaque</code>, even you setBackGround(whatever), then without definitions for <code>myLabel.setOpaque(true);</code> isn't <code>JLabel'area</code> colorized, another way is using <a href="http://download.oracle.com/javase/tutorial/2d/index.html" rel="noreferrer">CustomPaint</a> with overide <code>paintComponetn()</code>, for example</p> <p><img src="https://i.stack.imgur.com/83jqP.jpg" alt="enter image description here"></p> <pre><code>import java.awt.*; import javax.swing.*; public class LabelBackGround { private JFrame frame; public LabelBackGround() { JLabel lblWest = new JLabel(); lblWest.setPreferredSize(new Dimension(50, 150)); lblWest.setOpaque(true); lblWest.setBackground(Color.red); JLabel lblEast = new JLabel(); lblEast.setPreferredSize(new Dimension(50, 150)); lblEast.setOpaque(true); lblEast.setBackground(Color.red); frame = new JFrame(); frame.add(new CustomColoredComponents(), BorderLayout.NORTH); frame.add(new CustomColoredComponents(), BorderLayout.SOUTH); frame.add(lblWest, BorderLayout.WEST); frame.add(lblEast, BorderLayout.EAST); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.setLocation(100, 100); frame.pack(); frame.setVisible(true); } public static void main(String[] args) { SwingUtilities.invokeLater(new Runnable() { @Override public void run() { LabelBackGround gridBadFrame = new LabelBackGround(); } }); } } class CustomColoredComponents extends JLabel { private static final long serialVersionUID = 1L; @Override public Dimension getMinimumSize() { return new Dimension(200, 20); } @Override public Dimension getPreferredSize() { return new Dimension(200, 30); } @Override public void paintComponent(Graphics g) { int margin = 10; Dimension dim = getSize(); super.paintComponent(g); g.setColor(Color.blue); g.fillRect(margin, margin, dim.width - margin * 2, dim.height - margin * 2); } } </code></pre> <p>EDIT:</p> <p>and void that you posted has too much problem with <a href="http://download.oracle.com/javase/tutorial/uiswing/concurrency/index.html" rel="noreferrer">Concurency in Swing</a>, then all output from <code>BackGroung Tasks</code> to the GUI must be wrapped into <code>invokeLater()</code> and last code lines in you code block would be <code>revalidate ()</code> and <code>repaint()</code> for fill <code>JComponents</code> inside visible Container </p>
    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. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. 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