I have JFramewith 2 JPanelin it: a PaintPanel(with method paint()) and ButtonPanel(with buttons). When I call repaint() PaintPanel(but by pressing a button), the button ButtonPanelsigns in PaintPanel! It is not available for clicks or anything else, it is just there.
I tried to recreate the problem with this code:
public class Main {
public static void main(String[] args) {
JFrame frame = new JFrame("frame");
frame.setSize(400,400);
frame.setLayout(new GridLayout(2,1));
PaintPanel paint = new PaintPanel();
ButtonPanel buttons = new ButtonPanel(paint);
frame.add(paint);
frame.add(buttons);
frame.setVisible(true);
}
}
public class PaintPanel extends JPanel{
public void paint(Graphics g){
g.drawRect(10, 10, 10, 10);
}
}
public class ButtonPanel extends JPanel implements ActionListener{
private PaintPanel paintPanel;
public ButtonPanel(PaintPanel paintPanel){
this.paintPanel=paintPanel;
JButton button = new JButton("button");
button.addActionListener(this);
add(button);
}
@Override
public void actionPerformed(ActionEvent arg0) {
paintPanel.repaint();
}
}
This may recreate the problem that I have (sorry for the odd code markings, it doesn't seem to work out correctly).
I really hope that one of you knows what is happening here, because I don’t ...
source
share