Java实战之亲戚关系计算器(swing版)(3)——界面设计

本节将实现该项目的主界面。

首先使用IDEA创建一个普通的Java项目,然后创建map、util、view三个包。

如下:

在view包下创建一个Frame.java类,类内容如下:

package view;

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

public class Frame extends JFrame implements ActionListener {
    private JPanel relationPanel, nameButtonPanel, countButtonPanel, resultPanel;
    private JLabel relationLabel, resultLabel;
    private JTextArea inputRelationTextArea, outputResultTextArea;
    private JButton fatherButton, motherButton, husbandButton, wifeButton, sonButton, daughterButton, bigBrotherButton, smallBrotherButton, bigSisterButton, smallSisterButton, countButton, undoButton, clearButton;

    public Frame() {
        setTitle("亲属关系称谓计算查询");
        setBounds(200, 200, 750, 700);
        setLayout(new GridLayout(4, 1));

        relationPanel = new JPanel();
        relationPanel.setLayout(new BorderLayout());
        relationPanel.setBorder(BorderFactory.createEmptyBorder(5, 5, 5, 5));
        relationLabel = new JLabel("关系:");
        inputRelationTextArea = new JTextArea(5, 20);
        relationPanel.add(relationLabel, BorderLayout.NORTH);
        relationPanel.add(inputRelationTextArea, BorderLayout.CENTER);
        add(relationPanel);

        nameButtonPanel = new JPanel();
        nameButtonPanel.setLayout(new FlowLayout());
        nameButtonPanel.setBorder(BorderFactory.createEmptyBorder(5, 5, 5, 5));
        fatherButton = new JButton("父");
        motherButton = new JButton("母");
        husbandButton = new JButton("夫");
        wifeButton = new JButton("妻");
        sonButton = new JButton("子");
        daughterButton = new JButton("女");
        bigBrotherButton = new JButton("兄");
        smallBrotherButton = new JButton("弟");
        bigSisterButton = new JButton("姐");
        smallSisterButton = new JButton("妹");
        nameButtonPanel.add(fatherButton);
        nameButtonPanel.add(motherButton);
        nameButtonPanel.add(husbandButton);
        nameButtonPanel.add(wifeButton);
        nameButtonPanel.add(sonButton);
        nameButtonPanel.add(daughterButton);
        nameButtonPanel.add(bigBrotherButton);
        nameButtonPanel.add(smallBrotherButton);
        nameButtonPanel.add(bigSisterButton);
        nameButtonPanel.add(smallSisterButton);
        add(nameButtonPanel);

        fatherButton.addActionListener(this);
        motherButton.addActionListener(this);
        husbandButton.addActionListener(this);
        wifeButton.addActionListener(this);
        sonButton.addActionListener(this);
        daughterButton.addActionListener(this);
        bigBrotherButton.addActionListener(this);
        smallBrotherButton.addActionListener(this);
        bigSisterButton.addActionListener(this);
        smallSisterButton.addActionListener(this);

        countButtonPanel = null;
        countButtonPanel = new JPanel();
        countButtonPanel.setLayout(new FlowLayout());
        countButtonPanel.setBorder(BorderFactory.createEmptyBorder(5, 5, 5, 5));
        countButton = new JButton("计算");
        undoButton = new JButton("回退");
        clearButton = new JButton("清空");
        countButtonPanel.add(countButton);
        countButtonPanel.add(undoButton);
        countButtonPanel.add(clearButton);
        add(countButtonPanel);

        resultPanel = new JPanel();
        resultPanel.setLayout(new BorderLayout());
        resultPanel.setBorder(BorderFactory.createEmptyBorder(5, 5, 5, 5));
        resultLabel = new JLabel("计算结果:");
        outputResultTextArea = new JTextArea(5, 20);
        outputResultTextArea.setEditable(false);
        resultPanel.add(resultLabel, BorderLayout.NORTH);
        resultPanel.add(outputResultTextArea, BorderLayout.CENTER);
        add(resultPanel);

        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    }

    @Override
    public void actionPerformed(ActionEvent e) {

    }
}

然后在src目录下创建一个MainApp.java类。

内容如下:

import view.Frame;

/**
 * 启动类
 *
 * @author lck100
 */
public class MainApp {
    public static void main(String[] args) {
        new Frame().setVisible(true);
    }
}

启动MainApp的main方法。

运行界面如下:

 

 

可搜索微信公众号【Java实例程序】或者扫描下方二维码关注公众号获取更多。

注意:在公众号后台回复【20191109】可获取本节源码。

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值