面板上的画线

扫码查看
本文介绍了面板上的画线的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

几天来我一直无法使这段代码正常工作


I was never able to make this code work for days now


public void paint(Graphics comp) {
   comp.setColor(myColor);
   comp.fillRoundRect(0, 0, this.getWidth(), this.getHeight(), 10, 25);
   comp.setColor(Color.green);
   comp.setFont(new Font("Times New Roman",Font.BOLD,22));
   comp.drawString("test",0,0);
   super.paint(comp);
                        }




如果必须仅在面板上显示一个字符串.这是整个面板的代码




if must just display a string on the panel. Here is the code for the whole panel

class Item extends JPanel{
    private int index;
    private JLabel myLabel;
    private Color myColor;

    private boolean movingDown;
    int Destination;
    @SuppressWarnings("serial")
    private final Timer motion = new Timer(15, new ActionListener(){
        public void actionPerformed(ActionEvent arg0){

            setLocation(0, getY() + getMotion());
            if ((Destination == getY() && movingDown) ||
                (Destination >= getY() + getHeight() && !movingDown)){
                motion.stop();
                }

        }
    }){
        public void start(){
            if(movingDown)
                Destination = getY() + getHeight();
            else
                Destination = getY();
            super.start();
            if (Destination < mainPosition*(1+.2) &&
                Destination > mainPosition*(1-.2))
                    myColor = Color.green;
            else
                try{
                    if (!myColor.equals(Color.blue))
                        myColor = Color.blue;
                }catch(NullPointerException e){
                    myColor = Color.blue;
                }
        }
    };

    Item(String item, int position, int index){
        super(new BorderLayout());
//      this.setBounds(0, 0, defaultSize.width, defaultSize.height);
        this.setLocation(0, position);
        if (position==mainPosition) myColor = Color.green;
        else myColor = Color.blue;
        this.setOpaque(false);
//      this.add(myLabel=new JLabel(item,JLabel.CENTER));
//      myLabel.setForeground(Color.white);
//      myLabel.setFont(new Font(Font.SANS_SERIF,Font.PLAIN,22));
        this.index = index;
    }

    public int getMotion() {
        if (movingDown) return 1;
        return -1;
    }


    public void setMovingDirection(int direction) {
        movingDown = (direction==1);
        motion.start();
    }

    public void moveUp(){
        setMovingDirection(-1);
    }

    public void moveDown(){
        setMovingDirection(1);
    }

    public void setLocation(int x, int y) {
        if(y==mainPosition){
            this.setSize(defaultSize);
        }
        else{
            final int reduce;
            if(mainPosition<y)
                reduce = (y-mainPosition)/2;
            else
                reduce = mainPosition-y;
            this.setSize(defaultSize.width - reduce,
                        defaultSize.height - reduce);
            x = (defaultSize.width - this.getWidth())/2;

        super.setLocation(x, y);
    }

    public void paint(Graphics comp) {
        comp.setColor(myColor);
        comp.fillRoundRect(0, 0, this.getWidth(), this.getHeight(), 10, 25);
        comp.setColor(Color.green);
        comp.setFont(new Font("Times New Roman",Font.BOLD,22));
        comp.drawString("test",0,0);
        super.paint(comp);
    }

}

推荐答案


这篇关于面板上的画线的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-05 13:21
查看更多