Java-几个基础的Java小应用

主要内容有:简单的计算器、小时钟程序、JMenu菜单应用实例

简单的计算器

import java.awt.*;
import javax.swing.*;
import java.awt.event.*;

public class Caculator implements ActionListener {
    private JFrame frame;
    private Container contentPane;

    private JMenuItem m_item_1, m_item_2;
    private JMenu m_1, m_2;
    private JMenuBar mb;
    private JTextField textField;
    private JButton up_btn_1, up_btn_2, up_btn_3;
    private JButton left_btn_1, left_btn_2, left_btn_3, left_btn_4;

    private JButton[] btn_no = new JButton[20];
    private String[] btn_content = new String[]{"7", "8", "9", "/", "sqrt", "4", "5",
            "6", "*", "%", "1", "2", "3", "-", "1/x", "0", "+/-", ".", "+", "="};

    public Caculator() {
        frame = new JFrame("Caculator");
        contentPane = frame.getContentPane();
        frame.setBounds(200, 200, 400, 400);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        initGUI();
    }

    public void initGUI() {
        //声明子容器
        JPanel p1 = new JPanel(new FlowLayout(FlowLayout.RIGHT));
        JPanel p2 = new JPanel(new GridLayout(4, 1, 5, 5));
        JPanel p3 = new JPanel(new GridLayout(4, 5, 5, 5));
        JPanel p4 = new JPanel(new BorderLayout());
        JPanel p5 = new JPanel(new BorderLayout());

        //菜单
        m_1 = new JMenu("Edit");
        m_2 = new JMenu("About");
        m_item_1 = new JMenuItem("Ctrl c");
        m_item_2 = new JMenuItem("Ctrl v");
        m_1.add(m_item_1);
        m_1.add(new JSeparator());
        m_1.add(new JMenuItem("test"));
        m_2.add(m_item_2);
        mb = new JMenuBar();
        mb.add(m_1);
        mb.add(m_2);
        p5.add(mb, BorderLayout.NORTH);

        up_btn_1 = new JButton("Backspace");
        up_btn_2 = new JButton("CE");
        up_btn_3 = new JButton("C");

        p1.add(up_btn_1);
        p1.add(up_btn_2);
        p1.add(up_btn_3);


        left_btn_1 = new JButton("MC");
        left_btn_2 = new JButton("MR");
        left_btn_3 = new JButton("MS");
        left_btn_4 = new JButton("M+");

        p2.add(left_btn_1);
        p2.add(left_btn_2);
        p2.add(left_btn_3);
        p2.add(left_btn_4);

        //添加运算按钮
        for (int i = 0; i < btn_no.length; i++) {
            btn_no[i] = new JButton(btn_content[i]);
        }

        //简便的方法,使用数组存放后边的符号
        for (int i = 0; i < btn_no.length; i++) {
            p3.add(btn_no[i]);
            btn_no[i].addActionListener(this);
        }

        p4.add(p1, BorderLayout.NORTH);
        p4.add(p2, BorderLayout.WEST);
        p4.add(p3, BorderLayout.CENTER);

        textField = new JTextField();
        textField.setHorizontalAlignment(JTextField.RIGHT);
        p5.add(textField, BorderLayout.SOUTH);

        contentPane.add(p4, BorderLayout.CENTER);
        contentPane.add(p5, BorderLayout.NORTH);
    }

    public void go() {
        frame.setVisible(true);
    }

    StringBuffer strBuf = new StringBuffer("");

    public void actionPerformed(ActionEvent e) {
        Object source = e.getSource();

        for (int i = 0; i < btn_no.length; i++) {
            if (source == btn_no[i]) {
                JButton btn = (JButton) source;
                strBuf.append(btn.getLabel());
                textField.setText(strBuf.toString());
            }
        }
    }

    public static void main(String[] args) {
        new Caculator().go();
    }
}

小时钟程序ClockPanel

import java.awt.*;
import javax.swing.*;
import java.awt.event.*;

public class ClockDemo implements ActionListener {
    private JFrame frame;
    private JPanel clockPanel;
    private Container contentPane;

    public void actionPerformed(ActionEvent e) {
    }

    public ClockDemo() {
        frame = new JFrame("ClockDemo");
        contentPane = frame.getContentPane();
        frame.setBounds(200, 200, 400, 400);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        initGUI();
    }

    public void initGUI() {
        contentPane.setLayout(new BorderLayout());
        clockPanel = new ClockPanel();
        contentPane.add(clockPanel);
    }

    public void go() {
        frame.setVisible(true);
    }

    public static void main(String[] args) {
        new ClockDemo().go();
    }
}

class ClockPanel extends JPanel implements ActionListener {
    private Timer timer;
    //private java.util.Random rand;
    private int i = -15, j = -15, k = -15;

    public ClockPanel() {
        //rand=new java.util.Random();
        timer = new Timer(20, this);
        timer.start();
    }

    public void actionPerformed(ActionEvent e) {
        updateUI();        //来自JPanel
    }

    public void paint(Graphics g) {
        int cx = 200, cy = 200;    //圆心位置
        //int x1=rand.nextInt(400);
        //int y1=rand.nextInt(400);
        //int x2=rand.nextInt(400);
        //int y2=rand.nextInt(400);
        i++;
        if (0 == (i + 15) % 60) {
            j++;
            if (0 == (j + 15) % 12) {
                k++;
            }
        }
        double r1 = 150, r2 = 120, r3 = 100;    //半径
        double alpha = Math.PI / 30.0;    //表针一次走动所转的孤度数

        g.clearRect(0, 0, 400, 400);
        //g.drawLine(x1,y1,x2,y2);
        g.drawOval(50, 50, 300, 300);
        for (int n = 0; n < 60; n++) {
            double r0 = r1 - 3.0;
            if (0 == n % 5) {
                r0 = r1 - 8.0;
            }
            g.drawLine((int) (cx + r0 * Math.cos(n * alpha)), (int) (cy + r0 * Math.sin(n * alpha)), (int) (cx + r1 * Math.cos(n * alpha)), (int) (cy + r1 * Math.sin(n * alpha)));
        }
        g.drawLine(cx, cy, (int) (cx + r1 * Math.cos(i * alpha)), (int) (cy + r1 * Math.sin(i * alpha)));
        g.drawLine(cx, cy, (int) (cx + r2 * Math.cos(j * alpha)), (int) (cy + r2 * Math.sin(j * alpha)));
        g.drawLine(cx, cy, (int) (cx + r3 * Math.cos(k * alpha)), (int) (cy + r3 * Math.sin(k * alpha)));

    }
}   

JMenu菜单应用

import javax.swing.*;
import java.awt.*;

public class MenuTest {
    private JFrame frame;
    private Container contentPane;
    private JMenuItem m_item_1, m_item_2;
    private JMenu m_1, m_2;
    private JMenuBar mb;

    public MenuTest() {
        frame = new JFrame("Menu Test");
        frame.setBounds(200, 200, 400, 200);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        contentPane = frame.getContentPane();
        initGUI();
    }

    public void initGUI() {
        contentPane.setLayout(new BorderLayout());
        m_1 = new JMenu("Edit");
        m_2 = new JMenu("About");
        m_item_1 = new JMenuItem("Ctrl c");
        m_item_2 = new JMenuItem("Ctrl v");
        m_1.add(m_item_1);
        m_1.add(new JSeparator());
        m_1.add(new JMenuItem("test"));
        m_2.add(m_item_2);
        mb = new JMenuBar();
        mb.add(m_1);
        mb.add(m_2);
        contentPane.add(mb, BorderLayout.NORTH);
    }

    public void go() {
        frame.setVisible(true);
    }

    public static void main(String[] args) {
        new MenuTest().go();
    }
}

版权所有,转载请注明出处 luowei.github.io.