当前位置: 代码迷 >> J2SE >> Java编程简单有关问题
  详细解决方案

Java编程简单有关问题

热度:148   发布时间:2016-04-24 15:59:43.0
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
  相关解决方案