0

グラスペインでブール値を変更することに頭を悩ませようとしています:

public class Frame extends JFrame{
    setResizable(false);
    setVisible(true);
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}

public static void main(String[] args) {
    Frame f = new Frame();
    MyGlassPane mgp = new MyGlassPane();
    f.setGlassPane(mgp);
    mgp.setVisible(true);
    mgp.setOpaque(false);
            Store s = new Store();
            f.add(s);
            f.pack();
}

public class MyGlassPane extends JPanel{
Boolean show;

    @Override
public void paintComponent(Graphics g) {
    super.paintComponent(g);
    g.setColor(Color.black);
    if(show){
    g.fillRect(50, 50, 50, 50);
    }
}

public class Store extends JPanel{

public Store(){
    setLayout(null);
    But jb1 = new But();
        add(jb1);

    setBackground(Color.DARK_GRAY);

}

}

public class But extends JButton implements MouseListener, MouseMotionListener {
public But(){
        addMouseListener(this);
    addMouseMotionListener(this);

}
@Override
public void mouseClicked(MouseEvent e) {
//Here I should be able to "show = true", but can't figure out how? 
}

どんな助けでも大歓迎です。

public void setShow(Boolean x){ show = x}; を作成してみました。MyGlassPane クラスで、しかしそれを動作させることができませんでした。インスタンス化された JPanel のブール値の値を変更して、長方形を描画するにはどうすればよいですか (これは、「otherClass」に追加されたボタンをクリックすると発生するはずです)。

4

1 に答える 1