本节将实现该项目的主界面。
首先使用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】可获取本节源码。