Java象类实现计算景区门票(包含GUI相关窗体组件、文本以及按钮)
程序员文章站
2022-07-10 18:41:41
1.Problem Description需要为一个景区实现计算景区门票的程序,已知成年人的门票价格是100元,儿童票打3折,老年票打5折。使用抽象类来为任意多张不同类型的票计算总价。其UML类图如下所示。在这个UML类图中,最上方的是一个抽象类Ticket,其中包含两个构造方法的重载,以及相应的set、get方法,而下方有三个符号(一条实线+一个空心三角形),这个符号叫:泛化,通俗的说,就是继承。下方的三个类(YoungTicket、ChildTicket和OldTicket 均为非抽....
1.Problem Description
需要为一个景区实现计算景区门票的程序,已知成年人的门票价格是100元,儿童票打3折,老年票打5折。使用抽象类来为任意多张不同类型的票计算总价。其UML类图如下所示。
在这个UML类图中,最上方的是一个抽象类Ticket,其中包含两个构造方法的重载,以及相应的set、get方法,而下方有三个符号(一条实线+一个空心三角形),这个符号叫:泛化,通俗的说,就是继承。下方的三个类(YoungTicket、ChildTicket和OldTicket 均为非抽象子类)分别继承了抽象父类Ticket,所以他们这三个类必须重写抽象父类中的抽象方法,也就是 float singlePrice() 这个方法。
程序最终实现后的运行结果应该是下图这样:????????????
2.Code
2.1 Ticket类:标准票价
package cn.edu.shengda;
public abstract class Ticket {
private float price;
public Ticket() {
this.price=100;
}
public Ticket(float price) {
this.price=price;
}
public float getPrice() {
return price;
}
public abstract float singlePrice();
}
2.2 YoungTicket类:成年票
package cn.edu.shengda;
public class YoungTicket extends Ticket {
public float singlePrice() {
return this.getPrice();
}
public YoungTicket() {
}
public YoungTicket(float price) {
super(price);
}
}
2.3 OldTicket类:老年票
package cn.edu.shengda;
public class OldTicket extends Ticket {
public float singlePrice() {
return (float)(this.getPrice()*0.5);
}
public OldTicket() {
}
public OldTicket(float price) {
super(price);
}
}
2.4 ChildTicket类:儿童票
package cn.edu.shengda;
public class ChildTicket extends Ticket {
public float singlePrice() {
return (float)(this.getPrice()*0.3);
}
public ChildTicket() {
}
public ChildTicket(float price) {
super(price);
}
}
2.5 TicketJFrame类:设置GUI相关的窗体、组件文本内容
package cn.edu.shengda;
import java.awt.EventQueue;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JScrollBar;
import javax.swing.JScrollPane;
import javax.swing.JTextField;
import javax.swing.JTextPane;
public class TicketJFrame extends JFrame {
//此界面只有一个按钮:“计算”按钮
private JButton jButton1;
/*标签文本,用于显示信息,一般情况下不能直接更改其显示的内容
* 这里设置9个,大家在运行之后就可以看到是哪9个标签
*/
private JLabel jLabel1;
private JLabel jLabel2;
private JLabel jLabel3;
private JLabel jLabel4;
private JLabel jLabel5;
private JLabel jLabel6;
private JLabel jLabel7;
private JLabel jLabel8;
private JLabel jLabel9;
//JScrollPane和JScrollBar主要用于滚动窗口的设置
private JScrollBar jScrollBar1;
private JScrollPane jScrollPane1;
private JScrollPane jScrollPane3;
private JScrollPane jScrollPane4;
private JScrollPane jScrollPane5;
//文本框,它只能接收单行文本的输入
private JTextField jTextField1;
//可以在其中插入相关文字数字
private JTextPane jTextPane1;
private JTextPane jTextPane2;
private JTextPane jTextPane3;
private JTextPane jTextPane4;
public TicketJFrame() {
initComponents();
}
private void initComponents() {
jScrollBar1=new JScrollBar();
jScrollPane1=new JScrollPane();
jTextPane1=new JTextPane();
jScrollPane3=new JScrollPane();
jTextPane2=new JTextPane();
jScrollPane4=new JScrollPane();
jTextPane3=new JTextPane();
jScrollPane5=new JScrollPane();
jTextPane4=new JTextPane();
jButton1=new JButton();
jButton1.addActionListener(new ActionListener() {//监听动作事件
public void actionPerformed(ActionEvent e) {
//getText()方法获取文本组件中的所有内容
//成年票
String YoungNumber=jTextPane1.getText();
int a=Integer.parseInt(YoungNumber);
//老年票
String OldNumber=jTextPane2.getText();
int b=Integer.parseInt(OldNumber);
//儿童票
String ChildNumber=jTextPane3.getText();
int c=Integer.parseInt(ChildNumber);
YoungTicket Ticket1=new YoungTicket();
OldTicket Ticket2=new OldTicket();
ChildTicket Ticket3=new ChildTicket();
float TotalPrice=Ticket1.singlePrice()*a + Ticket2.singlePrice()*b + Ticket3.singlePrice()*c;
String ans=Float.toString(TotalPrice);
//计算出总票价之后,通过setText()方法将结果存入文本组件
jTextPane4.setText(ans);
}
});
//创建9个无图像并且其标题为空字符串的JLabel对象
jLabel1=new JLabel();
jLabel2=new JLabel();
jLabel3=new JLabel();
jLabel4=new JLabel();
jLabel5=new JLabel();
jLabel6=new JLabel();
jLabel7=new JLabel();
jLabel8=new JLabel();
jLabel9=new JLabel();
//文本框,它只能接收单行文本的输入
jTextField1=new JTextField();
//设置关闭窗口时的默认操作
setDefaultCloseOperation(javax.swing.JFrame.EXIT_ON_CLOSE);
//将文本内容存入相应的滚动面板组件
jScrollPane1.setViewportView(jTextPane1);
jScrollPane3.setViewportView(jTextPane2);
jScrollPane4.setViewportView(jTextPane3);
jScrollPane5.setViewportView(jTextPane4);
//通过setText()方法设置相关组件的文本
jButton1.setText("计算");
jLabel1.setText("张");
jLabel2.setText("张");
jLabel3.setText("张");
jLabel4.setText("元");
jLabel5.setText("成年票:");
jLabel6.setText("门票标准单价:");
jLabel7.setText("老年票:");
jLabel8.setText("儿童票:");
jLabel9.setText("总价:");
//门票标准单价最初编辑设置为100,也可以进行改变
jTextField1.setText("100");
javax.swing.GroupLayout layout=new javax.swing.GroupLayout(getContentPane());
getContentPane().setLayout(layout);
//按照水平来确定组件位置
layout.setHorizontalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addGap(53,53,53)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(jLabel6)
.addComponent(jLabel5,javax.swing.GroupLayout.PREFERRED_SIZE,222,javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(jLabel8)
.addComponent(jLabel7,javax.swing.GroupLayout.PREFERRED_SIZE,42,javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(jLabel9))
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED,6,Short.MAX_VALUE)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING,false)
.addComponent(jScrollPane5,javax.swing.GroupLayout.Alignment.LEADING,javax.swing.GroupLayout.DEFAULT_SIZE, 59,Short.MAX_VALUE)
.addComponent(jScrollPane4,javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(jScrollPane3,javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(jScrollPane1,javax.swing.GroupLayout.Alignment.LEADING))
.addGap(18,18,18)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(jLabel1)
.addComponent(jLabel2)
.addComponent(jLabel3)
.addComponent(jLabel4)))
.addComponent(jTextField1,javax.swing.GroupLayout.PREFERRED_SIZE,79,javax.swing.GroupLayout.PREFERRED_SIZE))
.addGap(108,108,108))
.addGroup(layout.createSequentialGroup()
.addComponent(jButton1)
.addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE,Short.MAX_VALUE))))
);
//按照垂直来确定组件位置
layout.setVerticalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addGap(48,48,48)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(jLabel6)
.addComponent(jTextField1,javax.swing.GroupLayout.PREFERRED_SIZE,javax.swing.GroupLayout.DEFAULT_SIZE,javax.swing.GroupLayout.PREFERRED_SIZE))
.addGap(40,40,40)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
.addComponent(jScrollPane1)
.addComponent(jLabel1,javax.swing.GroupLayout.DEFAULT_SIZE,javax.swing.GroupLayout.DEFAULT_SIZE,Short.MAX_VALUE))
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(jScrollPane3,javax.swing.GroupLayout.PREFERRED_SIZE,javax.swing.GroupLayout.DEFAULT_SIZE,javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(jLabel2,javax.swing.GroupLayout.DEFAULT_SIZE,javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)))
.addGroup(layout.createSequentialGroup()
.addComponent(jLabel5,javax.swing.GroupLayout.DEFAULT_SIZE,16,Short.MAX_VALUE)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
.addComponent(jLabel7,javax.swing.GroupLayout.PREFERRED_SIZE,31,javax.swing.GroupLayout.PREFERRED_SIZE)))
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(jLabel3, javax.swing.GroupLayout.DEFAULT_SIZE,25,Short.MAX_VALUE)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
.addComponent(jLabel8)
.addComponent(jScrollPane4, javax.swing.GroupLayout.PREFERRED_SIZE,javax.swing.GroupLayout.DEFAULT_SIZE,javax.swing.GroupLayout.PREFERRED_SIZE)))
.addGap(18,18,18)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
.addComponent(jScrollPane5,javax.swing.GroupLayout.PREFERRED_SIZE,javax.swing.GroupLayout.DEFAULT_SIZE,javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(jLabel9)))
.addGroup(layout.createSequentialGroup()
.addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE,Short.MAX_VALUE)
.addComponent(jLabel4)))
.addGap(37,37,37)
.addComponent(jButton1)
.addContainerGap())
);
pack();
}
}
2.6 Main主类:Java程序运行入口
/**
* @author 宋子浩(张起灵-小哥)
* 2020.10.24 程序员日
*/
package cn.edu.shengda;
import cn.edu.shengda.TicketJFrame;
import java.awt.EventQueue;
public class Main {
public static void main(String[] args) {
/* invokeLater()方法
* 导致Runnable中的run()方法在the system EventQueue指派的线程中被调用
*/
EventQueue.invokeLater(new Runnable() {
@Override
public void run() {
//将JFrame容器窗口设置为可见
new TicketJFrame().setVisible(true);
}
});
}
}
2.7 程序运行结果如下:
最后需要说明一点:TicketJFrame类中的所有代码内容,并不是我独自完成的,而是参考了网上有些大佬的写法、思路,以及相关学习资料,最后整合出来的完整类。在此诚挚的感谢大佬们提供的帮助!!!????????????
本文地址:https://blog.csdn.net/weixin_43823808/article/details/109257676
下一篇: Swagger入门以及404解决