Jpanel 和 Jframe 在运行时分别显示

Posted

技术标签:

【中文标题】Jpanel 和 Jframe 在运行时分别显示【英文标题】:Jpanel and Jframe showing up separately when run 【发布时间】:2020-09-05 01:51:16 【问题描述】:

我想将一堆垂直格式的按钮添加到 JPanel,然后将其添加到 JFrame 的容器中,当我运行程序时,JPanel 显示为与 JFrame 一样的单独窗口。没有错误消息。代码如下

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

public class MyFrame extends JFrame implements MouseMotionListener, ActionListener
    private JFrame f;
    private JPanel p;

    private Color currentColor;

    JButton red, yellow, white, pink, orange, magenta, light_gray, green, gray, dark_gray, cyan, blue, black; 


    public MyFrame() 
        f=new JFrame("Emoji Editor");
        f.setVisible(true);
        f.setSize(1200,1200);
        f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        gui();
    

    private void gui() //set up the color selection button to the right

        //initializing the buttons

        red = new JButton("red");
        red.setBackground(Color.red);
        red.addActionListener(this);

        yellow = new JButton("yellow");
        yellow.setBackground(Color.yellow);
        yellow.addActionListener(this);

        white = new JButton("white");
        white.setBackground(Color.white);
        white.addActionListener(this);

        pink = new JButton("pink");
        pink.setBackground(Color.pink);
        pink.addActionListener(this);

        orange = new JButton("orange");
        orange.setBackground(Color.orange);
        orange.addActionListener(this);

        magenta = new JButton("magenta");
        magenta.setBackground(Color.magenta);
        magenta.addActionListener(this);

        light_gray = new JButton("light_gray");
        light_gray.setBackground(Color.lightGray);
        light_gray.addActionListener(this);

        green = new JButton("green");
        green.setBackground(Color.green);
        green.addActionListener(this);

        gray = new JButton("gray");
        gray.setBackground(Color.gray);
        gray.addActionListener(this);

        dark_gray = new JButton("dark_gray");
        dark_gray.setBackground(Color.darkGray);
        dark_gray.addActionListener(this);

        cyan = new JButton("cyan");
        cyan.setBackground(Color.cyan);
        cyan.addActionListener(this);

        blue = new JButton("blue");
        blue.setBackground(Color.blue);
        blue.addActionListener(this);

        black = new JButton("black");
        black.setBackground(Color.black);
        black.addActionListener(this);
        //initializing the Jpanel

        JPanel colorButtonPanel=new JPanel();
        colorButtonPanel.setLayout(new GridLayout(12,1));//row,column
        colorButtonPanel.setBackground(Color.WHITE);

        //add buttons to Jpanel
        colorButtonPanel.add(black);
        colorButtonPanel.add(cyan);
        colorButtonPanel.add(dark_gray);
        colorButtonPanel.add(gray);
        colorButtonPanel.add(green);
        colorButtonPanel.add(light_gray);
        colorButtonPanel.add(magenta);
        colorButtonPanel.add(orange);
        colorButtonPanel.add(pink);
        colorButtonPanel.add(white);
        colorButtonPanel.add(yellow);
        colorButtonPanel.add(red);

        //add JPanel to Container which I assumed is referring to the JFrame???
        Container pane = this.getContentPane();
        pane.setLayout(new BorderLayout()); 
        pane.add(colorButtonPanel, BorderLayout.EAST);
        setVisible (true);

    

    //action stuff


主要是


public class main 
    public static void main (String[] args)        
        MyFrame p = new MyFrame();
    

![and a screenshot of the outcome ]1

任何帮助将不胜感激

【问题讨论】:

f.setVisible(true); 这应该是最后一个。 你正在创建JFrame 的两个实例......所以,我已经很困惑了 如需更好的帮助,请edit 添加minimal reproducible example 或Short, Self Contained, Correct Example(如我的回答中所见)。 【参考方案1】:

原始代码是打开两个框架。他们俩都需要打电话给pack()

为简单起见,此代码使用扩展的框架,但最好使用标准框架的实例。

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

public class MyFrame extends JFrame 

    JButton red, yellow, white, pink, orange, magenta, light_gray, green, gray, dark_gray, cyan, blue, black;

    public MyFrame() 
        /* all this is pointless
         f = new JFrame("Emoji Editor");
         f.setVisible(true);
         f.setSize(1200, 1200);
         f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
         */

        gui();
    

    private void gui() //set up the color selection button to the right
        //initializing the buttons
        red = new JButton("red");
        red.setBackground(Color.red);

        yellow = new JButton("yellow");
        yellow.setBackground(Color.yellow);

        white = new JButton("white");
        white.setBackground(Color.white);

        pink = new JButton("pink");
        pink.setBackground(Color.pink);

        orange = new JButton("orange");
        orange.setBackground(Color.orange);

        magenta = new JButton("magenta");
        magenta.setBackground(Color.magenta);

        light_gray = new JButton("light_gray");
        light_gray.setBackground(Color.lightGray);

        green = new JButton("green");
        green.setBackground(Color.green);

        gray = new JButton("gray");
        gray.setBackground(Color.gray);

        dark_gray = new JButton("dark_gray");
        dark_gray.setBackground(Color.darkGray);

        cyan = new JButton("cyan");
        cyan.setBackground(Color.cyan);

        blue = new JButton("blue");
        blue.setBackground(Color.blue);

        black = new JButton("black");
        black.setBackground(Color.black);
        //initializing the Jpanel

        JPanel colorButtonPanel = new JPanel();
        colorButtonPanel.setLayout(new GridLayout(12, 1));//row,column
        colorButtonPanel.setBackground(Color.WHITE);

        //add buttons to Jpanel
        colorButtonPanel.add(black);
        colorButtonPanel.add(cyan);
        colorButtonPanel.add(dark_gray);
        colorButtonPanel.add(gray);
        colorButtonPanel.add(green);
        colorButtonPanel.add(light_gray);
        colorButtonPanel.add(magenta);
        colorButtonPanel.add(orange);
        colorButtonPanel.add(pink);
        colorButtonPanel.add(white);
        colorButtonPanel.add(yellow);
        colorButtonPanel.add(red);

        //add JPanel to Container which I assumed is referring to the JFrame???
        Container pane = this.getContentPane();
        pane.setLayout(new BorderLayout());
        pane.add(colorButtonPanel, BorderLayout.EAST);
        pack();
        setVisible(true);
    

    public static void main(String[] args) 
        MyFrame p = new MyFrame();
    

【讨论】:

有趣,那我应该在哪里初始化 JFrame?

以上是关于Jpanel 和 Jframe 在运行时分别显示的主要内容,如果未能解决你的问题,请参考以下文章

在 JFrame 上更改 JPanel 组件时无法显示它

GridLayout在JFrame / JPanel中一起消失

JMenuBar显示在JFrame和JPanel中

如何将两个 JPanel 添加到中心的 JFrame?

java swing编程问题:一个jframe中添加一个jpanel后,为jpanel添加一个滚动条,当jpanel中内容过多时滑动

JPanel 更改未在 Jframe 上显示