/*
package 模拟四则运算;
import cn.itcast.uiutil;
/** *
@author 曹航 */ public class NewJFrame extends javax.swing.JFrame {
/**
Creates new form NewJFrame / public NewJFrame() { initComponents(); init(); } private void init(){ this.setTitle("模拟四则运算"); uiutil.setFrameImage(this); uiutil.setFrameCenter(this); } /*
This method is called from within the constructor to initialize the form.
WARNING: Do NOT modify this code. The content of this method is always
regenerated by the Form Editor.
*/
@SuppressWarnings("unchecked")
//
private void initComponents() {
jLabel1 = new javax.swing.JLabel(); jLabel2 = new javax.swing.JLabel(); jLabel3 = new javax.swing.JLabel(); SecondNumber = new javax.swing.JTextField(); FirstNumber = new javax.swing.JTextField(); Resurt = new javax.swing.JTextField(); jLabel4 = new javax.swing.JLabel(); SelectName = new javax.swing.JComboBox(); jButton1 = new javax.swing.JButton();
setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
jLabel1.setText("第一个操作数");
jLabel2.setText("结果");
jLabel3.setText("第二个操作数");
SecondNumber.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { SecondNumberActionPerformed(evt); } });
FirstNumber.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { FirstNumberActionPerformed(evt); } });
jLabel4.setText("=");
SelectName.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "+", "-", "*", "/" }));
jButton1.setText("计算"); jButton1.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { jButton1ActionPerformed(evt); } });
javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane()); getContentPane().setLayout(layout); layout.setHorizontalGroup( layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup() .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(layout.createSequentialGroup() .addGap(30, 30, 30) .addComponent(jLabel1) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 111, Short.MAX_VALUE) .addComponent(jLabel3)) .addGroup(layout.createSequentialGroup() .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE) .addComponent(SelectName, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE) .addGap(28, 28, 28) .addComponent(SecondNumber, javax.swing.GroupLayout.PREFERRED_SIZE, 79, javax.swing.GroupLayout.PREFERRED_SIZE))) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED) .addComponent(jLabel4) .addGap(18, 18, 18) .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addComponent(jButton1) .addComponent(Resurt, javax.swing.GroupLayout.PREFERRED_SIZE, 80, javax.swing.GroupLayout.PREFERRED_SIZE) .addGroup(layout.createSequentialGroup() .addGap(10, 10, 10) .addComponent(jLabel2))) .addGap(41, 41, 41)) .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(layout.createSequentialGroup() .addGap(40, 40, 40) .addComponent(FirstNumber, javax.swing.GroupLayout.PREFERRED_SIZE, 78, javax.swing.GroupLayout.PREFERRED_SIZE) .addContainerGap(358, Short.MAX_VALUE))) ); layout.setVerticalGroup( layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(layout.createSequentialGroup() .addContainerGap() .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE) .addComponent(jLabel1) .addComponent(jLabel2, javax.swing.GroupLayout.PREFERRED_SIZE, 34, javax.swing.GroupLayout.PREFERRED_SIZE) .addComponent(jLabel3, javax.swing.GroupLayout.PREFERRED_SIZE, 34, javax.swing.GroupLayout.PREFERRED_SIZE)) .addGap(18, 18, 18) .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE) .addComponent(SecondNumber, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE) .addComponent(jLabel4) .addComponent(Resurt) .addComponent(SelectName, javax.swing.GroupLayout.PREFERRED_SIZE, 24, javax.swing.GroupLayout.PREFERRED_SIZE)) .addGap(18, 18, 18) .addComponent(jButton1) .addContainerGap(23, Short.MAX_VALUE)) .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(layout.createSequentialGroup() .addGap(64, 64, 64) .addComponent(FirstNumber, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE) .addContainerGap(69, Short.MAX_VALUE))) );
pack(); }//
private void FirstNumberActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
}
private void SecondNumberActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
}
private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
//获取操作符
//获取第一个操作数
//获取第二个操作数
String First =this.FirstNumber.getText().trim();
String Second =this.SecondNumber.getText().trim();
String Select=this.SelectName.getSelectedItem().toString();
System.out.println(First);
System.out.println(Second );
System.out.println(Select);
//字符串无法进行四则运算,必须先转换成整数
int First1=Integer.parseUnsignedInt(First);
int Second2=Integer.parseInt(Second);
int Result=0;
switch(Select){
case "+":
Result=First1+Second2;
break;
case "-":
Result=First1-Second2;
break;
case "": Result=First1Second2;
break;
case "/": Result=First1/Second2;
break;
//得到的结果是整型,输出需要字符型
}
this.Resurt.setText(String.valueOf(Result));
}
/**
@param args the command line arguments / public static void main(String args[]) { / Set the Nimbus look and feel / // / If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
/* Create and display the form */ java.awt.EventQueue.invokeLater(new Runnable() { public void run() { new NewJFrame().setVisible(true); } }); }
// Variables declaration - do not modify
private javax.swing.JTextField FirstNumber;
private javax.swing.JTextField Resurt;
private javax.swing.JTextField SecondNumber;
private javax.swing.JComboBox SelectName;
private javax.swing.JButton jButton1;
private javax.swing.JLabel jLabel1;
private javax.swing.JLabel jLabel2;
private javax.swing.JLabel jLabel3;
private javax.swing.JLabel jLabel4;
// End of variables declaration
}
/*
package cn.itcast;
import java.awt.Dimension; import java.awt.Image; import java.awt.Toolkit; import javax.swing.JFrame;
/** *专门 做界面的报
@author 曹航 */ public class uiutil { private uiutil(){ } //修改窗体的小图标 public static void setFrameImage(JFrame jf){ //第一步,获取工具类对象 //public static Toolkit getDeFaultToolkit();获取默认工具包 Toolkit tk=Toolkit.getDefaultToolkit(); Image i= tk.getImage("src\cn\itcast\source\jjcc.jpg"); //给窗体设置图标 jf.setIconImage(i); }
public static void setFrameCenter(JFrame jf){ /** * 思路: * A:获取屏幕以及窗体宽和高 * B:屏幕的宽高减去窗体的宽高除42 * C:赋值 * D:联系 */ Toolkit tk=Toolkit.getDefaultToolkit(); Dimension d=tk.getScreenSize(); double Screenwidth=d.getWidth(); double Screenheight=d.getHeight(); //获取窗体宽高 int Framewidth=jf.getWidth(); int Frameheight=jf.getHeight(); //计算新的宽高 int width=(int)(Screenwidth-Framewidth)/2; int height=(int)(Screenheight-Frameheight)/2;
//将新的宽高付给locate
jf.setLocation(width, height);
}
}
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。