0
Javaに触発されているので、私はフォームに追加する最初のボタンで次のコードが機能し、同じクラスの他の2つのインスタンスでは機能しません。また、mouseExitハンドラは、まったく呼び出されないように見えるか、すべてのボタンを終了するときにのみ呼び出されます。手伝って頂けますか?同じクラスのjava graphics2dインスタンスが異なる振る舞いをします
セットアップは、単純な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);
}
}
ありがとうございます。私は境界を設定しなければ、まったく動作しないことがわかった。私はラベルを使っていました。これらは私が確信している初心者の間違いです。パネルが画像を保持していますか?それとも私がここで成し遂げようとしているものに代わるより優れた選択肢がありますか?私は基本的にピアノのキーボードの形のイメージマップに似たものを設定していますが、HTMLではなくjavaで設定しています。 – devdon