相同类的java graphics2d实例表现不同

时间:2012-03-16 08:16:45

标签: java swing graphics2d

Java新手,我不明白为什么以下代码适用于我添加到表单的第一个按钮而不是同一个类中的其他两个实例。此外,mouseExit处理程序似乎根本没有被调用,或者只有在退出所有按钮时才会被调用,而不是单独退出所有按钮。你能救我吗?

设置是我有一个带有简单JFrame,contentPane和带有显示钢琴键盘图标的JLabel的测试用例。就是这样。我创建了表示键的多边形按钮的实例,当鼠标经过它们时,它们所要做的就是hilight,第一个执行,其他执行文本而不是图形 - 这意味着调用了正确的处理程序而没有绘制完了。

package com.mst.buttons;

import java.awt.BorderLayout;

public class buttonFrame extends JFrame {

    private JPanel contentPane;

    /**
     * Launch the application.
     */
    public static void main(String[] args) {
        EventQueue.invokeLater(new Runnable() {
            public void run() {
                try {
                    buttonFrame frame = new buttonFrame();
                    frame.setVisible(true);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        });
    }

    /**
     * Create the frame.
     */
    public buttonFrame() {
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setBounds(100, 100, 578, 373);
        contentPane = new JPanel();
        contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
        setContentPane(contentPane);
        GridBagLayout gbl_contentPane = new GridBagLayout();
        gbl_contentPane.columnWidths = new int[]{0, 0, 0, 0, 0, 0, 0, 0};
        gbl_contentPane.rowHeights = new int[]{0, 0, 0, 0, 0};
        gbl_contentPane.columnWeights = new double[]{0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 1.0, Double.MIN_VALUE};
        gbl_contentPane.rowWeights = new double[]{0.0, 0.0, 0.0, 1.0, Double.MIN_VALUE};
        contentPane.setLayout(gbl_contentPane);

        JLabel piano = new JLabel("");
        piano.setLabelFor(piano);
        piano.setIcon(new ImageIcon("C:\\Users\\Donald\\workspace2\\Keyboard450x220.png"));
        GridBagConstraints gbc_piano = new GridBagConstraints();
        gbc_piano.insets = new Insets(0, 0, 5, 5);
        gbc_piano.fill = GridBagConstraints.BOTH;
        gbc_piano.gridx = 2;
        gbc_piano.gridy = 1;
        contentPane.add(piano, gbc_piano);

        ////////////////////////////////////////////////
        int[] xs1 = new int[] { 0,45,45,0,0 };
        int[] ys1 = new int[] { 0,0,220,220,0 };
        Polygon p1 = new Polygon(xs1, ys1, xs1.length);
        String n1 = "C3";

        int[] xs2 = new int[] { 45,90,90,45,45 };
        int[] ys2 = new int[] { 0,0,220,220,0 };
        Polygon p2 = new Polygon(xs2, ys2, xs2.length);
        String n2 = "D3";

        int[] xs3 = new int[] { 90,135,135,90,90 };
        int[] ys3 = new int[] { 0,0,220,220,0 };
        Polygon p3 = new Polygon(xs3, ys3, xs3.length);
        String n3 = "D3";

        PianoButton pb1 = new PianoButton(p1, n1);
        piano.add(pb1);

        PianoButton pb2 = new PianoButton(p2, n2);
        piano.add(pb2);

        PianoButton pb3 = new PianoButton(p3, n3);
        piano.add(pb3);
    }
}


class PianoButton extends JComponent implements MouseListener {

    Polygon key;
    String noteName;
    Color color;

    public PianoButton(Polygon p, String nn) {
        key = p;
        noteName = nn;
        color = Color.white;

        setBounds(key.getBounds());
        addMouseListener(this);
    }

    @Override
    public void mouseClicked(MouseEvent arg0) {}

    @Override
    public void mouseEntered(MouseEvent arg0) {
        hilite();
    }

    @Override
    public void mouseExited(MouseEvent arg0) {
        unhilite();
    }

    @Override
    public void mousePressed(MouseEvent arg0) {}

    @Override
    public void mouseReleased(MouseEvent arg0) {
        turnRed();
        repaint();
    }

    void hilite() {
        System.out.println("Turned yellow");
        color = Color.yellow;
        repaint();
    }

    void turnRed() {
        System.out.println("Turned Red");
        color = Color.red;
    }

    void unhilite() {
        System.out.println("Turned white");
        color = Color.white;
    }

    public void paintComponent(Graphics g){
        Graphics2D g2 = (Graphics2D) g;
        super.paintComponent(g2);
        g2.setColor(color);
        g2.fillPolygon(key);
        g2.drawPolygon(key);
    }
}

1 个答案:

答案 0 :(得分:2)

请勿使用setBounds。问题似乎是代码将三个组件添加到 标签 。将它们添加到面板中。