当前位置:编程学习 > JAVA >>

有一个Java程序,大家帮忙改改,我不太会Java

如题,是网上找的计算器。但是它这个界面不太美观,按钮太大文本框太小,大家帮我改改,帮我把界面改的好看一些。是用Eclipse写的。

import javax.swing.*
import java.awt.*;
import java.awt.event.*;
public class Calculator extends JFrame
{
private static final long serialVersionUID = 8199443193151152362L;
private JButton bto_s=new JButton("sqrt");
private JButton bto_zf=new JButton("+/-");
private JButton bto_ce=new JButton("CE");
private JButton bto_c=new JButton("C");
private JButton bto_7=new JButton("7");
private JButton bto_8=new JButton("8");
private JButton bto_9=new JButton("9");
private JButton bto_chu=new JButton("/");
private JButton bto_4=new JButton("4");
private JButton bto_5=new JButton("5");
private JButton bto_6=new JButton("6");
private JButton bto_cheng=new JButton("*");
private JButton bto_1=new JButton("1");
private JButton bto_2=new JButton("2");
private JButton bto_3=new JButton("3");
private JButton bto_jian=new JButton("-");
private JButton bto_0=new JButton("0");
private JButton bto_dian=new JButton(".");
private JButton bto_deng=new JButton("=");
private JButton bto_jia=new JButton("+");
JButton button[]={bto_s,bto_zf,bto_ce,bto_c,bto_7,bto_8,bto_9,bto_chu,bto_4,bto_5,bto_6,bto_cheng,bto_1,bto_2,bto_3,bto_jian,
    bto_0,bto_dian,bto_deng,bto_jia};
private JTextField text_double;// 计算器主文本框
private String operator = "="; //当前运算的运算符
private boolean firstDigit = true; // 标志用户按的是否是整个表达式的第一个数字,或者是运算符后的第一个数字
private double resultNum = 0.0; // 计算的中间结果
private boolean operateValidFlag = true; //判断操作是否合法
public Calculator()    //类的构造函数
{
   super("Calculator");
   this.setBounds(500, 255, 300, 300);
   this.setResizable(false);
   this.setBackground(Color.blue);
   this.getContentPane().setLayout(new BorderLayout());//设置布局
   text_double=new JTextField("0.",50);//设置文本区
   text_double.setHorizontalAlignment(JTextField.RIGHT);//设置水平对齐方式未右对齐
   text_double.setBounds(500,255,100,100);
   this.getContentPane().add(text_double,BorderLayout.NORTH);//将文本区添加到Content北部
   JPanel panel=new JPanel(new GridLayout(5,4));//在内容窗口添加一个网格布局
   this.getContentPane().add(panel);//添加panel面板
   for(int i=0;i<button.length;i++)//在面板上添加按钮
    panel.add(button[i]);
   for(int i=0;i<button.length;i++)
   text_double.setEditable(false);//文本框不可编辑
   this.setVisible(true);
}
public static void main(String[] args) 
{
new Calculator();
}
} --------------------编程问答-------------------- 我觉得你的思路比那些直接伸手就是要毕设的好一点!有前途,少年。 --------------------编程问答-------------------- 修改了下字体:
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Font;
import java.awt.GridLayout;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JTextField;

public class Calculator extends JFrame
{
    private static final long serialVersionUID = 8199443193151152362L;
    
    private JButton bto_s = new JButton("sqrt");
    
    
    private JButton bto_zf = new JButton("+/-");
    
    private JButton bto_ce = new JButton("CE");
    
    private JButton bto_c = new JButton("C");
    
    private JButton bto_7 = new JButton("7");
    
    private JButton bto_8 = new JButton("8");
    
    private JButton bto_9 = new JButton("9");
    
    private JButton bto_chu = new JButton("/");
    
    private JButton bto_4 = new JButton("4");
    
    private JButton bto_5 = new JButton("5");
    
    private JButton bto_6 = new JButton("6");
    
    private JButton bto_cheng = new JButton("*");
    
    private JButton bto_1 = new JButton("1");
    
    private JButton bto_2 = new JButton("2");
    
    private JButton bto_3 = new JButton("3");
    
    private JButton bto_jian = new JButton("-");
    
    private JButton bto_0 = new JButton("0");
    
    private JButton bto_dian = new JButton(".");
    
    private JButton bto_deng = new JButton("=");
    
    private JButton bto_jia = new JButton("+");
    
    JButton button[] =
        {bto_s, bto_zf, bto_ce, bto_c, bto_7, bto_8, bto_9, bto_chu, bto_4, bto_5, bto_6, bto_cheng, bto_1, bto_2,
            bto_3, bto_jian, bto_0, bto_dian, bto_deng, bto_jia};
    
    private JTextField text_double;// 计算器主文本框
    
    private String operator = "="; //当前运算的运算符
    
    private boolean firstDigit = true; // 标志用户按的是否是整个表达式的第一个数字,或者是运算符后的第一个数字
    
    private double resultNum = 0.0; // 计算的中间结果
    
    private boolean operateValidFlag = true; //判断操作是否合法
    
    public Calculator() //类的构造函数
    {
        super("Calculator");
        this.setBounds(500, 255, 300, 300);
        this.setResizable(false);
        this.setBackground(Color.blue);
        this.getContentPane().setLayout(new BorderLayout());//设置布局
        text_double = new JTextField("0.", 50);//设置文本区
        text_double.setHorizontalAlignment(JTextField.RIGHT);//设置水平对齐方式未右对齐
        text_double.setBounds(500, 255, 100, 100);
        this.getContentPane().add(text_double, BorderLayout.NORTH);//将文本区添加到Content北部
        JPanel panel = new JPanel(new GridLayout(5, 4));//在内容窗口添加一个网格布局
        this.getContentPane().add(panel);//添加panel面板
        for (int i = 0; i < button.length; i++)
        {
            button[i].setFont(new Font("",Font.BOLD,18));
            //在面板上添加按钮
            panel.add(button[i]);
        }
        for (int i = 0; i < button.length; i++)
            text_double.setEditable(false);//文本框不可编辑
        this.setVisible(true);
    }
    
    public static void main(String[] args)
    {
        new Calculator();
    }
} --------------------编程问答--------------------
引用 1 楼 q2979978 的回复:
我觉得你的思路比那些直接伸手就是要毕设的好一点!有前途,少年。

+1
--------------------编程问答-------------------- 我晕,这也行!如果只是为了画个窗口,其实用VB6.0很好画!还更美观! --------------------编程问答-------------------- text_double.setSize lz用这个试试 --------------------编程问答-------------------- java自定义按钮外观
293031 

import java.awt.FlowLayout;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.UIManager;
import javax.swing.plaf.synth.SynthLookAndFeel;  
     public class MyButton {
        JFrame frame = new JFrame("Test Buttons");
       JButton jButton = new JButton("JButton"); // 按钮 
     public MyButton() { 
        frame.setLayout(new FlowLayout()); 
        frame.getContentPane().add(jButton); } 
    public void show() {  
        frame.pack();  
        frame.show(); }
    public static void main(String[] args) { 
        MyButton tb = new MyButton();  
        tb.show();  
        SynthLookAndFeel slf = new SynthLookAndFeel();
       try {    
          slf.load(MyButton.class.getResourceAsStream("mybutton.xml"), MyButton.class);      UIManager.setLookAndFeel(slf);  } catch (Exception e) {      e.printStackTrace();      return;  }  }} 
补充:Java ,  Eclipse
CopyRight © 2012 站长网 编程知识问答 www.zzzyk.com All Rights Reserved
部份技术文章来自网络,