如何按按钮,控制台都不能输出
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;
} 展开
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;
} 展开
1个回答
展开全部
首先不好意思哈楼主,刚开始太粗心了。
仔细看了下应该是没有实例化的错误,还有我觉得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;
}
仔细看了下应该是没有实例化的错误,还有我觉得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;
}
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询