把它放在单独的文件中,定义成public class.
package testpanels;import java.awt.*;
import java.awt.event.*;
import javax.swing.*;/**
* <p>Title: </p>
* <p>Description: </p>
* <p>Copyright: Copyright (c) 2003</p>
* <p>Company: </p>
* @author not attributable
* @version 1.0
*/
public class TestPanel extends JPanel {
public TestPanel() {
super();
}
public TestPanel(boolean isDoubleBuffered)
{
super(isDoubleBuffered);
}
public TestPanel(LayoutManager layout)
{
super(layout);
}
public TestPanel(LayoutManager layout,boolean isDoubleBuffered)
{
super(layout,isDoubleBuffered);
}
public void paint(Graphics g) {
g.setColor(Color.red);
g.fillRect(0,0,this.getWidth(),this.getHeight());
g.setColor(Color.black);
g.drawString("for swing component, override paintComponent()",10,10);
}
public void paintComponent(Graphics g) {
super.paintComponent(g);
paint(g);
}
}
///////////////////////////////////////////////
package testpanels;import java.awt.*;
import java.awt.event.*;
import javax.swing.*;/**
* <p>Title: </p>
* <p>Description: </p>
* <p>Copyright: Copyright (c) 2003</p>
* <p>Company: </p>
* @author not attributable
* @version 1.0
*/public class Frame1 extends JFrame {
JPanel contentPane;
TestPanel jPanel1 = new TestPanel(); //Construct the frame
public Frame1() {
enableEvents(AWTEvent.WINDOW_EVENT_MASK);
try {
jbInit();
}
catch(Exception e) {
e.printStackTrace();
}
}
//Component initialization
private void jbInit() throws Exception {
contentPane = (JPanel) this.getContentPane();
contentPane.setLayout(null);
this.setSize(new Dimension(400, 300));
this.setTitle("Frame Title");
jPanel1.setBounds(new Rectangle(56, 42, 64, 219));
contentPane.add(jPanel1, null);
}
//Overridden so we can exit when window is closed
protected void processWindowEvent(WindowEvent e) {
super.processWindowEvent(e);
if (e.getID() == WindowEvent.WINDOW_CLOSING) {
System.exit(0);
}
}
}
package testpanels;import java.awt.*;
import java.awt.event.*;
import javax.swing.*;/**
* <p>Title: </p>
* <p>Description: </p>
* <p>Copyright: Copyright (c) 2003</p>
* <p>Company: </p>
* @author not attributable
* @version 1.0
*/
public class TestPanel extends JPanel {
public TestPanel() {
super();
}
public TestPanel(boolean isDoubleBuffered)
{
super(isDoubleBuffered);
}
public TestPanel(LayoutManager layout)
{
super(layout);
}
public TestPanel(LayoutManager layout,boolean isDoubleBuffered)
{
super(layout,isDoubleBuffered);
}
public void paint(Graphics g) {
g.setColor(Color.red);
g.fillRect(0,0,this.getWidth(),this.getHeight());
g.setColor(Color.black);
g.drawString("for swing component, override paintComponent()",10,10);
}
public void paintComponent(Graphics g) {
super.paintComponent(g);
paint(g);
}
}
///////////////////////////////////////////////
package testpanels;import java.awt.*;
import java.awt.event.*;
import javax.swing.*;/**
* <p>Title: </p>
* <p>Description: </p>
* <p>Copyright: Copyright (c) 2003</p>
* <p>Company: </p>
* @author not attributable
* @version 1.0
*/public class Frame1 extends JFrame {
JPanel contentPane;
TestPanel jPanel1 = new TestPanel(); //Construct the frame
public Frame1() {
enableEvents(AWTEvent.WINDOW_EVENT_MASK);
try {
jbInit();
}
catch(Exception e) {
e.printStackTrace();
}
}
//Component initialization
private void jbInit() throws Exception {
contentPane = (JPanel) this.getContentPane();
contentPane.setLayout(null);
this.setSize(new Dimension(400, 300));
this.setTitle("Frame Title");
jPanel1.setBounds(new Rectangle(56, 42, 64, 219));
contentPane.add(jPanel1, null);
}
//Overridden so we can exit when window is closed
protected void processWindowEvent(WindowEvent e) {
super.processWindowEvent(e);
if (e.getID() == WindowEvent.WINDOW_CLOSING) {
System.exit(0);
}
}
}
你的这个可以改变大小但是在degin中设好大小与位置后看下源码,在看一个Degin的控件的位置又变没有了! 还得重设 存不住呀!