本题将frame,panel独立成类。问题是我监听按钮(ButtonPanel)时如何实现在画布(GPanel)上作画呢?请各位帮助指点一下。源代码:/*SwingTest.java*/
import javax.swing.JFrame;public class SwingTest
{
public static void main( String[] args )
{
PlafFrame frame = new PlafFrame();
frame.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );
frame.setResizable(false); //固定窗口大小
frame.setVisible( true );
}
}/*PlafFrame.java*/
import java.awt.BorderLayout;
import java.awt.Container;
import java.awt.Dimension;
import java.awt.Toolkit;import javax.swing.JFrame;class PlafFrame extends JFrame
{
private int SCREEN_WIDTH = 0;
private int SCREEN_HEIGHT = 0;
private BorderLayout layout; //布局方式
private Container container; //容器 public PlafFrame()
{
Toolkit kit = Toolkit.getDefaultToolkit();
Dimension screenSize = kit.getScreenSize(); //获取屏幕大小
SCREEN_WIDTH = screenSize.width;
SCREEN_HEIGHT = screenSize.height;

layout = new BorderLayout();
container = getContentPane();
container.setLayout( layout );

ButtonPanel bPanel = new ButtonPanel();
GPanel gPanel = new GPanel();
container.add( bPanel, BorderLayout.NORTH ); //添加按钮面板
container.add( gPanel, BorderLayout.CENTER ); //添加绘图面板

setTitle( "画图" );
setSize( SCREEN_WIDTH / 2, SCREEN_HEIGHT / 2 );
setLocation(SCREEN_WIDTH / 4, SCREEN_HEIGHT / 4 );
}
}/*ButtonPanel.java*/
import java.awt.Container;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;import javax.swing.JButton;
import javax.swing.JPanel;
import javax.swing.JTextField;
class ButtonPanel extends JPanel
{
private String infos[] = { "正方形", "圆形", "线条", "图像", "清空" };
private int count = -1;
private Container container;

public int getCount()
{
return count;
}

public ButtonPanel()
{
for( String info : infos )
makeButton( info );
}

void makeButton( String name )
{
JButton button = new JButton( name );
add( button );

button.addActionListener(new ActionListener()
{
public void actionPerformed( ActionEvent event )
{
try
{
if( ( ( JButton )event.getSource() ).getText() == infos[ 0 ] ) 
{
GPanel.tag=0;
}
else if( ( ( JButton )event.getSource() ).getText() == infos[ 1 ] ) GPanel.tag=0;
else if( ( ( JButton )event.getSource() ).getText() == infos[ 2 ] ) GPanel.tag=1;
else if( ( ( JButton )event.getSource() ).getText() == infos[ 3 ] ) GPanel.tag=2;
else if( ( ( JButton )event.getSource() ).getText() == infos[ 4 ] ) GPanel.tag=-1;
}
catch( Exception e )
{
e.printStackTrace();
}
}
});
}
}/*GPanel.java*/
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.geom.Ellipse2D;
import java.awt.geom.Line2D;
import java.awt.geom.Rectangle2D;import javax.swing.JPanel;class GPanel extends JPanel
{
public static int tag = -1;

public void paintComponent( Graphics g )
{
super.paintComponent( g ); Graphics2D g2 = ( Graphics2D ) g;

double leftX = 150;  
double topY = 50;
double width = 200;
double height = 200;

//圆心的位置
double centerX = leftX + width / 2;
double centerY = topY + height / 2;
double radius = 100; switch( tag )
{
case 0: //正方形
Rectangle2D rect = new Rectangle2D.Double( leftX, topY, width, height );
g2.setPaint( Color.GREEN );
g2.fill( rect );
break;
case 1: //圆形
Ellipse2D circle = new Ellipse2D.Double();
circle.setFrameFromCenter( centerX, centerY, centerX + radius, centerY + radius );
g2.setPaint( Color.RED );
g2.fill( circle );
break;
case 2: //线条
g2.setPaint( Color.YELLOW );
g2.draw( new Line2D.Double( leftX, topY, leftX + width, topY + height ) );
break;
}
}
}

解决方案 »

  1.   

    修改ButtonPanel的构造函数:
    public ButtonPanel(GPanel panel) {
    this.graphicsPanel = panel;
    for (String info : infos) {
    makeButton(info);
    }
    }
    修改PlafFrame的构造函数:
    GPanel gPanel = new GPanel();
    ButtonPanel bPanel = new ButtonPanel(gPanel);
    修改class GPanel:
    private int tag = -1;

    public int getTag() {
    return this.tag;
    }public void setTag(int tag) {
    this.tag = tag;
    this.repaint();
    }
    修改ButtonPanel的匿名内部class:
    button.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent event) {
    try {
    if (((JButton) event.getSource()).getText() == infos[0]) {
    ButtonPanel.this.graphicsPanel.setTag(0);
    } else if (((JButton) event.getSource()).getText() == infos[1])
    ButtonPanel.this.graphicsPanel.setTag(1);
    else if (((JButton) event.getSource()).getText() == infos[2])
    ButtonPanel.this.graphicsPanel.setTag(2);
    else if (((JButton) event.getSource()).getText() == infos[3])
    ButtonPanel.this.graphicsPanel.setTag(3);
    else if (((JButton) event.getSource()).getText() == infos[4])
    ButtonPanel.this.graphicsPanel.setTag(-1);
    } catch (Exception e) {
    e.printStackTrace();
    }
        }
    });