Java GUI难倒。 JFrame中只有一个显示

时间:2014-04-12 20:00:56

标签: java swing jframe jpanel paintcomponent

我正在尝试创建包含三个Jframe的{​​{1}}。我已经延长JPanels,以便每次都可以传递颜色和直径。最终结果为JPanel,其中包含1个红色,1个黄色和1个绿色JFrame。我计划在这些面板上添加stoplightpanel,这就是为什么它是这样设计的。它不起作用,因为目前,我只能看到黄色面板。

公平警告这是一堂课。所以我尝试了所有我能想到的配置,但我仍然只看到我ActionListener中存在的子类的一个实例。如果有人能指出显而易见的我将不胜感激。奇怪的是,只显示我的黄灯。

Jframe

2 个答案:

答案 0 :(得分:4)

1-确保您的代码在EDT

中运行

2- @ Flight2039是正确的,似乎BorderLayout位置不是CENTER使用preferredSize来确定大小。因此,您可以覆盖getPreferredSize()

3-当您覆盖paintComponent(..)时,您必须致电super.paintComponent(..)以遵循绘画方法链接。更多信息here

4-添加@Override注释总是会在编译时检查,例如,如果你做了一些错字覆盖方法。

看到这个可运行的例子,我使用了gridLayout,其中包含一列和三行。

package test2;

import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.GridLayout;
import javax.swing.JFrame;
import javax.swing.JPanel;

public class TrafficLight3 {

    private JPanel redPanel;
    private JPanel yellowPanel;
    private JPanel greenPanel;

    // Constructor
    public TrafficLight3() {        
        redPanel = new StopLightPanel(100, Color.RED);
        yellowPanel = new StopLightPanel(100, Color.YELLOW);
        greenPanel = new StopLightPanel(100, Color.GREEN);
    }

    private static class StopLightPanel extends JPanel {
        private int diameter;
        private Color color;

        public StopLightPanel(int d, Color c) {
            diameter = d;
            color = c;
        }

        @Override
        public void paintComponent(Graphics g) {
            super.paintComponent(g);
            g.setColor(color);
            g.fillOval(50, 25, diameter, diameter);
        }

        @Override
        public Dimension getPreferredSize(){
            int x = diameter*2;
            return new Dimension(x,x);
        }
    }

    /**
     * Create the GUI and show it. For thread safety, this method should be
     * invoked from the event-dispatching thread.
     */
    private static void createAndShowGUI() {
        // Create and set up the window.
        JFrame frame = new JFrame("Traffic Light");
        frame.setSize(200,500);
        frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
        frame.setLayout(new GridLayout(3,1));
        frame.setLocationByPlatform(Boolean.TRUE);
        TrafficLight3 example = new TrafficLight3();
        frame.add(example.redPanel);
        frame.add(example.yellowPanel);
        frame.add(example.greenPanel);
        // Display the window.      
        frame.setVisible(Boolean.TRUE);
    }

    public static void main(String[] args) {
        // Schedule a job for the event-dispatching thread:
        // creating and showing this application's GUI.
        javax.swing.SwingUtilities.invokeLater(new Runnable() {
            public void run() {
                createAndShowGUI();
            }
        });
    }

}

输出..

enter image description here

答案 1 :(得分:3)

您需要覆盖自定义JPanel的getPreferredSize(),以便布局管理员了解制作它们的大小。中心位置将调整面板的大小以使用所有可用空间,但其他位置则不会。请参阅此示例,该示例也会删除您的setSize()setLocation(),并将其替换为对pack()的调用。

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;

public class TrafficLight3 extends JFrame {

    public static void main(String[] args) {
        SwingUtilities.invokeLater(new Runnable() {

        @Override
        public void run() {
            TrafficLight3 tl = new TrafficLight3();

        }

    });
    }

    // Constructor
    public TrafficLight3() {

        setTitle("Traffic Light");
        // setSize(200, 400);
        // setLocation(200, 200);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        StopLightPanel red = new StopLightPanel(100, Color.RED);

        // add stoplight panel's to JFrame's default border layout.
        add(red, BorderLayout.NORTH);

        StopLightPanel yellow = new StopLightPanel(100, Color.YELLOW);
        add(yellow, BorderLayout.CENTER);

        StopLightPanel green = new StopLightPanel(100, Color.GREEN);
        add(green, BorderLayout.SOUTH);
        pack();
        setLocationRelativeTo(null);
        setVisible(true);
    }

    class StopLightPanel extends JPanel {

        private int diameter;
        private Color color;

        public StopLightPanel(int d, Color c) {

            diameter = d;
            color = c;
        }

        public void paintComponent(Graphics g) {
            super.paintComponent(g);
            g.setColor(color);
            g.fillOval(50, 25, diameter, diameter);
        }

        @Override
        public Dimension getPreferredSize() {
            return new Dimension(200, 133);
        }
    }
}