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