2 qq 28144595 qq_28144595 于 2015.05.25 23:33 提问

怎样使界面成为下面的样子,求好心人帮帮忙。。

图片说明
源程序:import java.awt.*;
import java.awt.event.*;

import javax.swing.*;
public class RecJFrame extends JFrame implements ActionListener,ComponentListener {
private JRadioButton colorsbutton[]; //单选按钮组
private RecNCanvas canvas; //自定义画布组件
public RecJFrame()
{
super("图画—颜色"); //框架边布局
Dimension dim=getToolkit().getScreenSize(); //获得屏幕分辨率
this.setBounds(dim.width/4,dim.height/4,dim.width/2,dim.height/2); //窗口居中
this.setDefaultCloseOperation(EXIT_ON_CLOSE);
this.addComponentListener(this); //注册组件事件监听器

    JToolBar toolbar=new JToolBar();                   //创建工具栏,默认水平方向
    this.getContentPane().add(toolbar,"North");        //工具栏添加到框架内容窗格北部
    JButton button_color = new JButton("选择颜色");
    toolbar.add(button_color);
    button_color.addActionListener(this);
    String colors[]={"红","黄","蓝"};
    ButtonGroup bgroup = new ButtonGroup();            //按钮组
    colorsbutton = new JRadioButton[colors.length];    //单选按钮数组
    for (int i=0; i<colorsbutton.length; i++)
    {
        colorsbutton[i]=new JRadioButton(colors[i]);   //单选按钮
        colorsbutton[i].addActionListener(this);
        bgroup.add(colorsbutton[i]);                    //单选按钮添加到按钮组
        toolbar.add(colorsbutton[i]);                   //单选按钮添加到工具栏
    }
    canvas = new RecNCanvas();                //创建自定义画布组件
    this.getContentPane().add(canvas,"Center");
    this.setVisible(true); //创建自定义画布组件this.getBackground()Color.yellow
}
    public void actionPerformed(ActionEvent e)             //按钮动作事件处理方法
    {
       if(e.getActionCommand().equals("红"))
        {
            canvas.setColor(Color.red);
            }
       if(e.getActionCommand().equals("蓝"))
            {
                canvas.setColor(Color.blue);
                } 
       if(e.getActionCommand().equals("黄"))
            {
                canvas.setColor(Color.yellow);
                }
       if (e.getActionCommand().equals("选择颜色"))
        {
            Color c=JColorChooser.showDialog(this,"选择颜色",Color.blue); //弹出JColorChooser颜色选择对话框,返回选中颜色
            canvas.setColor(c);
        }
            canvas.repaint();//需要在这里加上repaint
    }
    public void componentHidden(ComponentEvent arg0) {}
    public void componentMoved(ComponentEvent arg0) {}
    public void componentResized(ComponentEvent arg0) {}
    public void componentShown(ComponentEvent arg0) {}
    public static void main(String arg[])
    {
        new RecJFrame();
    }

}
class RecNCanvas extends Canvas
{
private Color color;
public void setColor(Color color)
{
this.color=color;
}
public void paint(Graphics g)
{
int x0=this.getWidth()/4;
int y0=this.getHeight()/4;
g.setColor(this.color);
g.drawRect(x0,y0,250,100);
}
}

1个回答

danielinbiti
danielinbiti   Ds   Rxr 2015.05.26 01:03
已采纳

加一段代码。搜索canvas = new RecNCanvas(); //创建自定义画布组件,在这前后增加内容

 JPanel jp = new JPanel(new FlowLayout(FlowLayout.RIGHT));
    jp.setPreferredSize(new Dimension(150, 50));
    JPanel jp2 = new JPanel();
    jp2.setPreferredSize(new Dimension(50, 100));
    jp2.setBorder(BorderFactory.createLineBorder(Color.blue));//设置面板边框颜
    ButtonGroup bgroup2 = new ButtonGroup();            //按钮组
    JRadioButton[] colorsbutton2 = new JRadioButton[colors.length];    //单选按钮数组
    for (int i=0; i<colorsbutton2.length; i++)
    {
        colorsbutton2[i]=new JRadioButton(colors[i]);   //单选按钮
        colorsbutton2[i].addActionListener(this);
        bgroup2.add(colorsbutton[i]);                    //单选按钮添加到按钮组
        jp2.add(colorsbutton2[i]);
    }
    jp.add(jp2);
    canvas = new RecNCanvas();                //创建自定义画布组件
    this.getContentPane().add(jp,"West");
    this.getContentPane().add(canvas,"Center");
qq_28144595
qq_28144595 感谢死你了。。。。你是个好人
接近 3 年之前 回复
Csdn user default icon
上传中...
上传图片
插入图片
准确详细的回答,更有利于被提问者采纳,从而获得C币。复制、灌水、广告等回答会被删除,是时候展现真正的技术了!