我想以垂直格式向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
任何帮助将不胜感激
原始代码打开了两个框架。他们两个都需要呼叫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();
}
}