Good day. Stupid question, but I can not solve. There is a small such code. When you click the rectangle should change the coordinates, but this does not happen. What should be done?
import javax.swing.*; import java.awt.*; import java.awt.event.*; public class Gui implements ActionListener { int x = 50; int y = 200; JButton button; MyDrawPanel myDrawPanel; JFrame frame; public static void main (String[] args) { Gui gui = new Gui(); gui.go(); } public void go() { frame = new JFrame(); myDrawPanel = new MyDrawPanel(); button = new JButton("Test"); frame.setSize(1500, 800); frame.setVisible(true); frame.setResizable(false); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.getContentPane().add(BorderLayout.CENTER, myDrawPanel); frame.getContentPane().add(BorderLayout.SOUTH, button); button.addActionListener(this); } public void actionPerformed(ActionEvent event) { x++; y++; } class MyDrawPanel extends JPanel { public void paintComponent(Graphics g) { g.setColor(Color.gray); g.fillRect(x, y, 50, 200); } } }
должен менять координаты- why are you so bogged down? You only increase the values of the variables. Are these variables related to this rectangle? - post_zeewg.fillRect(x, y, 50, 200);methodg.fillRect(x, y, 50, 200);they set the position of the rectangle. - Kojer DeforMyDrawPanel.setLocation(x, y);to theactionPerformed(...)method after the increment of variablesMyDrawPanel.setLocation(x, y);. - post_zeew