【问题标题】:The actionperformed don't get the signals from jbutton执行的操作没有从 jbutton 获得信号
【发布时间】:2013-10-26 20:42:14
【问题描述】:

我正在尝试编写代码来移动椭圆,因此我将椭圆设置在透明的 JPanel 内(它将是红色背景上的红色 JPanel)并使用 actionperformed 来移动 JPanel。在我让 JButton 工作之后,我打算添加键盘绑定器。为什么actionperformed方法没有从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();
        }
    });
}
}

【问题讨论】:

  • 你能精简你的代码,让它只包含一个JButton 实现吗?
  • 这段代码是从一个更大的代码中精简出来的,我试图让它只包含与问题相关的最少组件......
  • 但我明白你的意思。我会编辑它。
  • @עמוסרום,你甚至花时间阅读 cmets 吗?在你上次发帖时 (stackoverflow.com/q/19575849/131872) 我说过你不应该使用静态变量。为什么不听建议就发问题???我们为您提供避免当前和未来问题的建议。
  • @camickr:啊,x1 和 y1,我现在看到了。是的,它们不应该是静态的。

标签: java swing jpanel action jbutton


【解决方案1】:

在任何地方都没有对addActionListener(...) 的呼叫。除非您首先与听众“挂钩”,否则任何按钮都不会起作用,这是您作为编码员的责任。

解决方案:在您的 JButton 上调用 addActionListener(...) 并传入适当的侦听器。这在JButton tutorial 中都有很好的描述(现在添加了链接),如果您认真学习 Swing,我建议您不仅要看,还要学习。


编辑:

  • 您的代码也没有 ActionListener!你真的应该阅读我提供的链接上的教程。
  • 正如@Radiodef 指出的那样,您将 actionPerformed 大写错误。请务必在所有被覆盖的方法之前加上 @Override 注释,以让编译器检查您是否正确执行此操作,您的方法“签名”是否正确。
  • 另外,正如 camickr 所指出的,x1 和 y1 不应该是静态的。您应该为持有它们的公共 setter 方法 setX1(int x1)setY1(int y1) 提供类,并让需要设置这些字段的类调用这些方法。
  • 此外,在移动组件时,请务必在容纳它们的容器上调用 revalidate()repaint(),以便重新定位和重绘它们。

【讨论】:

  • 实际上,我确实从macs.hw.ac.uk/guidebook/?name=JButton&page=2 研究过它,我觉得它更容易阅读。我错过了这部分,但即使我实现了动作监听器并添加了 upButton.addActionListener(this);它不动...
  • ActionPerformed也需要改成actionPerformed
  • 伙计们,我真的在听你们所有的 cmets 和答案,我正在努力学习每一件事,并在发布之前用谷歌搜索每一个问题(这样,解决了我 99.99% 的问题)。那是我的第一个摇摆代码,我很难用它。非常感谢大家的帮助!
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-12-07
  • 2014-03-20
  • 1970-01-01
  • 2012-09-04
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多