图形和事件驱动程序 -------- 可以计算长方形周长和面积的图形化用户界面(GUI)(第三篇)

我们不用窗口来运行自己的代码,只是一个很单调的界面,把自己的代码结合到窗口上就可以变成一个可以使用使用程序,我们先来制作一个最简易的计算矩形面积的和周长的小窗口程序。
先来主函数:

package com.li;

public class TextOblongGUI {

    public static void main(String[] args) {
        // TODO 自动生成的方法存根
        new OblongGUI();
    }

}

再来看一看Oblong()类的代码(这就是我们平时直接编好,然后在main中调用这个方法就可以了,加上窗口方面的代码就可以实现在窗口中显示了)

package com.li;

public class Oblong {
    private double length;
    private double height;

    public Oblong(double lengthIn, double heightIn)
    {
        length = lengthIn;
        height = heightIn;
    }

    public void setLength(double lengthIn)
    {
        length = lengthIn;
    }
    public void setHeight(double heigthIn)
    {
        height = heigthIn;
    }

    public double calculateArea()
    {
        return height * length;
    }
    public double calculatePerimeter()
    {
        return 2 * (height + length);
    }
}

下面是OblongGUI()类的代码

package com.li;

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

public class OblongGUI extends JFrame implements ActionListener {

    /**
     * 
     */
    private static final long serialVersionUID = 1L;

    private Oblong myOblong = new Oblong(0,0);

    private JLabel lengthLabel = new JLabel("Length");
    private JTextField lengthField = new JTextField(5);
    private JLabel heightLabel = new JLabel("Height");
    private JTextField heightField = new JTextField(5);
    private JButton calcButton = new  
                                    JButton("Calculate");
    private JTextArea displayArea = new JTextArea(2,20);

    public OblongGUI()
    {
        setTitle("Oblong GUI");
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setLayout(new FlowLayout());

        add(lengthLabel);
        add(lengthField);
        add(heightLabel);
        add(heightField);
        add(calcButton);
        add(displayArea);
        setSize(240,135);
        setLocation(300,300);

        calcButton.addActionListener(this);

        setVisible(true);
    }
    @Override
    public void actionPerformed(ActionEvent e) {
        // TODO 自动生成的方法存根
        String lengthEntered = lengthField.getText();
        String heightEntered = heightField.getText();

        if(lengthEntered.length() == 0 || 
                         heightEntered.length() == 0)
        {
            displayArea.setText("Length and height must 
                                          be entered");
        }
        else
        {

myOblong.setLength(Double.parseDouble(lengthEntered));
            myOblong.setHeight(Double.parseDouble(heightEntered));
displayArea.setText("The area of the oblong is"
                         +myOblong.calculateArea()
                         + "\n"
                         + "The perimeter of the 
                         + oblong is"
                         +myOblong.calculatePerimeter());
        }
    }
}

效果图
这里写图片描述

我们来逐一分析一下代码
对于Oblong()类中的代码就没有必要过多解释了
对于OblongGUI()类中的代码:

    private Oblong myOblong = new Oblong(0,0);

    private JLabel lengthLabel = new JLabel("Length");
    private JTextField lengthField = new JTextField(5);
    private JLabel heightLabel = new JLabel("Height");
    private JTextField heightField = new JTextField(5);
    private JButton calcButton = new 
                                  JButton("Calculate");
    private JTextArea displayArea = new JTextArea(2,20);

第一行是对Oblong()类进行实例化

JLayout类是一个可以用来显示文本的,实例化的对象直接以文本的方式显示在窗口上显示出来

JTextField类包含一个构造函数,可以接受一个整数作为参数,以列为单位,输入的整数表示列数,即可以用于用户输入的文本域的长度

JButton就是用来添加按钮的,我们在第二篇就已经谈过了

JTextArea是一个可以在窗口中显示一个大块文本域,在这里的文本域可以用来显示矩形的周长和面积的计算结果。其中的两个数字分别表示文本域的行和列。

public OblongGUI()
    {
        setTitle("Oblong GUI");
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setLayout(new FlowLayout());

        add(lengthLabel);
        add(lengthField);
        add(heightLabel);
        add(heightField);
        add(calcButton);
        add(displayArea);
        setSize(240,135);
        setLocation(300,300);

        calcButton.addActionListener(this);

        setVisible(true);
    }

对于这段代码,以前有很详细的解释,对此就不进行详细的阐述,而对于calcButton.addActionListener(this)表示的是给calcButton按钮添加一个监视器,当按钮被按下的时候可以激发相关的操作。

add()是把上面的那些组件添加到窗口里。

@Override
    public void actionPerformed(ActionEvent e) {
        // TODO 自动生成的方法存根
        String lengthEntered = lengthField.getText();
        String heightEntered = heightField.getText();

        if(lengthEntered.length() == 0 || 
                       heightEntered.length() == 0)
        {
            displayArea.setText("Length and height must 
                                        be entered");
        }
        else
        {
            myOblong.setLength(Double.parseDouble(lengthEntered));
            myOblong.setHeight(Double.parseDouble(heightEntered));
displayArea.setText("The area of the oblong is"
                       + myOblong.calculateArea()
                       + "\n"
                       + "The perimeter of the oblong is"
                       + myOblong.calculatePerimeter());
        }
    }

声明的两个变量lenghtEntered和heightEntered用于接受用户输入的值,这些值通过TextField类中的getText方法读入(以String的形式),然后是检查输入的结果是否小于0.
最后是调用Oblong中自己定义的一些方法进行相关的计算。
这里的Double.parseDouble()可以把一个字符串形变量转换成double形的。

这里再介绍一下怎么把普通的变量转换成String型
看程序:

double d = 1.3;
int i = 5;
String = "" + i + d;

通过“”就可以把一些非String类型的变量转换成String型。

  • 5
    点赞
  • 13
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
用java写GUI图形界面 public class login extends JFrame { private JComboBox nameJComboBox; private JPanel userJPanel; private JLabel pictureJLabel; private JButton okJButton,cancelJButton; private JLabel nameJLabel,passwordJLabel,note; private JPasswordField passwordJPasswordField; private String name1; private String password1; private String user; private ImageIcon myImageIcon; public login( ) { createUserInterface(); // 调用创建用户界面方法 } private void createUserInterface() { Container contentPane = getContentPane(); contentPane.setLayout( null ); userJPanel = new JPanel(); userJPanel.setBounds( 35, 120, 300, 96 ); userJPanel.setBorder(BorderFactory.createEtchedBorder() ); //显示一圈边儿 userJPanel.setLayout( null ); contentPane.add( userJPanel ); nameJComboBox = new JComboBox(); nameJComboBox.setBounds( 100, 12, 170, 25 ); nameJComboBox.addItem( "admin" ); nameJComboBox.addItem( "aloie" ); nameJComboBox.setSelectedIndex( 0 ); nameJComboBox.setEditable(true); userJPanel.add( nameJComboBox ); pictureJLabel=new JLabel(); pictureJLabel.setBounds(45,0,380,118); pictureJLabel.setIcon(new ImageIcon("pic.gif")); contentPane.add(pictureJLabel); nameJLabel=new JLabel("姓 名:"); nameJLabel.setBounds(20,12,80,25); userJPanel.add(nameJLabel); passwordJPasswordField=new JPasswordField(); passwordJPasswordField.setBounds(100,60,170,25); userJPanel.add(passwordJPasswordField); passwordJLabel=new JLabel("密 码:"); passwordJLabel.setBounds(20,60,80,25); userJPanel.add(passwordJLabel); note=new JLabel("密码与用户名相同"); note.setBounds(0,295,180,25); add(note); okJButton=new JButton("登 陆"); okJButton.setBounds(60,250,80,25); contentPane.add(okJButton); okJButton.addActionListener( new ActionListener() { public void actionPerformed(ActionEvent event) { okJButtonActionPerformed(event); } } ); cancelJButton=new JButton("取 消"); cancelJButton.setBounds(210,250,80,25); contentPane.add(cancelJButton); cancelJButton.addActionListener( new ActionListener() { public void actionPerformed( ActionEvent event ) { System.exit(0); //退出登陆 } } ); setTitle( "登陆窗口" ); setSize( 380, 350 ); setResizable( false ); //将最大化按钮设置为不可用 } private void okJButtonActionPerformed( ActionEvent event ) { //okJButton响应事件,检查用户名和密码的匹配 name1= nameJComboBox.getSelectedItem().toString(); if (name1.equals("admin") ) { if (passwordJPasswordField.getText().equals("admin")) { showNewWindow(); setVisible( false); } else { JOptionPane.showMessageDialog( this,"密码错误,拒绝登陆", "密码错误 !", JOptionPane.ERROR_MESSAGE ); } } else if (name1.equals("aloie")) { if ( passwordJPasswordField.getText().equals("aloie") ) { showNewWindow(); setVisible(false); } else { JOptionPane.showMessageDialog( this,"密码错误,拒绝登陆", "密码错误 !", JOptionPane.ERROR_MESSAGE ); } } } public void showNewWindow() { JFrame jf=new JFrame("main Frame"); jf.setSize(500,400); jf.setVisible(true); jf.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE ); } public static void main( String[] args ) { JFrame.setDefaultLookAndFeelDecorated(true); login mylogin = new login( ); mylogin.setVisible( true ); mylogin.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE ); } }

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值