【问题标题】:Why are my panels not showing up in the JFrame为什么我的面板没有出现在 JFrame 中
【发布时间】:2012-10-20 12:34:21
【问题描述】:
public class InputPanel extends JPanel{
    public static int shapeType; //1: Rectangle; 2: Oval; 3: Line
    public static boolean isFilled; //whether or not the shape is filled
    public static Color color; //color of the shape

    public InputPanel(){

        JPanel panel = new JPanel(new FlowLayout(FlowLayout.LEFT));
        panel.setBackground(Color.GRAY);
        setPreferredSize(new Dimension(200,500));

        JButton rect = new JButton("Rectangle");
        JButton oval = new JButton("Oval");
        JButton line = new JButton("Line");
        JRadioButton fill = new JRadioButton("Filled:");
        JButton color1 = new JButton("Color..");

        rect.addActionListener(new rectListener());
        oval.addActionListener(new ovalListener());
        line.addActionListener(new lineListener());
        isFilled = fill.isSelected();
        color1.addActionListener(new colorListener());

        panel.add(rect);
        panel.add(oval);
        panel.add(line);
        panel.add(fill);
        panel.add(color1);

        this.setVisible(true);

    }
}



public class PaintPanel extends JPanel{

    public static int x1, y1, x2, y2;

    ArrayList<Shape> shapeList = new ArrayList<Shape>();

    public PaintPanel(){

        JPanel panel = new JPanel(new FlowLayout(FlowLayout.RIGHT));
        panel.setBackground(Color.WHITE);
        setPreferredSize(new Dimension(500, 500));

        this.addMouseListener(new MouseAdapter() {
        @Override public void mousePressed(MouseEvent e) {
            PaintPanel.x1 = e.getX();
            PaintPanel.y1 = e.getY();
        }

        @Override public void mouseReleased(MouseEvent e) {
            PaintPanel.x2 = e.getX();
            PaintPanel.y2 = e.getY();
            if(InputPanel.shapeType == 1){
                shapeList.add(new Rectangle(PaintPanel.x1, PaintPanel.y1, PaintPanel.x2, PaintPanel.y2, InputPanel.isFilled));
            }
            if(InputPanel.shapeType == 2){
                shapeList.add(new Oval(PaintPanel.x1, PaintPanel.y1, PaintPanel.x2, PaintPanel.y2, InputPanel.isFilled));
            }   
            if(InputPanel.shapeType == 3){
                shapeList.add(new Line(PaintPanel.x1, PaintPanel.y1, PaintPanel.x2, PaintPanel.y2));
            }
            repaint();
        }
      });

     this.setVisible(true);   
    }

    @Override 
    public void paintComponent(Graphics g){
        super.paintComponent(g);

        for(Shape s : shapeList){
            s.draw(g);
        }
    }

}

public class PaintGUI {

    public static void main(String[] args){

        JFrame frame = new JFrame("Shape Drawer!");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        frame.add(new InputPanel());
        frame.add(new PaintPanel());

        frame.pack();

        frame.setVisible(true);

    }

}

我很肯定我已经正确地创建了 JFrame 并且我的所有其他类都可以正常工作,但是这里一定有一些我遗漏的东西...... 当我运行 main 方法时,我得到的只是一个灰色框,它显然是一个正方形(500x500,在 PaintPanel 类中实例化。我做错了什么?

【问题讨论】:

  • 如需尽快获得更好的帮助,请发帖SSCCE
  • 我很难在您的代码中找到JFrame。你为什么在你的面板上写setVisible(true)
  • 我添加了 JFrame 并将其设置为真正的乱七八糟,以尝试解决面板不显示的问题
  • frame.add(new InputPanel()); frame.add(new PaintPanel()); 框架的默认布局将只采用一个没有约束的组件。将其更改为 GridLayout(1,0)
  • 1. class PaintPanel 在该类中创建一个未添加到此面板的面板添加以下语句 this.add(panel); 2.InputPanel : 同上 this.add(panel);最后你没有为 JFrame 和 JPanel 设置 LayoutManage 所以你也需要设置它

标签: java swing jframe jpanel visibility


【解决方案1】:

除了 Andrew 提到的之外,我注意到在您的 InputPanelPaintPanel 中,您正在创建一个新的 JPanel。您肯定会在此面板中添加新组件,但最后您不会将此 JPanel 本身添加到您的 InputPanelPaintPanel。因此,请确保在这些面板的构造函数中,最后有一个 add(panel)

另外,请注意,Swing 中的大多数操作都不是线程安全的,因此请在创建/与 UI 组件交互之前阅读"Concurrency in Swing"。换句话说,用户界面的任何更新都必须发生在事件派发线程上,例如应用程序的启动:

public static void main(String[] args){

    SwingUtilities.invokeLater(new Runnable() {
        @Override
        public void run() {
            JFrame frame = new JFrame("Shape Drawer!");
            frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

            //set the layout, add your panels

            frame.pack();
            frame.setVisible(true);             
        }
    });
}

【讨论】:

  • @user1787458: 很高兴为您提供帮助:)
【解决方案2】:

JFrame 默认使用 BorderLayout。

frame.add(new InputPanel());
frame.add(new PaintPanel());

相当于说,

frame.add(new InputPanel(), BorderLayout.CENTER);
frame.add(new PaintPanel(), BorderLayout.CENTER);

最终结果是最后添加的面板将是可见的,前提是您的其余代码工作正常。

【讨论】:

    猜你喜欢
    • 2022-01-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-02-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多