如何按按钮,控制台都不能输出

mportjava.awt.BorderLayout;importjava.awt.EventQueue;importjava.awt.GridLayout;import... mport java.awt.BorderLayout;

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

import javax.swing.AbstractAction;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;

public class Calculartor
{
public static void main(String[] args)
{
EventQueue.invokeLater(new Runnable()
{
public void run()
{
CalculartorFrame frame = new CalculartorFrame();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
}
});
}

}

class CalculartorFrame extends JFrame
{
public CalculartorFrame()
{
setSize(DEFAULT_WIDTH ,DEFAULT_HEIGHT);
setTitle("Calculartor");

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

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

button = new NewButton("9", command);
button = new NewButton("8", command);
button = new NewButton("7", command);
button = new NewButton("/", insert );

button = new NewButton("6", command);
button = new NewButton("5", command);
button = new NewButton("4", command);
button = new NewButton("*", insert);

button = new NewButton("3", command);
button = new NewButton("2", command);
button = new NewButton("1", command);
button = new NewButton("-", insert);

button = new NewButton("0", command);
button = new NewButton(".", insert);
button = new NewButton("=", insert);
button = new NewButton("+", insert);

add(panel, BorderLayout.CENTER);
pack();

}

private class NewButton extends JButton
{
public NewButton(String t, ActionListener action)
{
super(t);
addActionListener(action);
panel.add(NewButton.this);
}
}

private class commandAction extends AbstractAction
{

@Override
public void actionPerformed(ActionEvent event)
{
System.out.println("dfdf");
}

}

private class insertAction extends AbstractAction
{
public void actionPerformed(ActionEvent event)
{

}
}

private JPanel panel;
private NewButton button;
private JButton display;

private commandAction command;
private insertAction insert;
public static final int DEFAULT_WIDTH = 400;
public static final int DEFAULT_HEIGHT = 300;
}
展开
 我来答
liujiahi
2010-09-11 · TA获得超过1001个赞
知道小有建树答主
回答量:621
采纳率:66%
帮助的人:420万
展开全部
首先不好意思哈楼主,刚开始太粗心了。
仔细看了下应该是没有实例化的错误,还有我觉得cmmandAction和insertAction没必要再分了吧。
下面是我修改的代码,应该没问题,哎,我粗心了
import java.awt.BorderLayout;

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

import javax.swing.AbstractAction;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;

public class Calculartor
{
public static void main(String[] args)
{
EventQueue.invokeLater(new Runnable()
{
public void run()
{
CalculartorFrame frame = new CalculartorFrame();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
}
});
}

}

class CalculartorFrame extends JFrame
{
public CalculartorFrame()
{
setSize(DEFAULT_WIDTH ,DEFAULT_HEIGHT);
setTitle("Calculartor");

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

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

button = new NewButton("9", command);
button = new NewButton("8", command);
button = new NewButton("7", command);
button = new NewButton("/", command );

button = new NewButton("6", command);
button = new NewButton("5", command);
button = new NewButton("4", command);
button = new NewButton("*", command);

button = new NewButton("3", command);
button = new NewButton("2", command);
button = new NewButton("1", command);
button = new NewButton("-", command);

button = new NewButton("0", command);
button = new NewButton(".", command);
button = new NewButton("=", command);
button = new NewButton("+", command);

add(panel, BorderLayout.CENTER);
pack();

}

private class NewButton extends JButton
{
public NewButton(String t, ActionListener action)
{
super(t);
action = new commandAction();//注意这里
addActionListener(action);
panel.add(NewButton.this);
// requestFocusInWindow();
}
}

private class commandAction extends AbstractAction
{

public void actionPerformed(ActionEvent event)
{
System.out.println("dfdf");
}

}

/* private class insertAction extends AbstractAction
{
public void actionPerformed(ActionEvent event)
{
System.out.println("hello!");

}
}
*/
private JPanel panel;
private NewButton button;
private JButton display;

private commandAction command;
// private insertAction insert;
public static final int DEFAULT_WIDTH = 400;
public static final int DEFAULT_HEIGHT = 300;
}
推荐律师服务: 若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询

为你推荐:

下载百度知道APP,抢鲜体验
使用百度知道APP,立即抢鲜体验。你的手机镜头里或许有别人想知道的答案。
扫描二维码下载
×

类别

我们会通过消息、邮箱等方式尽快将举报结果通知您。

说明

0/200

提交
取消

辅 助

模 式