| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 2626 人关注过本帖
标题:[求助]java计算器
只看楼主 加入收藏
千里冰封
Rank: 16Rank: 16Rank: 16Rank: 16
来 自:灌水之王
等 级:版主
威 望:155
帖 子:28477
专家分:59
注 册:2006-2-26
收藏
得分:0 

呵呵,你的界面比较不好看,是因为你的布局不太好
你可以用用GridLayout来处理你的布局,如果你想精确控制你的组件位置,可以不用布局管理器,而直接调用组件的setBounds(int x,int y,int width,int height)来设置
另外.JAVA的界面和WINDOWS本来就是有差异的,并且现在都推荐用javax.swing包里的组件来做界面,你用的还是老的,希望能改过来
也希望我以上的话对你有一点点的帮助


可惜不是你,陪我到最后
2006-11-06 18:40
千里冰封
Rank: 16Rank: 16Rank: 16Rank: 16
来 自:灌水之王
等 级:版主
威 望:155
帖 子:28477
专家分:59
注 册:2006-2-26
收藏
得分:0 
还有,你的代码可以写得更好看些的,你可以把一些代码整理出来,写到一个方法里面去,然后再调用方法,这样代码就很清析了.并且想要修改维护也会方便很多

可惜不是你,陪我到最后
2006-11-06 18:40
达达
Rank: 1
等 级:新手上路
帖 子:87
专家分:0
注 册:2005-5-26
收藏
得分:0 
实验要求用awt和swing 分别写一个,
我会尽量熟悉java的编程风格的,谢谢版主.

2006-11-07 06:38
千里冰封
Rank: 16Rank: 16Rank: 16Rank: 16
来 自:灌水之王
等 级:版主
威 望:155
帖 子:28477
专家分:59
注 册:2006-2-26
收藏
得分:0 
祝你成功

可惜不是你,陪我到最后
2006-11-07 08:59
十口草兮
Rank: 1
等 级:新手上路
帖 子:16
专家分:0
注 册:2006-11-8
收藏
得分:0 
因为我最近也在做一个"24点"的游戏,所以感慨很多,用javax.swing做出来的界面漂亮的多,也快捷
2006-11-09 00:06
kepazhiji
Rank: 1
等 级:新手上路
帖 子:3
专家分:0
注 册:2006-11-10
收藏
得分:0 

package Jsq;

import java.awt.BorderLayout;

import javax.swing.JFrame;
import javax.swing.JLabel;
import com.borland.jbcl.layout.XYLayout;
import com.borland.jbcl.layout.*;
import javax.swing.JTextField;
import javax.swing.JButton;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowEvent;
import java.awt.Toolkit;
import java.awt.Dimension;
import javax.swing.JOptionPane;

/**
* <p>Title: </p>
*
* <p>Description: </p>
*
* <p>Copyright: Copyright (c) 2006</p>
*
* <p>Company: </p>
*
* @author not attributable
* @version 1.0
*/
public class Jsqf extends JFrame {
JLabel jLabel1 = new JLabel();
XYLayout xYLayout1 = new XYLayout();
JTextField jTextField1 = new JTextField();
JButton jButton1 = new JButton();
JButton jButton2 = new JButton();
JButton jButton3 = new JButton();
JButton jButton4 = new JButton();
JButton jButton5 = new JButton();
JButton jButton6 = new JButton();
JButton jButton7 = new JButton();
JButton jButton8 = new JButton();
JButton jButton9 = new JButton();
JButton jButton10 = new JButton();
JButton jButton11 = new JButton();
JButton jButton12 = new JButton();
JButton jButton13 = new JButton();
JButton jButton14 = new JButton();
JButton jButton15 = new JButton();
JButton jButton16 = new JButton();
JOptionPane jOptionPane1 = new JOptionPane();
boolean flag=false;
public Jsqf() {
try {
jbInit();
} catch (Exception exception) {
exception.printStackTrace();
}
}

private void jbInit() throws Exception {
getContentPane().setLayout(xYLayout1);
jLabel1.setText("结果:");
jButton10.setText("7");
jButton10.addActionListener(new Jsqf_jButton10_actionAdapter(this));
jButton11.setText("8");
jButton11.addActionListener(new Jsqf_jButton11_actionAdapter(this));
jButton12.setText("9");
jButton12.addActionListener(new Jsqf_jButton12_actionAdapter(this));
jButton13.setText("*");
jButton13.addActionListener(new Jsqf_jButton13_actionAdapter(this));
jButton14.setText("0");
jButton14.addActionListener(new Jsqf_jButton14_actionAdapter(this));
jButton15.setText("=");
jButton15.addActionListener(new Jsqf_jButton15_actionAdapter(this));
jButton16.setText("/");
jButton16.addActionListener(new Jsqf_jButton16_actionAdapter(this));
jButton2.addActionListener(new Jsqf_jButton2_actionAdapter(this));
jButton3.addActionListener(new Jsqf_jButton3_actionAdapter(this));
jButton4.addActionListener(new Jsqf_jButton4_actionAdapter(this));
jButton6.addActionListener(new Jsqf_jButton6_actionAdapter(this));
jButton7.addActionListener(new Jsqf_jButton7_actionAdapter(this));
jButton8.addActionListener(new Jsqf_jButton8_actionAdapter(this));
jButton5.addActionListener(new Jsqf_jButton5_actionAdapter(this));
jButton9.addActionListener(new Jsqf_jButton9_actionAdapter(this));
jButton1.addActionListener(new Jsqf_jButton1_actionAdapter(this));
this.getContentPane().add(jTextField1,
new XYConstraints(98, 34, 167, -1));
jButton9.setText("-");
jButton8.setText("6");
jButton7.setText("5");
jButton6.setText("4");
jButton5.setText("+");
jButton4.setText("3");
jButton3.setText("2");
jButton2.setText("1");
jButton1.setText("清空");
this.getContentPane().add(jLabel1, new XYConstraints(45, 34, 51, 19));
this.getContentPane().add(jButton1, new XYConstraints(268, 32, -1, -1));
this.getContentPane().add(jButton15,
new XYConstraints(142, 230, 114, -1));

this.getContentPane().add(jButton14, new XYConstraints(60, 230, -1, -1));
this.getContentPane().add(jButton16, new XYConstraints(283, 230, -1, -1));
this.getContentPane().add(jButton11, new XYConstraints(142, 181, -1, -1));
this.getContentPane().add(jButton10, new XYConstraints(60, 181, -1, -1));
this.getContentPane().add(jButton12, new XYConstraints(218, 181, -1, -1));
this.getContentPane().add(jButton7, new XYConstraints(142, 133, -1, -1));
this.getContentPane().add(jButton6, new XYConstraints(60, 133, -1, -1));
this.getContentPane().add(jButton3, new XYConstraints(142, 82, -1, -1));
this.getContentPane().add(jButton2, new XYConstraints(60, 82, 41, -1));
this.getContentPane().add(jButton13, new XYConstraints(283, 181, -1, -1));
this.getContentPane().add(jButton9, new XYConstraints(283, 133, -1, -1));
this.getContentPane().add(jButton5, new XYConstraints(283, 82, -1, -1));
this.getContentPane().add(jOptionPane1,
new XYConstraints(69, 237, -1, 3));
this.getContentPane().add(jButton4, new XYConstraints(218, 82, -1, -1));
this.getContentPane().add(jButton8, new XYConstraints(218, 133, -1, -1));
}

public static void main(String[] args) {
Jsqf jsqf = new Jsqf();
jsqf.setSize(400,400);
// Center the frame
Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
Dimension frameSize = jsqf.getSize();
// if (frameSize.height > screenSize.height)
// frameSize.height = screenSize.height;
// if (frameSize.width > screenSize.width)
// frameSize.width = screenSize.width;
jsqf.setLocation((screenSize.width - frameSize.width) / 2, (screenSize.height - frameSize.height) / 2);
jsqf.setVisible(true);
}

public void jButton2_actionPerformed(ActionEvent e) {
if (flag==false){
String s = jTextField1.getText();
jTextField1.setText(s + jButton2.getText());
}
else{
jTextField1.setText(jButton2.getText());
flag=false;
}
}

public void jButton3_actionPerformed(ActionEvent e) {
if (flag==false){
String s = jTextField1.getText();
jTextField1.setText(s + jButton3.getText());
}
else
{
jTextField1.setText(jButton3.getText());
flag=false;
}
}

public void jButton4_actionPerformed(ActionEvent e) {
if (flag==false){
String s = jTextField1.getText();
jTextField1.setText(s + jButton4.getText());
}
else{
jTextField1.setText(jButton4.getText());
flag=false;
}
}

public void jButton6_actionPerformed(ActionEvent e) {
if (flag==false){
String s = jTextField1.getText();
jTextField1.setText(s + jButton6.getText());
}
else{
jTextField1.setText(jButton6.getText());
flag=false;
}
}

public void jButton7_actionPerformed(ActionEvent e) {
if (flag==false){
String s = jTextField1.getText();
jTextField1.setText(s + jButton7.getText());
}
else{
jTextField1.setText(jButton7.getText());
flag=false;
}
}

public void jButton8_actionPerformed(ActionEvent e) {
if (flag==false){
String s = jTextField1.getText();
jTextField1.setText(s + jButton8.getText());
}
else
{
jTextField1.setText(jButton8.getText());
flag=false;
}
}

public void jButton10_actionPerformed(ActionEvent e) {
if (flag==false){
String s = jTextField1.getText();
jTextField1.setText(s + jButton10.getText());
}
else{
jTextField1.setText(jButton10.getText());
flag=false;
}
}

public void jButton11_actionPerformed(ActionEvent e) {
if (flag==false){
String s = jTextField1.getText();
jTextField1.setText(s + jButton11.getText());
}
else{
jTextField1.setText(jButton11.getText());
flag=false;
}
}

public void jButton12_actionPerformed(ActionEvent e) {
if (flag==false){
String s = jTextField1.getText();
jTextField1.setText(s + jButton12.getText());
}
else{
jTextField1.setText(jButton12.getText());
flag=false;
}
}
public void jButton14_actionPerformed(ActionEvent e) {
if (flag==false){
String s = jTextField1.getText();
jTextField1.setText(s + jButton14.getText());
}
else{
jTextField1.setText(jButton14.getText());
flag=false;
}
}

2006-11-10 19:42
kepazhiji
Rank: 1
等 级:新手上路
帖 子:3
专家分:0
注 册:2006-11-10
收藏
得分:0 

///紧接上面


public void jButton5_actionPerformed(ActionEvent e) {
String s=jTextField1.getText();
jTextField1.setText(s+" "+jButton5.getText()+" ");
flag=false;
}

public void jButton9_actionPerformed(ActionEvent e) {
String s=jTextField1.getText();
jTextField1.setText(s+" "+jButton9.getText()+" ");
flag=false;
}

public void jButton13_actionPerformed(ActionEvent e) {
String s=jTextField1.getText();
jTextField1.setText(s+" "+jButton13.getText()+" ");
flag=false;
}

public void jButton16_actionPerformed(ActionEvent e) {
String s=jTextField1.getText();
jTextField1.setText(s+" "+jButton16.getText()+" ");
flag=false;
}

public void jButton1_actionPerformed(ActionEvent e) {
jTextField1.setText("");
}

public void jButton15_actionPerformed(ActionEvent e) {
float r=0;
char op1;
String t=jTextField1.getText();
if (t.length()==0){return;}
try{
int space=t.indexOf(' ');
String s1=t.substring(0,space);
//char op=t.substring(space+1,1);
String s2=t.substring(space+3);
// char[] op=t.substring(space+1,1).toCharArray();
float arg1=Float.parseFloat(s1);
float arg2=Float.parseFloat(s2);
//op1=op[0];
op1=t.charAt(space+1);

switch(op1)
{
case'+':
r=arg1+arg2;
break;
case'-':
r=arg1-arg2;
break;
case'*':
r=arg1*arg2;
break;
case'/':
if(arg2==0)
{
//throw new Exception();
jTextField1.setText("除数不能为零");
flag=true;
return;
}
else
{
r=arg1/arg2;
}
break;
default:
//throw new Exception();
}

jTextField1.setText(Float.toString(r));
flag=true;
}
catch (StringIndexOutOfBoundsException exception)
{
jOptionPane1.showMessageDialog(null,exception.getMessage());
flag=true;
}
catch (NumberFormatException ex){
jOptionPane1.showMessageDialog(null,ex.getMessage());
flag=true;
}
}
/**
* Overridden so we can exit when window is closed
*
* @param e WindowEvent
*/
protected void processWindowEvent(WindowEvent e) {
super.processWindowEvent(e);
if (e.getID() == WindowEvent.WINDOW_CLOSING) {
System.exit(0);
}
}
/**
* File | Exit action performed
*
* @param e ActionEvent
*/
public void jMenuFileExit_actionPerformed(ActionEvent e) {
System.exit(0);
}

}


class Jsqf_jButton14_actionAdapter implements ActionListener {
private Jsqf adaptee;
Jsqf_jButton14_actionAdapter(Jsqf adaptee) {
this.adaptee = adaptee;
}

public void actionPerformed(ActionEvent e) {
adaptee.jButton14_actionPerformed(e);
}
}


class Jsqf_jButton15_actionAdapter implements ActionListener {
private Jsqf adaptee;
Jsqf_jButton15_actionAdapter(Jsqf adaptee) {
this.adaptee = adaptee;
}

public void actionPerformed(ActionEvent e) {
adaptee.jButton15_actionPerformed(e);
}
}


class Jsqf_jButton1_actionAdapter implements ActionListener {
private Jsqf adaptee;
Jsqf_jButton1_actionAdapter(Jsqf adaptee) {
this.adaptee = adaptee;
}

public void actionPerformed(ActionEvent e) {
adaptee.jButton1_actionPerformed(e);
}
}


class Jsqf_jButton16_actionAdapter implements ActionListener {
private Jsqf adaptee;
Jsqf_jButton16_actionAdapter(Jsqf adaptee) {
this.adaptee = adaptee;
}

public void actionPerformed(ActionEvent e) {
adaptee.jButton16_actionPerformed(e);
}
}


class Jsqf_jButton13_actionAdapter implements ActionListener {
private Jsqf adaptee;
Jsqf_jButton13_actionAdapter(Jsqf adaptee) {
this.adaptee = adaptee;
}

public void actionPerformed(ActionEvent e) {
adaptee.jButton13_actionPerformed(e);
}
}


class Jsqf_jButton9_actionAdapter implements ActionListener {
private Jsqf adaptee;
Jsqf_jButton9_actionAdapter(Jsqf adaptee) {
this.adaptee = adaptee;
}

public void actionPerformed(ActionEvent e) {
adaptee.jButton9_actionPerformed(e);
}
}


class Jsqf_jButton5_actionAdapter implements ActionListener {
private Jsqf adaptee;
Jsqf_jButton5_actionAdapter(Jsqf adaptee) {
this.adaptee = adaptee;
}

public void actionPerformed(ActionEvent e) {
adaptee.jButton5_actionPerformed(e);
}
}


class Jsqf_jButton12_actionAdapter implements ActionListener {
private Jsqf adaptee;
Jsqf_jButton12_actionAdapter(Jsqf adaptee) {
this.adaptee = adaptee;
}

public void actionPerformed(ActionEvent e) {
adaptee.jButton12_actionPerformed(e);
}
}


class Jsqf_jButton11_actionAdapter implements ActionListener {
private Jsqf adaptee;
Jsqf_jButton11_actionAdapter(Jsqf adaptee) {
this.adaptee = adaptee;
}

public void actionPerformed(ActionEvent e) {
adaptee.jButton11_actionPerformed(e);
}
}


class Jsqf_jButton10_actionAdapter implements ActionListener {
private Jsqf adaptee;
Jsqf_jButton10_actionAdapter(Jsqf adaptee) {
this.adaptee = adaptee;
}

public void actionPerformed(ActionEvent e) {
adaptee.jButton10_actionPerformed(e);
}
}


class Jsqf_jButton8_actionAdapter implements ActionListener {
private Jsqf adaptee;
Jsqf_jButton8_actionAdapter(Jsqf adaptee) {
this.adaptee = adaptee;
}

public void actionPerformed(ActionEvent e) {
adaptee.jButton8_actionPerformed(e);
}
}


class Jsqf_jButton7_actionAdapter implements ActionListener {
private Jsqf adaptee;
Jsqf_jButton7_actionAdapter(Jsqf adaptee) {
this.adaptee = adaptee;
}

public void actionPerformed(ActionEvent e) {
adaptee.jButton7_actionPerformed(e);
}
}


class Jsqf_jButton6_actionAdapter implements ActionListener {
private Jsqf adaptee;
Jsqf_jButton6_actionAdapter(Jsqf adaptee) {
this.adaptee = adaptee;
}

public void actionPerformed(ActionEvent e) {
adaptee.jButton6_actionPerformed(e);
}
}


class Jsqf_jButton4_actionAdapter implements ActionListener {
private Jsqf adaptee;
Jsqf_jButton4_actionAdapter(Jsqf adaptee) {
this.adaptee = adaptee;
}

public void actionPerformed(ActionEvent e) {
adaptee.jButton4_actionPerformed(e);
}
}


class Jsqf_jButton3_actionAdapter implements ActionListener {
private Jsqf adaptee;
Jsqf_jButton3_actionAdapter(Jsqf adaptee) {
this.adaptee = adaptee;
}

public void actionPerformed(ActionEvent e) {
adaptee.jButton3_actionPerformed(e);
}
}


class Jsqf_jButton2_actionAdapter implements ActionListener {
private Jsqf adaptee;
Jsqf_jButton2_actionAdapter(Jsqf adaptee) {
this.adaptee = adaptee;
}

public void actionPerformed(ActionEvent e) {
adaptee.jButton2_actionPerformed(e);
}
}

2006-11-10 19:44
kepazhiji
Rank: 1
等 级:新手上路
帖 子:3
专家分:0
注 册:2006-11-10
收藏
得分:0 
p
2006-11-10 19:46
ysol
Rank: 1
等 级:新手上路
帖 子:107
专家分:0
注 册:2006-6-11
收藏
得分:0 
用SWING写界面那就叫痛苦!!!!!!
怀恋C#!!!!!!!!!!
2006-11-10 21:16
千里冰封
Rank: 16Rank: 16Rank: 16Rank: 16
来 自:灌水之王
等 级:版主
威 望:155
帖 子:28477
专家分:59
注 册:2006-2-26
收藏
得分:0 
本来可以很简单的,你用了JB代码就给你生成这样了
所以,写程序最好还是自己写

可惜不是你,陪我到最后
2006-11-11 00:16
快速回复:[求助]java计算器
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.026285 second(s), 7 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved