JAvaは簡単な計算機を書きます

11213 ワード

内容関連:swing
テーマの要求:1つのアプリケーションを編纂して、4つのボタンを設計して、それぞれ“プラス”、“マイナス”、“乗算”、“除算”と命名して、3つのテキストボックスがあります.対応するボタンをクリックして、2つのテキストボックスの数値を演算し、3番目のテキストボックスに結果を表示します.
考え方は以下の通りである.
  • すべてのコンポーネントを1つの最上位コンテナに配置する
  • はコンポーネントの位置を配置して、私はGridLayoutを使って、簡単で便利にコンポーネントを配置して、結局初学の
  • は4つの計算ボタンのbuttonactionを書き出し、textfieldの値を取得して計算し、textareaの
  • に値を入れる.
  • 最上位メニューを書いて遊ぶ、終了、アプリケーション情報を含む、一時的にファイルioに関連していない、
  • コードは次のとおりです.
    import javax.swing.*;
    import java.awt.*;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    import java.awt.event.KeyEvent;
    import java.awt.image.Kernel;
    import java.text.DecimalFormat;
    
    public class e4_1 {
    
    
        public static void main(String[] args) {
            SwingUtilities.invokeLater(new Runnable() {
                @Override
                public void run() {
                    caculateGUI();
                }
            });
        }
        private static void caculateGUI(){
            JFrame.setDefaultLookAndFeelDecorated(true);
            JFrame frame = new JFrame();
            frame.setTitle("caculator");
            frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            frame.setTitle("caculator");
            frame.setLayout(new GridLayout(5,2));
    
            //add menubar
            JMenuBar jMenuBar = new JMenuBar();
            JMenu operationMenu = new JMenu("operation");
            JMenu helpMenu = new JMenu("help");
            jMenuBar.add(operationMenu);
            jMenuBar.add(helpMenu);
    
            //add exit actionlistener
            JMenuItem operationExitMI = new JMenuItem("Exit");
            operationMenu.add(operationExitMI);
            operationExitMI.addActionListener(new ActionListener() {
                @Override
                public void actionPerformed(ActionEvent e) {
                    System.exit(0);
                }
            });
    
            // add information actionlistener
            JMenuItem helpInformationMI = new JMenuItem("Information");
            helpMenu.add(helpInformationMI);
            helpInformationMI.addActionListener(new ActionListener() {
                @Override
                public void actionPerformed(ActionEvent e) {
                    JOptionPane.showOptionDialog(null,
                              "program name:  caculator 
    "
    + "information : trash
    "
    + " created by : ChrisTree
    "
    +"
    "
    +"fuck the world
    "
    +"i love zhangyan forever
    "
    , "caculator information", JOptionPane.DEFAULT_OPTION, JOptionPane.INFORMATION_MESSAGE, null, null, null); } }); //set menubar frame.setJMenuBar(jMenuBar); frame.setSize(200, 100); frame.setVisible(true); //add label frame.add(new JLabel("1st num")); frame.add(new JLabel("2nd num")); //creat and add textfield JTextField textField1 = new JTextField(); JTextField textField2 = new JTextField(); frame.add(textField1); frame.add(textField2); //add result_label and result_textarea frame.add(new JLabel("result")); JTextArea jTextArea = new JTextArea(); frame.add(jTextArea); //creat buttons JButton jButton1 = new JButton("+"); JButton jButton2 = new JButton("-"); JButton jButton3 = new JButton("*"); JButton jButton4 = new JButton("/"); //buttonActions frame.add(jButton1); jButton1.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { int a = Integer.parseInt(textField1.getText()); int b = Integer.parseInt(textField2.getText()); jTextArea.setText(Integer.toString(a+b)); } }); frame.add(jButton2); jButton2.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { int a = Integer.parseInt(textField1.getText()); int b = Integer.parseInt(textField2.getText()); jTextArea.setText(Integer.toString(a-b)); } }); frame.add(jButton3); jButton3.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { int a = Integer.parseInt(textField1.getText()); int b = Integer.parseInt(textField2.getText()); jTextArea.setText(Integer.toString(a*b)); } }); frame.add(jButton4); jButton4.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { double a = Double.parseDouble(textField1.getText()); double b = Double.parseDouble(textField2.getText()); jTextArea.setText(Double.toString(a/b)); } }); int frameWidth = 400; int frameHeight = 800; Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize(); frame.setBounds((int)screenSize.getWidth() - frameWidth,0,frameWidth,frameHeight); frame.setVisible(true); } }

    コードが必要な場合は、変更してください
     helpInformationMI.addActionListener(new ActionListener() {
                @Override
                public void actionPerformed(ActionEvent e) {
                    JOptionPane.showOptionDialog(null,
                              "program name:  caculator 
    "
    + "information : trash
    "
    + " created by : ChrisTree
    "
    +"
    "
    +"fuck the world
    "
    +"i love zhangyan forever
    "
    , "caculator information", JOptionPane.DEFAULT_OPTION, JOptionPane.INFORMATION_MESSAGE, null, null, null); } });
    showOptionDialogダイアログボックスの内容
    または、//add menu注釈から//add label注釈までの内容を削除する
    編集者は2018/05/30に漏れや問題があれば、私信を歓迎して私と検討して、私は謙虚に教えを受けます[email protected]