gpt4 book ai didi

同一类的 java Graphics2d 实例的行为不同

转载 作者:行者123 更新时间:2023-12-02 07:55:13 25 4
gpt4 key购买 nike

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

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

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);
}
}

最佳答案

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

关于同一类的 java Graphics2d 实例的行为不同,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9733899/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com