jframe简单使用--Java-GUI

1、描述

包含主要元素JPanel、JButton、JTextField,标题栏,消息框(message box)等,如设置标题,两个显示文本内容框,两个输入框,一个计算按钮,以及计算显示结果的对话框。

 

2、关于坐标问题

横轴为x轴,向右为正;

竖轴为y轴,向下为正;

两者交叉点为起点。

3、代码

package cn.com.java.test;

import java.awt.EventQueue;
import javax.swing.*;
import javax.swing.border.EmptyBorder;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;

public class CalculatorGUI extends JFrame {

    // Constructor
    public CalculatorGUI() {
        // Set the properties of the JFrame
        setTitle("促销活动计算器");
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        //设置整个窗口大小
        setBounds(100, 100, 350, 300);

        // 创建JPanel
        JPanel contentPane = new JPanel();
        contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
        setContentPane(contentPane);
        contentPane.setLayout(null);

        //添加一个显示文本
        JLabel lblNewLabel_money = new JLabel("请输入金额");
        lblNewLabel_money.setBounds(10, 0, 130, 30);
        //添加组件
        contentPane.add(lblNewLabel_money);

        //创建JTextField输入框textField1
        JTextField textField1 = new JTextField();
        textField1.setBounds(10, 40, 130, 30);
        //添加组件
        contentPane.add(textField1);

        //添加一个显示文本
        JLabel lblNewLabel_price = new JLabel("请输入单价");
        lblNewLabel_price.setBounds(10, 70, 130, 30);
        //添加组件
        contentPane.add(lblNewLabel_price);

        //创建JTextField输入框textField2
        JTextField textField2 = new JTextField();
        textField2.setBounds(10, 100, 130, 30);
        //添加组件
        contentPane.add(textField2);

        //运算按钮
        JButton addButton = new JButton("开始计算");

        //创建按钮并添加listener
        addButton.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                int num1 = Integer.parseInt(textField1.getText());
                int num2 = Integer.parseInt(textField2.getText());
                int result = num1/num2;
                //将结果以弹窗形式提示出来
                JOptionPane.showMessageDialog(null, "可得饮料 " + result+"瓶");
            }
        });
        addButton.setBounds(80, 160, 100, 30);
        contentPane.add(addButton);

    }

    // Main method
    public static void main(String[] args) {
        EventQueue.invokeLater(new Runnable() {
            public void run() {
                try {
                    CalculatorGUI frame = new CalculatorGUI();
                    frame.setVisible(true);
                } catch(Exception e) {
                    e.printStackTrace();
                }
            }
        });
    }
}

4、setBounds()

setBounds()是JFrame类的一个方法,用于设置窗口的大小和位置。

这里单独的setBounds()是整个窗口相对于电脑屏幕位置来说。(虽然这里左侧还是有点缝隙,)

setBounds()还可以为按钮,文本框设置坐标。

 5、其他说明

1、contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));含义

在Java Swing中,JFrame类包含一个内容窗格(content pane),它是一个可以包含其他Swing组件的面板。在JFrame中添加组件时,实际上是将它们添加到内容窗格中,而不是直接添加到JFrame本身。

EmptyBorder是一个Swing边框类,它可以用来创建一个无填充的边框。EmptyBorder的构造函数有四个参数,它们分别代表边框的上、左、下、右四个方向的边框宽度(以像素为单位)。

在这里,contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));的意思是将内容窗格的边框设置为上、左、下、右四个方向都有5像素的空白区域。这样可以使窗口的边缘与窗口中的组件之间留下一些空间,使窗口看起来更加美观。

2、addActionListener()含义

addActionListener() 是一个 Java Swing 中的方法,用于向按钮添加一个 ActionListener。在 Java Swing 中,ActionListener 是一个监听器接口,用于响应按钮的单击事件。

在上面的代码中,addButton.addActionListener 是将一个 ActionListener 对象添加到 addButton 按钮上,以便在 addButton 被单击时执行相应的操作。具体来说,当 addButton 被单击时,程序会调用 ActionListener 中的 actionPerformed(ActionEvent e) 方法,执行其中的代码块,实现添加操作的逻辑。

  • 1
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
对于Java GUI编程实现计算器,你可以使用Swing和AWT这两种Java GUI工具包之一。 以下是一个简单Java GUI计算器的实现示例: ```java import javax.swing.*; import java.awt.*; import java.awt.event.*; public class Calculator extends JFrame implements ActionListener { private JButton[] numberButtons; private JButton[] operatorButtons; private JButton clearButton, equalsButton, decimalButton; private JTextField textField; private double num1 = 0, num2 = 0, result = 0; private char operator; public Calculator() { setTitle("Calculator"); // Create the number buttons numberButtons = new JButton[10]; for (int i = 0; i < 10; i++) { numberButtons[i] = new JButton(String.valueOf(i)); } // Create the operator buttons operatorButtons = new JButton[4]; operatorButtons[0] = new JButton("+"); operatorButtons[1] = new JButton("-"); operatorButtons[2] = new JButton("*"); operatorButtons[3] = new JButton("/"); // Create the clear, equals, and decimal buttons clearButton = new JButton("C"); equalsButton = new JButton("="); decimalButton = new JButton("."); // Create the text field textField = new JTextField(10); textField.setEditable(false); // Set the layout manager and add the components setLayout(new GridBagLayout()); GridBagConstraints gc = new GridBagConstraints(); gc.gridx = 0; gc.gridy = 0; gc.gridwidth = 4; add(textField, gc); gc.gridwidth = 1; gc.gridy = 1; for (int i = 0; i < 10; i++) { gc.gridx = i % 3; gc.gridy = i / 3 + 2; add(numberButtons[i], gc); numberButtons[i].addActionListener(this); } gc.gridx = 0; gc.gridy = 5; add(operatorButtons[0], gc); operatorButtons[0].addActionListener(this); gc.gridx = 1; add(operatorButtons[1], gc); operatorButtons[1].addActionListener(this); gc.gridx = 2; add(operatorButtons[2], gc); operatorButtons[2].addActionListener(this); gc.gridx = 3; add(operatorButtons[3], gc); operatorButtons[3].addActionListener(this); gc.gridx = 0; gc.gridy = 6; add(clearButton, gc); clearButton.addActionListener(this); gc.gridx = 1; add(equalsButton, gc); equalsButton.addActionListener(this); gc.gridx = 2; add(decimalButton, gc); decimalButton.addActionListener(this); setSize(200, 200); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); setVisible(true); } public void actionPerformed(ActionEvent e) { for (int i = 0; i < 10; i++) { if (e.getSource() == numberButtons[i]) { textField.setText(textField.getText() + i); } } if (e.getSource() == decimalButton) { textField.setText(textField.getText() + "."); } if (e.getSource() == clearButton) { textField.setText(""); num1 = 0; num2 = 0; result = 0; operator = ' '; } if (e.getSource() == operatorButtons[0]) { num1 = Double.parseDouble(textField.getText()); operator = '+'; textField.setText(""); } if (e.getSource() == operatorButtons[1]) { num1 = Double.parseDouble(textField.getText()); operator = '-'; textField.setText(""); } if (e.getSource() == operatorButtons[2]) { num1 = Double.parseDouble(textField.getText()); operator = '*'; textField.setText(""); } if (e.getSource() == operatorButtons[3]) { num1 = Double.parseDouble(textField.getText()); operator = '/'; textField.setText(""); } if (e.getSource() == equalsButton) { num2 = Double.parseDouble(textField.getText()); switch (operator) { case '+': result = num1 + num2; break; case '-': result = num1 - num2; break; case '*': result = num1 * num2; break; case '/': result = num1 / num2; break; } textField.setText(Double.toString(result)); } } public static void main(String[] args) { new Calculator(); } } ``` 这个示例应该可以实现一个简单Java GUI计算器。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值