为什么我的JPanels没有显示颜色或形状 [英] Why are my JPanels not showing color or shapes

查看:80
本文介绍了为什么我的JPanels没有显示颜色或形状的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

因此,每当我运行应用程序时,框架就在那里,但是所有颜色和矩形都没有.我要制作3个不同的菜单,每个菜单都比较难处理,所以我的框架中需要3个面板

So whenever I run the application the frame is there however all the colors and rectangles are not. I'm making 3 different menus each intractable so I need 3 panels within my frame

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

public class Application extends JPanel{

    public static void main(String[] args) {

        JFrame frame = new JFrame("FrogVibes");
        JPanel container = new JPanel();
        JPanel mainPanel = new JPanel();
        JPanel upgradePanel = new JPanel();
        JPanel frogPanel = new JPanel();
        JButton button = new JButton();

        mainPanel.setSize(400,690);
        upgradePanel.setSize(400,690);
        frogPanel.setSize(400,690);
        mainPanel.setBackground(Color.BLACK);
        upgradePanel.setBackground(Color.BLACK);
        frogPanel.setBackground(Color.BLACK);

        container.setLayout(new BoxLayout(container, BoxLayout.X_AXIS));
        container.add(mainPanel);
        container.add(upgradePanel);
        container.add(frogPanel);

        new Application() {
        };

        frame.setContentPane(new Container());
        frame.setSize(1280,700);
        frame.setVisible(true);
        frame.addWindowListener(new WindowAdapter() {
            public void windowClosing(WindowEvent e) {
                System.exit(0);
            }
        });

    }

    public void Graphics(Graphics g) {
        super.paintComponent(g);
        g.drawRect(0,700,400,100);
        g.drawRect(0, 600,100,150);
    }
}

什么是错误的位置,缺少的语句或什么

what is wrong placement, a missing statement, or what

推荐答案

您只有一个错别字,您需要像frame.setContentPane(container);一样将创建的container添加到frame中,而不是添加新容器.我只是改变颜色,以便您可以看到每个面板:

You have just a miss typos, you need to add container that you created to the frame like so frame.setContentPane(container); instead of adding a new container. and I just change colors so that you can see each panel :

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

public class Application extends JPanel{

    public static void main(String[] args) {

        JFrame frame = new JFrame("FrogVibes");
        JPanel container = new JPanel();
        JPanel mainPanel = new JPanel();
        JPanel upgradePanel = new JPanel();
        JPanel frogPanel = new JPanel();
        JButton button = new JButton("button!");

        mainPanel.setSize(400,200);
        upgradePanel.setSize(500,690);
        frogPanel.setSize(400,690);
        mainPanel.setBackground(Color.RED);
        upgradePanel.setBackground(Color.BLACK);
        frogPanel.setBackground(Color.BLUE);
        
        upgradePanel.add(button);

        container.setLayout(new BoxLayout(container, BoxLayout.X_AXIS));
        container.add(mainPanel);
        container.add(upgradePanel);
        container.add(frogPanel);

        new Application() {
        };

        frame.setContentPane(container);
        frame.setSize(1280,700);
        frame.setVisible(true);
        
        frame.addWindowListener(new WindowAdapter() {
            public void windowClosing(WindowEvent e) {
                System.exit(0);
            }
        });

}

public void Graphics(Graphics g) {
    super.paintComponent(g);
    g.drawRect(0,700,400,100);
    g.drawRect(0, 600,100,150);
}
}

这篇关于为什么我的JPanels没有显示颜色或形状的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆