私はここで初心者です。私はJLabelのテキストを移動マウスの位置のテキストに変更したいコードをここから取得しています。ここに私のコードです。マウス - モーションリスナーが動作しない
import javax.swing.*;
import java.awt.*;
import java.awt.event.MouseEvent;
import java.awt.event.MouseMotionListener;
public class Draw extends JFrame{
int x;
int y;
String positions = "Positions: " + x + ", " + y;
JLabel positionsOnFrame = new JLabel(positions);
public class AL implements MouseMotionListener {
public void mouseMoved(MouseEvent e) {
x = e.getX();
y = e.getY();
}
public void mouseDragged(MouseEvent e) {
positions += " dragged.";
}
}
//Constructor
public Draw() {
setTitle("Title");
setBackground(Color.BLACK);
setSize(300, 300);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setLocationRelativeTo(null);
addMouseMotionListener(new AL());
add(positionsOnFrame);
setVisible(true);
}
public static void main(String[] args) {
new Draw();
}
}
サイドの問題:コンストラクタから黒色に設定されていても、JFrameは黒くならない。
マウスの問題に対する解決策はありますか?何も起こりません!値は0に設定されています! (私はそれらを初期化していない、彼らは自動的に0に設定されています!)。 ヘルプは間違いなく感謝します!
色の問題については、次のようにします。getContentPane()。setBackground(Color.BLACK); – isaace
私に解決策を教えていただきありがとうございますが、バックグラウンドを設定するためにgetContentPane()を呼び出さなければならないのはなぜですか?前もって感謝します! –