【问题标题】:I am having trouble implementing ActionListener, into a swing based GUI我在将 ActionListener 实现到基于摇摆的 GUI 中时遇到问题
【发布时间】:2021-05-10 17:28:53
【问题描述】:

我希望能够检测我的按钮何时被点击,命名按钮如此方便,但我无法让它成为这种方式。这是一些代码

import java.awt.GridLayout;
import java.awt.event;
import javax.swing.BorderFactory;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JButton;

public class Main implements ActionListener
//im having immense problems implementing the action listener. 
{
  public static void main(String[] args) 
  {
    new GUI(); // calling in main.
    System.out.print("test for bad wifi because my wifi hates me"); // I'm using a cloud based ide
  }

  JFrame frame1 = new JFrame();
  JPanel panel1 = new JPanel();
  JFrame frame2 = new JFrame(); //not in use yet
  JPanel panel2 = new JPanel(); //""

  public void GUI()
  {
    JButton button = new JButton("moment"); 
    button.addActionListener(this);

    panel1.setBorder(BorderFactory.createEmptyBorder( 30, 30, 30, 30 )); 
    panel1.setLayout(new GridLayout(0, 1));
    panel1.add(button); 

    frame1.add(panel1, BorderLayout.CENTER); // frame is on the pannel or vice versa
    frame1.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); // funny close
    frame1.setTitle("Final.");
    frame1.pack();
    frame1.setVisible(true);
  }

  @Override
  public void actionPerformed(ActionEvent e)
  {
    
  }
}

感谢任何帮助

【问题讨论】:

  • 欢迎来到 Stack Overflow。请通过tour 了解 Stack Overflow 的工作原理,并阅读How to Ask 以了解如何提高问题的质量。然后查看help center,看看你可以问什么问题。请参阅:Why is “Can someone help me?” not an actual question?.
  • 根据您的代码,该语句应该是 button.addActionListener(this); 如果您的 ActionListener 是一个单独的类,那么语句应该是 button.addActionListener (新的SeparateListener());
  • @GilbertLeBlanc 这个我已经试过了,没有效果,我又试了一次,你推荐了这个,可惜还是没有解决问题
  • Main.actionPerformed() 为空。你希望从一个空方法中做什么?
  • I simply am trying to get the button to work currently 我不明白你。因此,请告诉我们目前哪些工作无效:您期望什么以及您得到什么。收到任何错误消息?

标签: java swing actionlistener


【解决方案1】:

下载并使用 IDE。

Oracle 有一个有用的教程,Creating a GUI With JFC/Swing。跳过 Netbeans 部分。

JFrame 方法必须按特定顺序调用。这是我用于所有 Swing 应用程序的顺序。

这是我最终得到的完整的可运行代码。

import java.awt.BorderLayout;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.BorderFactory;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;

public class SimpleJButtonExample implements ActionListener {
    public static void main(String[] args) {
        SwingUtilities.invokeLater(new Runnable() {
            @Override
            public void run() {
                new SimpleJButtonExample().createAndShowGUI(); // calling in main.
            }
        });
        
        // I'm using a cloud based ide
        System.out.println("test for bad wifi because my wifi hates me"); 
    }

    JFrame frame1 = new JFrame();
    JPanel panel1 = new JPanel();
    JFrame frame2 = new JFrame(); // not in use yet
    JPanel panel2 = new JPanel(); // ""

    public void createAndShowGUI() {
        JButton button = new JButton("moment");
        button.addActionListener(this);

        panel1.setBorder(BorderFactory.createEmptyBorder(30, 30, 30, 30));
        panel1.setLayout(new GridLayout(0, 1));
        panel1.add(button);

        frame1.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); // funny close
        frame1.setTitle("Final");
        frame1.add(panel1, BorderLayout.CENTER); // panel is within the frame
        frame1.pack();
        frame1.setVisible(true);
    }

    @Override
    public void actionPerformed(ActionEvent e) {
        System.out.println("Button clicked");
    }

}

【讨论】:

    猜你喜欢
    • 2016-05-24
    • 2013-05-02
    • 1970-01-01
    • 2012-10-11
    • 2012-10-29
    • 1970-01-01
    • 2023-03-31
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多