首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > JAVA > J2SE开发 >

Java编程简单有关问题

2011-12-02 
Java编程简单问题?我想做一个简单计算器的界面,下面源代码:已经能通过编译但是不能执行,那位高手有空时帮

Java编程简单问题?
我想做一个简单计算器的界面,下面源代码:
已经能通过编译但是不能执行,那位高手有空时帮忙看一下,小弟在此先谢过了.

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

public   class   Graphics_Count2
{
public   static   void   main(String   args[])
{

new   FrameIO();
}
}


class   FrameIO   extends   JFrame  
{
JTextField   IOput;
JButton   btn0,btn1,btn2,btn3,btn4,btn5,btn6,btn7,btn8,btn9;
JButton   btnAdd,btnSub,btnMul,btnDiv,btnEqual,btnPoint;

FrameIO()
{
super( "简单计算 ");
IOput   =   new   JTextField(20);
btn0   =   new   JButton( "0 ");btn1   =   new   JButton( "1 ");
btn2   =   new   JButton( "2 ");btn3   =   new   JButton( "3 ");
btn4   =   new   JButton( "4 ");btn5   =   new   JButton( "5 ");
btn6   =   new   JButton( "6 ");                 btn7   =   new   JButton( "7 ");
btn8   =   new   JButton( "8 ");                 btn9   =   new   JButton( "9 ");
btnAdd   =   new   JButton( "+ ");             btnSub   =   new   JButton( "- ");
btnMul   =   new   JButton( "* ");             btnDiv   =   new   JButton( "/ ");

JPanel   p   =   new   JPanel();
p.setLayout(new   GridLayout(4,4));
p.add(btnAdd);     p.add(btn1);     p.add(btn2);             p.add(btn3);
p.add(btnSub);     p.add(btn4);     p.add(btn5);             p.add(btn6);
p.add(btnMul);     p.add(btn7);     p.add(btn8);             p.add(btn9);
p.add(btnDiv);     p.add(btn0);     p.add(btnPoint);     p.add(btnEqual);

Container   container   =   getContentPane();
container.setLayout(new   BorderLayout());
container.add(IOput,BorderLayout.NORTH);
container.add(p,BorderLayout.SOUTH);
container.add(new   JButton( "EXIT "),BorderLayout.EAST);

setSize(300,300);
setVisible(true);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

}


}




[解决办法]
/**
我这里有一个计算器的例子,愿能为你提供一些参考
*/

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

public class Calculator
{
public static void main(String[] args)
{
CalculatorFrame frame = new CalculatorFrame();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
}
}

/**
A frame with a calculator panel.
*/
class CalculatorFrame extends JFrame
{
public CalculatorFrame()
{
setTitle( "Calculator ");
CalculatorPanel panel = new CalculatorPanel();
add(panel);
pack();
}
}

/**
A panel with calculator buttons and a result display.
*/
class CalculatorPanel extends JPanel


{
public CalculatorPanel()
{
setLayout(new BorderLayout());

result = 0;
lastCommand = "= ";
start = true;

// add the display

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

ActionListener insert = new InsertAction();
ActionListener command = new CommandAction();

// add the buttons in a 4 x 4 grid

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

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

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

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

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

add(panel, BorderLayout.CENTER);
}

/**
Adds a button to the center panel.
@param label the button label
@param listener the button listener
*/
private void addButton(String label, ActionListener listener)
{
JButton button = new JButton(label);
button.addActionListener(listener);
panel.add(button);
}

/**
This action inserts the button action string to the
end of the display text.
*/
private class InsertAction implements ActionListener
{
public void actionPerformed(ActionEvent event)
{
String input = event.getActionCommand();
if (start)
{
display.setText( " ");
start = false;
}
display.setText(display.getText() + input);
}
}

/**
This action executes the command that the button
action string denotes.
*/
private class CommandAction implements ActionListener
{
public void actionPerformed(ActionEvent event)
{
String command = event.getActionCommand();

if (start)
{
if (command.equals( "- "))
{
display.setText(command);
start = false;
}
else
lastCommand = command;
}
else
{
calculate(Double.parseDouble(display.getText()));
lastCommand = command;
start = true;
}
}
}

/**
Carries out the pending calculation.
@param x the value to be accumulated with the prior result.
*/
public void calculate(double x)
{
if (lastCommand.equals( "+ ")) result += x;
else if (lastCommand.equals( "- ")) result -= x;
else if (lastCommand.equals( "* ")) result *= x;
else if (lastCommand.equals( "/ ")) result /= x;
else if (lastCommand.equals( "= ")) result = x;
display.setText( " " + result);
}


private JButton display;
private JPanel panel;
private double result;
private String lastCommand;
private boolean start;
}

热点排行