我真的不知道为什么不会出现此jpanel" p"?我以为我对jpanel P处于jframe的中间,应该使整个jframe红色编码,但似乎并没有这样做,并且按钮和jpanel不会出现。对不起。我知道我可能很愚蠢,但请帮忙。:?这是代码。
package com.gorillalogic.henry;
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.*;
public class Notepad {
private JFrame f; // creates all GUI components
private JPanel p;
private JButton b1;
public Notepad() {
gui();
}
public void gui() {
f = new JFrame("Notepad");
p = new JPanel();
b1 = new JButton("Quit");
b1.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
System.exit(0);
}
});
f.setSize(600, 400);
f.setLocationRelativeTo(null);
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.setVisible(true);
p.setBackground(Color.RED);
p.add(b1);
f.add(p, BorderLayout.CENTER);
}
public static void main(String[] args) {
new Notepad();
}
}
预先感谢。:)
p.setOpaque(true);
您需要这样做。