【问题标题】:Button won't do anything Java按钮不会做任何事情 Java
【发布时间】:2013-06-10 22:49:02
【问题描述】:

我已经查看了我的代码一段时间,但找不到我的按钮不起作用的原因。单击时我的“startButton”可以工作,但我的“helpButton”不会。当我单击按钮时,它不会运行 println 代码。任何帮助将不胜感激。

public class test extends JFrame{

    private JLabel label;
    private JButton button;

    private ImageIcon bgi;
    private JLabel bgl;

    public static Rectangle gameSquare;


    private JButton startButton;
    private JButton helpButton;
    private final Action action = new SwingAction();


    public static void main(String[] args) throws MalformedURLException, IOException {
        test gui = new test ();
        gui.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); // when click x close program
        gui.setSize(902, 305);
        gui.setVisible(true);
        gui.setTitle("Solid Cloud Inc - Twitter Unfolower");
    }

    public test() throws MalformedURLException, IOException{

        bgi = new ImageIcon(getClass().getResource("tu.png"));
        getContentPane().setLayout(null);

        BufferedImage img = ImageIO.read(new URL("http://i1344.photobucket.com/albums/p656/SolidCloudInc/start_zpsf3781681.png"));
        //ImageIcon start = new ImageIcon(getClass().getResource("start.png"));
        startButton = new JButton("");
        startButton.setIcon(new ImageIcon(img));
        startButton.setBounds(22, 186, 114, 50);


        getContentPane().add(startButton);

        BufferedImage img2 = ImageIO.read(new URL("http://i1344.photobucket.com/albums/p656/SolidCloudInc/help_zpsc4fad867.png"));
        final JButton helpButton = new JButton("");
        helpButton.setIcon(new ImageIcon(img2));
        helpButton.setBounds(192, 186, 114, 50);

        getContentPane().add(helpButton);

        bgl = new JLabel (bgi);
        bgl.setBounds(0, 0, 886, 272);
        getContentPane().add(bgl);

        Events e = new Events();
        startButton.addActionListener(e);
        helpButton.addActionListener(e);
    }

    public class Events implements ActionListener {


        public void actionPerformed(ActionEvent e) {

            if (e.getSource() == startButton) {
               label.setText("Searching");

               try {
                Unfollow();
            } catch (InterruptedException e1) {
                // TODO Auto-generated catch block
                e1.printStackTrace();
            }
            }
            else if (e.getSource() == helpButton){
                System.out.println("gottem");
            }
    }

    }

【问题讨论】:

  • 如果你这样做startButton.addActionListener(new Events());helpButton.addActionListener(new Events());是否有效?

标签: java swing user-interface action jbutton


【解决方案1】:

您在test 构造函数中隐藏了JButton 变量helpButton,从而与ActionListener 中的null 引用进行比较。替换

JButton helpButton = new JButton("");

helpButton = new JButton("");

另外:Java 命名约定表明类以大写 字母开头,例如Test

【讨论】:

  • 是的,就是这样!谢谢!
  • @LukeWorthing:如果他的回答对您有帮助,请投票并接受答案。
猜你喜欢
  • 2017-12-31
  • 2014-10-07
  • 2020-07-14
  • 2016-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-07-13
相关资源
最近更新 更多