新手swing按钮点击没反应?

liaoyf 发布于 2016/05/12 09:26
阅读 484
收藏 0
package count;

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

/**
 * Created by Administrator on 2016/5/11 0011.
 */
public class CountDemo {
    public static void main(String[] args){
        EventQueue.invokeLater(new Runnable() {
            @Override
            public void run() {
                CountFrame countFrame = new CountFrame();
                countFrame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
                countFrame.setVisible(true);
            }
        });
    }
}

class CountFrame extends JFrame{
    private JButton display;
    private JPanel jPanel;

    CountFrame(){
        setTitle("计算器");

        //add the display
        display = new JButton("0");
        display.setEnabled(false);
        add(display, BorderLayout.NORTH);

        jPanel = new JPanel();
        jPanel.setLayout(new GridLayout(4, 4));

        addButton(new JButton("7"), "insert");
        addButton(new JButton("8"), "insert");
        addButton(new JButton("9"), "insert");
        addButton(new JButton("/"), "command");

        addButton(new JButton("4"), "insert");
        addButton(new JButton("5"), "insert");
        addButton(new JButton("6"), "insert");
        addButton(new JButton("*"), "command");

        addButton(new JButton("1"), "insert");
        addButton(new JButton("2"), "insert");
        addButton(new JButton("3"), "insert");
        addButton(new JButton("-"), "command");

        addButton(new JButton("0"), "insert");
        addButton(new JButton("."), "insert");
        addButton(new JButton("="), "command");
        addButton(new JButton("+"), "command");

        add(jPanel, BorderLayout.CENTER);

        pack();
    }

    private void addButton(final JButton jButton, String type){
        ActionListener actionListener;
        final String buttonText = jButton.getText();

        if(type.equals("insert")){
            actionListener = new ActionListener() {
                @Override
                public void actionPerformed(ActionEvent e) {
                    String input = e.getActionCommand();
                    System.out.println(input);
                    display.setText(display.getText() + input);
                }
            };
            jPanel.add(jButton, actionListener);
        }else{
            jPanel.add(jButton);
            if(buttonText.equals("/")){

            }
        }
    }
}



这个是计算器,为什么我点按钮不会设置显示的值呢?哪里写错了?
加载中
0
ccc_clk
ccc_clk
class CountFrame extends JFrame{
    private JButton display;
    private JPanel jPanel;
 
    CountFrame(){
        setTitle("计算器");
 
        //add the display
        display = new JButton("0");
        display.setEnabled(false);
        add(display, BorderLayout.NORTH);
 
        jPanel = new JPanel();
        jPanel.setLayout(new GridLayout(4, 4));
 
        addButton(new JButton("7"), "insert");
        addButton(new JButton("8"), "insert");
        addButton(new JButton("9"), "insert");
        addButton(new JButton("/"), "command");
 
        addButton(new JButton("4"), "insert");
        addButton(new JButton("5"), "insert");
        addButton(new JButton("6"), "insert");
        addButton(new JButton("*"), "command");
 
        addButton(new JButton("1"), "insert");
        addButton(new JButton("2"), "insert");
        addButton(new JButton("3"), "insert");
        addButton(new JButton("-"), "command");
 
        addButton(new JButton("0"), "insert");
        addButton(new JButton("."), "insert");
        addButton(new JButton("="), "command");
        addButton(new JButton("+"), "command");
        add(jPanel, BorderLayout.CENTER);
 
        pack();
    }
 
    private void addButton( JButton jButton, String type){
        ActionListener actionListener;
        final String buttonText = jButton.getText();
 
        if(type.equals("insert")){
            actionListener = new ActionListener() {
                @Override
                public void actionPerformed(ActionEvent e) {
                    String input = e.getActionCommand();
                    System.out.println(input);
                    display.setText(display.getText() + input);
                }
            };
            jButton.addActionListener(actionListener);
            jPanel.add(jButton);
        }else{
            jPanel.add(jButton);
            if(buttonText.equals("/")){
 
            }
        }
    }
}



0
ccc_clk
ccc_clk

jPanel.add(jButton, actionListener);

这个我想不出你是要干嘛的?

监听的是按钮,不是Jpanel

返回顶部
顶部