开发者

Java Swing Application - buttons do not appear if resizable is set to false

开发者 https://www.devze.com 2023-01-27 06:58 出处:网络
I have the following code. Class KochSnowflakesMenu is a grid JPanel with three buttons. Class KochSnowflakesDraw currently draws a circle using drawOval:

I have the following code. Class KochSnowflakesMenu is a grid JPanel with three buttons. Class KochSnowflakesDraw currently draws a circle using drawOval:

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 KochSnowflak开发者_高级运维esMenu());
        frame.add(new KochSnowflakesDraw());
        frame.repaint();
    }
}

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...

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).

EDIT: As requested, here is the KockSnowflakesMenu class:

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);
    }
}

And, just to be sure I didn't mess something up with KochSnowflakesDraw, here's that class as well:

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);
    }
}


A general point, when using JFrame, you should be using the contentPane, rather than the JFrame itself, so to add items, try

frame.getContentPane().add(.....);

For your first question, try using pack on your JFrame. http://download.oracle.com/javase/1.4.2/docs/api/java/awt/Window.html#pack()

For your bonus question, JComponent has a getWidth and getHeight method. This will tell you the current size of the JPanel.

http://download.oracle.com/javase/1.4.2/docs/api/javax/swing/JComponent.html#getWidth()


Adding more info to the previous answers... The size is random until the component is drawn so make sure you have a setVisible(true) on your frame. Here's your code w/some modifications that let you use the BorderLayout and get the size of your drawing panel. I substituted some fake buttons for your interfaces but you'll get the drift.


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(new BorderLayout());

JPanel buttons = new JPanel(new FlowLayout());
buttons.add(new JButton("MENU"));
buttons.add(new JButton("DRAW"));
frame.add(buttons, BorderLayout.SOUTH);

JPanel drawArea = new JPanel();
drawArea.setBackground(Color.BLUE);
frame.add(drawArea, BorderLayout.CENTER);
frame.setVisible(true);
Dimension drawAreaDim = drawArea.getSize();
System.out.println(drawAreaDim);


When you add components to a visible frame the code should be something like:

panel.add(...);
panel.revalidate();
panel.repaint(); // sometimes required


Make sure you create your Swing objects on the Event Dispatch Thread (EDT). In your example you aren't, and that is often the candidate when you get weird, inconsistent behavior. Swing isn't thread-safe, and relies on creation and modification of Swing objects on the EDT.

To remedy, just wrap the contents of your main method in a SwingUtilities.invokeLater call like so:

public static void main(String[] args)
{
    SwingUtilities.invokeLater(new Runnable() {
        @Override
        public void run()
        {
            JFrame frame = new JFrame("Koch Snowflakes");
            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.setVisible(true);
        }
    });
}

That will create your JFrame and other components on the EDT thread. Does that fix the inconsistent "it doesn't work most of the time" behavior?

Also, I prefer to call setVisible last...though it probably doesn't matter.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号