Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<ul> <li><p><code>this.setVisible(true)</code> must be last code line in the GUI constructor</p></li> <li><p>use <a href="http://java.about.com/od/javasyntax/a/nameconventions.htm" rel="noreferrer">Using Java Naming Conventions</a> correctly then <code>Panel1</code> should be <code>panel1</code> e.i.</p></li> <li><p>don't extends <code>JFrame</code> or <code>JApplet</code>, create that as local variable with the same way as for <code>Panel1</code></p></li> <li><p>don't use <code>NullLayout</code>, use proper <code>LayoutManager</code> instead, in this case <code>GridLayout</code> probably, otherwise <code>JFrames</code> contents isn't resiziable with <code>JFrame</code></p></li> </ul> <p><img src="https://i.stack.imgur.com/ehyVP.jpg" alt="enter image description here"></p> <p>code</p> <pre><code>import java.awt.*; import javax.swing.*; public class ColorongPanels { private JFrame frame = new JFrame("ColorongPanels"); private JPanel panel1 = new JPanel(); private JPanel panel2 = new JPanel(); private JPanel panel3 = new JPanel(); private JPanel panel4 = new JPanel(); private JLabel label1 = new JLabel("Label1"); private JLabel label2 = new JLabel("Label2"); private JLabel label3 = new JLabel("Label3"); private JLabel label4 = new JLabel("Label4"); public ColorongPanels() { frame.setLayout(new GridLayout(2, 2, 5, 5)); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); panel1.setBackground(Color.red); panel1.setLayout(new BorderLayout()); panel1.add(label1, BorderLayout.CENTER); panel2.setBackground(Color.yellow); panel2.setLayout(new BorderLayout()); panel2.add(label2, BorderLayout.CENTER); panel3.setBackground(Color.black); panel3.setLayout(new BorderLayout()); panel3.add(label3, BorderLayout.CENTER); panel4.setBackground(Color.pink); panel4.setLayout(new BorderLayout()); panel4.add(label4, BorderLayout.CENTER); frame.add(panel1); frame.add(panel2); frame.add(panel3); frame.add(panel4); frame.setLocationRelativeTo(null); frame.pack(); frame.setVisible(true); } public static void main(String[] args) { SwingUtilities.invokeLater(new Runnable() { @Override public void run() { new ColorongPanels(); } }); } } </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