【问题标题】:No Action Performed when clicking on jbutton单击 jbutton 时未执行任何操作
【发布时间】:2015-12-04 04:31:17
【问题描述】:

我正在创建一个 Swing 应用程序。我创建了一个名为Drone 的类,并在另一个名为UserAccount 的类中创建了它的对象。在JButton,我正在使用userAccount 调用Drone 类的getter,但没有执行任何操作。为什么会这样?

这是Drone类的代码

private int x;
private int y;
private String text;
private int a;
private int b;
private String text1;
private int c;
private int d;
private String text2;

public Drone() {
x= 10;
y=150;
a=10;
b=150;
c=1000;
d=150;
text="Drone";
text1="Factory";
text2="Hospital";
    setSize(1070,300);
}

@Override
public void paint(Graphics g) {
    g.setColor(Color.white);
    g.fillRect(0, 0, 1070, 300);
    g.setColor(Color.BLACK);
    g.drawString(text, x, y);
    System.out.println(x + "" + y);

    g.drawString(text1, a, b);

    g.drawString(text2, c, d);

}

public void factory() throws InterruptedException{
    while(true){
        while(a<=getWidth()){
            b=getHeight()/2;
            repaint();
            Thread.sleep(1000000);
        }
    }
}

public void Hospital() throws InterruptedException{
    while(true){
        while(c<=getWidth()){
            d=getHeight()/2;
            repaint();
            Thread.sleep(1000000);
        }
    }
}

public void start() throws InterruptedException{
    while(true){
        while(x <= getWidth()-50){
            x++;
            y = getHeight()/2;
            repaint();
            Thread.sleep(150);
        }
        while(x >= 0){
            x--;
            y = getHeight()/2;
            repaint();
            Thread.sleep(150);
        }
        break;
    }
}

public static void main(String[] args) throws InterruptedException{
    JFrame frame = new JFrame("Drone Movement");
    frame.setDefaultCloseOperation(JFrame.HIDE_ON_CLOSE);
    Drone drone = new Drone();
    frame.getContentPane().add(drone);
    frame.setSize(1070,300);
    frame.setLocation(180,200);
    frame.setVisible(true);
    drone.start();
    drone.factory();
    drone.Hospital();
}

我在JButton 中的actionPerformed 呼叫这个课程,在JPanel 中出现

private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {                                         
    userAccount.getDrone();

}

为什么这里没有执行任何操作?

【问题讨论】:

  • 什么动作?什么按钮?没有任何代码为您的问题提供任何上下文。考虑提供一个runnable example 来证明您的问题。这不是代码转储,而是您正在做的事情的一个示例,它突出了您遇到的问题。这将导致更少的混乱和更好的响应
  • 假设 Drone 在基于 Swing 的组件中,那么您真的不应该覆盖 paint,而是更喜欢 paintComponent。您还应该调用 super 绘制方法以维护现有的绘制链合约
  • 我们可能永远不会知道...

标签: java swing class object jbutton


【解决方案1】:

我认为您缺少添加动作侦听器。

在 Drone 类中,创建的 JButton 应该有类似的东西

JButton button = new JButton("button");
button.addActionListener(this);

Drone 实现 ActionListener 的地方。

但是根据您所说的,JButton 具有 actionPerformed,我假设您正在扩展 JButton 以实现 ActionListener 并实现 actionPerformed 方法,那么代码应该是

public class MyButton extends JButton implements ActionListener {
    public void actionPerformed(ActionEvent actionEvent) {
        //TODO implement your action here
    }

那么在你的无人机中,你应该有类似的东西

MyButton button = new MyButton();
button.addActionListener(button); // the button itself instead of the this

【讨论】:

  • 根据我们仅有的 OP 代码,我想说他们已经使用 Netbeans 表单设计器设置了 UI...
猜你喜欢
  • 2012-04-16
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-03-23
  • 1970-01-01
  • 1970-01-01
  • 2021-09-15
  • 1970-01-01
相关资源
最近更新 更多