JAVA中实现计算加法的功能
做了一个窗口如下:我是想让这个第一个里面输入一个数第二个里面输入一个.按等号显示在第三个里面.怎么弄.我写的代码如下.请指点如何使输入数保存到变量中.如何去计算.impo...
做了一个窗口如下:我是想让这个 第一个里面输入一个数 第二个里面输入一个. 按等号显示在第三个里面.怎么弄. 我写的代码如下. 请指点如何使输入数保存到变量中. 如何去计算.import javax.swing.*;import java.awt.*;
import javax.swing.JFrame;
public class Window {
static int w = 300;
static int h = 200;
static int wbk1 = 10;
static int wbk2 = 10;
static int wbk3 = 10;
public static void main(String args[]){
JFrame jf = new JFrame("这是一个窗口");
jf.setSize(w,h);
jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
jf.setVisible(true);
JButton a1 = new JButton("=");
JTextField t1 = new JTextField(wbk1);
JTextField t2 = new JTextField(wbk2);
JTextField t3 = new JTextField(wbk3);
JLabel label1 = new JLabel("+");
JPanel pane = new JPanel();
jf.setContentPane(pane);
pane.add(t1);
pane.add(label1);
pane.add(t2);
pane.add(a1);
pane.add(t3);
jf.pack();
}
} 展开
import javax.swing.JFrame;
public class Window {
static int w = 300;
static int h = 200;
static int wbk1 = 10;
static int wbk2 = 10;
static int wbk3 = 10;
public static void main(String args[]){
JFrame jf = new JFrame("这是一个窗口");
jf.setSize(w,h);
jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
jf.setVisible(true);
JButton a1 = new JButton("=");
JTextField t1 = new JTextField(wbk1);
JTextField t2 = new JTextField(wbk2);
JTextField t3 = new JTextField(wbk3);
JLabel label1 = new JLabel("+");
JPanel pane = new JPanel();
jf.setContentPane(pane);
pane.add(t1);
pane.add(label1);
pane.add(t2);
pane.add(a1);
pane.add(t3);
jf.pack();
}
} 展开
1个回答
2013-10-16
展开全部
你需要看一下swing事件的编写,你需要调用文本框的getText方法得到里面的数值,然后为“=”按钮写监听事件,调用第三个文本框的setText方法显示结果。我这里有个跟你界面相同的完善版,包括加减乘除运算的,我在代码中用加粗着重突出了事件的那部分。import javax.swing.*;
import java.awt.*;
import java.awt.event.*;public class AA extends JFrame
{
protected JTextField tFirst,tSecond,tResult,tEqual;
private JComboBox cFu;
private JButton jOk;
private String sFuHao;
protected String sFirst;
float fFirst,fSecond,fResult;
private String cFuHao[] = {
"+","-","*","/"
};
public AA()
{
super( "Calculator" );
Container container = getContentPane();
container.setLayout( new FlowLayout() );
tFirst = new JTextField( 10 );
container.add( tFirst );
cFu = new JComboBox( cFuHao );
cFu.setMaximumRowCount( 3 );
container.add( cFu );
tSecond = new JTextField( 10 );
container.add( tSecond );
tEqual = new JTextField( "=", 1 );
tEqual.setEditable( false );
container.add( tEqual );
tResult = new JTextField( 10 );
tResult.setEditable( false );
container.add( tResult );
jOk = new JButton( "calculate" );
container.add( jOk );
ButtonHandler handler = new ButtonHandler();
jOk.addActionListener( handler );
setSize( 600,80 );
setVisible( true );
}
public static void main( String args[] )
{
AA application = new AA();
application.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );
}
private class ButtonHandler implements ActionListener{
public void actionPerformed( ActionEvent event )
{
sFuHao=(String)cFu.getSelectedItem();
if((tFirst.getText()).equals("#")||(tSecond.getText()).equals("#"))
System.exit(0);
fFirst = Float.parseFloat(tFirst.getText());
fSecond = Float.parseFloat(tSecond.getText());
if(sFuHao.equals("+"))
fResult = fFirst + fSecond;
else if(sFuHao.equals("-"))
fResult = fFirst - fSecond;
else if(sFuHao.equals("*"))
fResult = fFirst * fSecond;
else
fResult = fFirst / fSecond;
tResult.setText( String.valueOf(fResult) );
}
}
}
import java.awt.*;
import java.awt.event.*;public class AA extends JFrame
{
protected JTextField tFirst,tSecond,tResult,tEqual;
private JComboBox cFu;
private JButton jOk;
private String sFuHao;
protected String sFirst;
float fFirst,fSecond,fResult;
private String cFuHao[] = {
"+","-","*","/"
};
public AA()
{
super( "Calculator" );
Container container = getContentPane();
container.setLayout( new FlowLayout() );
tFirst = new JTextField( 10 );
container.add( tFirst );
cFu = new JComboBox( cFuHao );
cFu.setMaximumRowCount( 3 );
container.add( cFu );
tSecond = new JTextField( 10 );
container.add( tSecond );
tEqual = new JTextField( "=", 1 );
tEqual.setEditable( false );
container.add( tEqual );
tResult = new JTextField( 10 );
tResult.setEditable( false );
container.add( tResult );
jOk = new JButton( "calculate" );
container.add( jOk );
ButtonHandler handler = new ButtonHandler();
jOk.addActionListener( handler );
setSize( 600,80 );
setVisible( true );
}
public static void main( String args[] )
{
AA application = new AA();
application.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );
}
private class ButtonHandler implements ActionListener{
public void actionPerformed( ActionEvent event )
{
sFuHao=(String)cFu.getSelectedItem();
if((tFirst.getText()).equals("#")||(tSecond.getText()).equals("#"))
System.exit(0);
fFirst = Float.parseFloat(tFirst.getText());
fSecond = Float.parseFloat(tSecond.getText());
if(sFuHao.equals("+"))
fResult = fFirst + fSecond;
else if(sFuHao.equals("-"))
fResult = fFirst - fSecond;
else if(sFuHao.equals("*"))
fResult = fFirst * fSecond;
else
fResult = fFirst / fSecond;
tResult.setText( String.valueOf(fResult) );
}
}
}
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询