简体   繁体   中英

The actionperformed don't get the signals from jbutton

I'm trying to make a code to move an oval so I set the oval inside a transparent JPanel (its going to be a red JPanel on a red background) and using actionperformed to move the JPanel. After I will make the JButton work I intend to add keybinders. why don't the actionperformed method get the signals from the JBUtton?

public class PanelExample_Extended{

public static final int OVAL_WIDTH = 20, OVAL_HEIGHT = 20;
public static int x1 = 50, y1 = 100;
JButton upButton;
JPanel transparentPanel;

public class MyGraphics extends JComponent {



    private static final long serialVersionUID = 7526472295622776147L;

    MyGraphics() {
        setPreferredSize(new Dimension(20,20));
    }

    public void paintComponent(Graphics g){
        super.paintComponents(g);
        g.setColor(Color.blue);
        g.fillOval(0, 0, OVAL_WIDTH, OVAL_HEIGHT);
    }

}

 public JPanel createContentPane (){

    JPanel totalGUI = new JPanel();
    totalGUI.setLayout(null);

    transparentPanel = new JPanel(new BorderLayout());
    transparentPanel.setBackground(Color.red);
    transparentPanel.setLocation(x1, y1);
    transparentPanel.setSize(20,20);
    MyGraphics tr = new MyGraphics();
    tr.setLocation(0, 0);
    transparentPanel.add(tr);
    totalGUI.add(transparentPanel);

    upButton = new JButton("up");
    upButton.setLocation(0,50);
    upButton.setSize(50,50);
    totalGUI.add(upButton);


    totalGUI.setOpaque(true);
    return totalGUI;
}

private static void createAndShowGUI() {

    JFrame.setDefaultLookAndFeelDecorated(true);
    JFrame frame = new JFrame("[=] ??? [=]");


    PanelExample_Extended demo = new PanelExample_Extended();
    frame.setContentPane(demo.createContentPane());

    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.setSize(290, 100);
    frame.setVisible(true);
}

public void ActionPerformed(ActionEvent h){
    if( h.getSource() == upButton) {
        y1 = y1  - 10;
        transparentPanel.setLocation(x1, y1);
    }

}

public static void main(String[] args) {
    SwingUtilities.invokeLater(new Runnable() {
        public void run() {
            createAndShowGUI();
        }
    });
}
}

There's no call to addActionListener(...) anywhere. No button will work unless you first "hook it up" with a listener, and that is your responsibility as the coder.

Solution: call addActionListener(...) on your JButton and pass in the appropriate listener. This is all well described in the JButton tutorial (link now added), and if you are serious about learning Swing, I suggest you not only look at it, but study it.


Edit:

  • Also your code has no ActionListener either! You really should read the tutorial at the link I've provided.
  • And as @Radiodef points out, you have capitalized actionPerformed wrong. Be sure to precede all overridden methods with the @Override annotation to have the compiler check that you are doing it correctly, that your method "signature" is right.
  • Also, as camickr points out, x1 and y1 should not be static. You should give the class that holds them public setter methods, setX1(int x1) and setY1(int y1) and have classes that need to set these fields call these methods.
  • Also, when moving components, be sure to call revalidate() and repaint() on the container that holds them so that they are repositioned and redrawn.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM