Note that there are some explanatory texts on larger screens.

plurals
  1. POJava Swing Application - buttons do not appear if resizable is set to false
    text
    copied!<p>I have the following code. Class KochSnowflakesMenu is a grid JPanel with three buttons. Class KochSnowflakesDraw currently draws a circle using drawOval:</p> <pre><code>import javax.swing.*; import java.awt.*; public class KochSnowflakes { public static void main(String[] args) { JFrame frame = new JFrame("Koch Snowflakes"); frame.setVisible(true); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.setBounds(0,0, 600, 425); frame.setBackground(Color.BLACK); frame.setResizable(false); frame.setLayout(null); // Create the button interface frame.add(new KochSnowflakesMenu()); frame.add(new KochSnowflakesDraw()); frame.repaint(); } } </code></pre> <p>This works if I comment out frame.setResizable(false). When I don't the buttons don't appear. Why is that? As you can see, I have tried using repaint(). I had previously the problem that the buttons would not show up until I manually resized the window... </p> <p>Also, as a bonus question, if anyone can tell me how to get the dimensions of a JPanel that would be great. The reason why I can't use a resizable layout manager such as BorderLayout, which really is what I want to use, is that I can't figure out the dimension of the JPanel that occupies the center (and hence have no idea how to large to draw the things I'm drawing).</p> <p>EDIT: As requested, here is the KockSnowflakesMenu class:</p> <pre><code>import javax.swing.*; import java.awt.*; public class KochSnowflakesMenu extends JPanel { public KochSnowflakesMenu() { setLayout(new GridLayout(3,1)); setBounds(0,0,200,400); JButton button_red = new JButton("Red"); JButton button_yellow = new JButton("Yellow"); JButton button_blue = new JButton("Blue"); add(button_red); add(button_yellow); add(button_blue); } } </code></pre> <p>And, just to be sure I didn't mess something up with KochSnowflakesDraw, here's that class as well:</p> <pre><code>import javax.swing.*; import java.awt.*; public class KochSnowflakesDraw extends JPanel { public KochSnowflakesDraw() { setLayout(null); setBounds(200, 0, 400, 400); } public void paintComponent(Graphics g) { g.setColor(Color.RED); g.drawOval(0,0,400, 400); } } </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