【问题标题】:Java Action Listener not workingJava 动作监听器不工作
【发布时间】:2012-01-29 11:21:56
【问题描述】:

我有两个问题:

1 .为什么当我执行代码时,当我单击按钮 P 时它不显示 println。 2. 我尝试使用 JLabel 制作背景,效果很好,但它并没有涵盖所有的 JFrame。我用 try and catch 尝试了 JFrame,但它没有显示它。

JPanel L1 = new JPanel();
JButton P = new JButton("Open");

    JButton P1 = new JButton("Cancel");

    Dimension D = new Dimension(80 , 30);

    Container C = getContentPane();

    JLabel Label2 = new JLabel(new 




    super.setTitle("Ismail Application");
    //Buttons

    //Button 1
    P.setToolTipText("Click To Open");
    P.setPreferredSize(D);

    //Button 2
    P1.addActionListener(this);

    P1.setToolTipText("Click to exit program");

    P1.setPreferredSize(D);

    //Adding Components

    L1.add(P, BorderLayout.WEST);

    L1.add(P1, BorderLayout.EAST);

    add(L1, BorderLayout.SOUTH);

    P1.addActionListener(this);

    P.addActionListener(this);


             //Labels

    Label2.setLayout(null);

    Label2.setSize(400,300);

    Label2.setToolTipText("This is the Background");

    add(Label2, BorderLayout.NORTH);


}
public void actionPerformed (ActionEvent e)
    {
        if(e.getSource() == P)
        {
            System.out.println("not working");
        }
        if(e.getSource() == P1){

        }

    }

希望大家帮忙

谢谢

【问题讨论】:

  • 您的代码不完整或无法编译。请至少提供一个工作示例。
  • @ismail zaidi : 请遵守 Java 命名约定,阅读代码片段时太难理解了。问候

标签: java swing jframe jlabel actionlistener


【解决方案1】:

由于您发布的代码无法编译并且我们无法运行,因此很难说发生了什么。请参阅下面的JButton 的最基本示例,并附有ActionListener,每次按下按钮时都会打印一些内容。将其与您的代码进行比较以找出差异,或将您的代码调整为 sscce

import javax.swing.JButton;
import javax.swing.JFrame;
import java.awt.EventQueue;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

public class FrameWithButtonExample {

  public static void main( String[] args ) {
    EventQueue.invokeLater( new Runnable() {
      @Override
      public void run() {
        JFrame frame = new JFrame( "TestFrame" );

        final JButton testButton = new JButton( "TestButton" );
        testButton.addActionListener( new ActionListener() {
          @Override
          public void actionPerformed( ActionEvent aActionEvent ) {
            //if check to match the code from the question, but not really needed
            if ( aActionEvent.getSource() == testButton ){
              System.out.println("TestButton pressed");
            }
          }
        } );
        frame.add( testButton );
        frame.pack();
        frame.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );
        frame.setVisible( true );
      }
    } );
  }
}

【讨论】:

    【解决方案2】:

    你没有显示你是否在类上实现了 ActionListener。也就是说,你最好这样做:

    P.setActionCommand("P_BUTTON");
    

    然后

    if (e.getActionCommand().equals("P_BUTTON"))
    

    比你做的方式。

    【讨论】:

    • 我已经实现了actionListener,我试过你的方法,还是不行:(.
    【解决方案3】:

    由于您使用的是布局,因此请停止向组件提供您的个人尺寸。将所有 setPreferredSize() 事物添加为 cmets。 最好将您的 JLabel 添加到中心而不是北,以便它可以填充组件上的最大区域。此外,提供完整的代码,以便我们也可以查看您的 ActionListener 内容。 对于 JLabel 部分,删除

    add(Label2, BorderLayout.NORTH);
    

    用这个替换它

    add(Label2, BorderLayout.CENTER);
    

    对于你的 ActionListener 部分,试试这个

    public void actionPerformed(ActionEvent ae)
    {
        JButton button = (JButton)ae.getSource();
        if (button.equals(P))
        {
            System.out.println("not working.");
        }
        else if (button.equals(P1))
        {
            System.out.println("working.");
        }
    }
    

    希望能有所帮助。

    问候

    【讨论】:

      猜你喜欢
      • 2012-07-31
      • 1970-01-01
      • 1970-01-01
      • 2013-10-23
      • 1970-01-01
      • 1970-01-01
      • 2021-07-02
      • 2013-07-12
      • 1970-01-01
      相关资源
      最近更新 更多