繁体   English   中英

Jpanel 和 Jframe 在运行时分别显示

[英]Jpanel and Jframe showing up separately when run

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

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

![和结果截图] 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();
    }
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM