java程序语言设计:设计实现整数的算术运算,并使用图形用户界面,急!

就是这样的图形用户界面... 就是这样的图形用户界面 展开
 我来答
编程笔记
2016-06-05 · 超过31用户采纳过TA的回答
知道答主
回答量:106
采纳率:0%
帮助的人:36.1万
展开全部

import java.awt.BorderLayout;
import java.awt.Rectangle;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import javax.swing.JDialog;
import javax.swing.JPanel;

/**
 *
 * @author beans
 */
public class Test1 {

    public static void main(String[] args) {
        new Test1().showDialog();
    }

    /**
     * 显示窗口
     */
    private void showDialog() {
        JDialog dialog = new JDialog();

        dialog.setBounds(new Rectangle(150, 150, 370, 85));
        dialog.setTitle("整数的算术运算");

        dialog.addWindowListener(new WindowAdapter() {

            @Override
            public void windowClosing(WindowEvent e) {
                dialog.setVisible(false);
                dialog.dispose();
            }
        });

        dialog.add(this.getPanel(), BorderLayout.CENTER);
        dialog.setVisible(true);
    }

    private JPanel getPanel() {
        JPanel panel = new JPanel();

        jTextField1 = new javax.swing.JTextField();
        jComboBox1 = new javax.swing.JComboBox<>();
        jTextField2 = new javax.swing.JTextField();
        jLabel1 = new javax.swing.JLabel();
        jTextField3 = new javax.swing.JTextField();

        jTextField1.setText("10");
        jComboBox1.setModel(new javax.swing.DefaultComboBoxModel<>(new String[]{"+", "-", "*", "/"}));
        jTextField2.setText("10");
        jLabel1.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
        jLabel1.setText("=");
        jTextField3.setText("0");
        count();

        jTextField1.addKeyListener(new java.awt.event.KeyAdapter() {
            @Override
            public void keyReleased(java.awt.event.KeyEvent evt) {
                count();
            }
        });
        jTextField2.addKeyListener(new java.awt.event.KeyAdapter() {
            @Override
            public void keyReleased(java.awt.event.KeyEvent evt) {
                count();
            }
        });

        jComboBox1.addItemListener(new java.awt.event.ItemListener() {
            @Override
            public void itemStateChanged(java.awt.event.ItemEvent evt) {
                count();
            }
        });

        // 下面是布局代码
        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(panel);
        panel.setLayout(layout);
        layout.setHorizontalGroup(
                layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGroup(layout.createSequentialGroup()
                        .addContainerGap()
                        .addComponent(jTextField1, javax.swing.GroupLayout.PREFERRED_SIZE, 71, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(jComboBox1, javax.swing.GroupLayout.PREFERRED_SIZE, 45, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jTextField2, javax.swing.GroupLayout.PREFERRED_SIZE, 66, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(jLabel1, javax.swing.GroupLayout.PREFERRED_SIZE, 23, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jTextField3, javax.swing.GroupLayout.PREFERRED_SIZE, 58, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addContainerGap(48, Short.MAX_VALUE))
        );
        layout.linkSize(javax.swing.SwingConstants.HORIZONTAL, new java.awt.Component[]{jTextField1, jTextField2, jTextField3});

        layout.setVerticalGroup(
                layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGroup(layout.createSequentialGroup()
                        .addContainerGap()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                                .addComponent(jTextField1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addComponent(jComboBox1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addComponent(jTextField2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addComponent(jLabel1)
                                .addComponent(jTextField3, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addContainerGap(22, Short.MAX_VALUE))
        );
        layout.linkSize(javax.swing.SwingConstants.VERTICAL, new java.awt.Component[]{jTextField1, jTextField2, jTextField3});

        return panel;
    }

    /**
     * 计算
     */
    private void count() {
        int result = 0;
        switch (jComboBox1.getSelectedIndex()) {
            case 0:
                result = Integer.parseInt(jTextField1.getText()) + Integer.parseInt(jTextField2.getText());
                break;
            case 1:
                result = Integer.parseInt(jTextField1.getText()) - Integer.parseInt(jTextField2.getText());
                break;
            case 2:
                result = Integer.parseInt(jTextField1.getText()) * Integer.parseInt(jTextField2.getText());
                break;
            case 3:
                int a = Integer.parseInt(jTextField2.getText());
                result = a == 0 ? 0 : Integer.parseInt(jTextField1.getText()) / a;
                break;
        }
        jTextField3.setText(result + "");
    }

    private javax.swing.JComboBox<String> jComboBox1;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JTextField jTextField1;
    private javax.swing.JTextField jTextField2;
    private javax.swing.JTextField jTextField3;
}
推荐律师服务: 若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询

为你推荐:

下载百度知道APP,抢鲜体验
使用百度知道APP,立即抢鲜体验。你的手机镜头里或许有别人想知道的答案。
扫描二维码下载
×

类别

我们会通过消息、邮箱等方式尽快将举报结果通知您。

说明

0/200

提交
取消

辅 助

模 式