如何“激活”GUI上的按钮?

时间:2014-11-27 14:40:29

标签: java user-interface button

到现在为止,我已经知道我的代码存在缺陷。我只是想知道为什么它有缺陷。我想激活" webButton"因此,当它被点击时,它会在控制台上打印一条消息,显示"这会打开Mozilla Firefox。"

package smartphone;
import java.awt.*;
import java.awt.event.ActionEvent;
import javax.swing.*;
import java.util.Scanner;

public class Smartphone implements ActionListener {

public static void main(String[] args) {
{
{
JFrame container = new JFrame();

container.setLayout(new BorderLayout());

Scanner daniel = new Scanner(System.in);

JButton webButton = new JButton(new ImageIcon("Firefox.png"));
JButton phoButton = new JButton(new ImageIcon("Facebook.png"));
JButton texButton = new JButton(new ImageIcon("Phone.png"));
JButton setButton = new JButton(new ImageIcon("Settings.png"));
JButton smsButton = new JButton(new ImageIcon("sms.png"));


container.setTitle("Smartphone Interface!");
container.setSize(240,340);
container.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

container.add(setButton, BorderLayout.CENTER);
container.add(webButton, BorderLayout.SOUTH);
container.add(texButton, BorderLayout.NORTH);
container.add(phoButton, BorderLayout.EAST);
container.add(smsButton, BorderLayout.WEST);
container.setVisible(true);


    webButton.addActionListener(instanceofSmartphone);

    }

    }
    }
     }

3 个答案:

答案 0 :(得分:0)

这样做。

    webButton.addActionListener(new ActionListener() {

        public void actionPerformed(ActionEvent e)
        {
            //Execute when button is pressed
        }
    });

如果您想使用ActionListener接口,请在Frame类中实现它,然后用

替换该instanceOfSmartphone
webButton.addActionListener(this);

把它放在方法之外

public void actionPerformed(ActionEvent e) {
    if(e.getSource() == webButton) {
    }
}  

答案 1 :(得分:0)

您需要实现actionPerformed方法。下面是一个例子。

public void actionPerformed(ActionEvent e) {
    System.out.println("This method opens Mozilla Firefox.");
}

此外,您需要更改将动作侦听器添加到以下内容的方式。

webButton.addActionListener(this);

还有许多其他问题。这是您的代码的修改版本,以使其工作,但远非完美或最终您想要的。我强烈建议您按照以下网站顺序浏览所有教程。它没有比他们拥有的更容易。此外,如果您还没有使用它,您应该尝试使用Netbeans或其他IDE。它会为您提供在您开始时可能有所帮助的反馈。 https://docs.oracle.com/javase/tutorial/

package smartphone;

import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.*;
import java.util.Scanner;

public class Smartphone extends Frame implements ActionListener {
    static JButton webButton = new JButton(new ImageIcon("Firefox.png"));
    static JButton phoButton = new JButton(new ImageIcon("Facebook.png"));
    static JButton texButton = new JButton(new ImageIcon("Phone.png"));
    static JButton setButton = new JButton(new ImageIcon("Settings.png"));
    static JButton smsButton = new JButton(new ImageIcon("sms.png"));

    Smartphone(){
       webButton.addActionListener(this);       
    }

    public static void main(String[] args) {

        Smartphone container = new Smartphone();

        container.setLayout(new BorderLayout());

        Scanner daniel = new Scanner(System.in);

        container.setTitle("Smartphone Interface!");
        container.setSize(240, 340);

        container.add(setButton, BorderLayout.CENTER);
        container.add(webButton, BorderLayout.SOUTH);
        container.add(texButton, BorderLayout.NORTH);
        container.add(phoButton, BorderLayout.EAST);
        container.add(smsButton, BorderLayout.WEST);
        container.setVisible(true);
    }

    @Override
    public void actionPerformed(ActionEvent e) {
        System.out.println("This opens a Firefox Webbrowser.");
    }
}

答案 2 :(得分:0)

您需要在构造函数的INSIDE按钮中添加ActionListenerbuttonName.addActionListener(this);。 然后,您需要创建以下方法: public void actionPerformed(ActionEvent event) { Object control = event.getSource(); if (control == buttonName) { //Run code... } }