import java.awt.*;
import java.awt.event.*;
import javax.swing.*;public class CSDNTest extends JFrame
{
public CSDNTest()
{
JPanel a = new JPanel(null);
a.setPreferredSize(new Dimension(300,300));
a.setBackground(Color.red);
Container contentPane = getContentPane();
contentPane.setLayout(new BorderLayout());
contentPane.add(a,BorderLayout.NORTH);

setTitle("CSDN");
setBounds(0,0,600,600);
addWindowListener(new WindowAdapter()
{
public void windowClosing(WindowEvent e)
{
System.exit(0);
}
}); }

public static void main(String[] args)
{
JFrame frame = new CSDNTest();
frame.setVisible(true);
}

希望对你有所帮助!