创建一个类Main.java
import java.util.*;
import java.awt.*;
import javax.swing.*;
public class Main {
public static void main(String args[]){
new ZhuYeMian();//在堆里面开辟xiaofei的空间
}
}
紧接着到ZhuYeMain.java
import javax.swing.*;
import java.awt.*;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
public class ZhuYeMian extends JFrame implements MouseListener {
JTextField w1 = null;//创建文本框对象
JPasswordField mm1 = null;//密码框
JComboBox xl1 = null;//创建一个下拉列表
JTabbedPane XXK = new JTabbedPane();//用于创建选项卡式的界面布局
JPanel s1=null;
JPanel s2=null;
public ZhuYeMian() {
this.setContentPane(XXK);
YeMian();
XiaoFeiZhe();
GuanLiYuan();
}
public void YeMian() {
this.setTitle("KTV点歌系统");
this.setBounds(100, 200, 600, 600);
this.setVisible(true);
this.setDefaultCloseOperation(3);
}
private void XiaoFeiZhe() {
s1 = new JPanel();
s1.setLayout(new BorderLayout());//中西南北布局
JLabel t1 = new JLabel();
// t1.setText("包厢选择");
JLabel t5 = new JLabel("包厢选择");
t5.setBounds(230, 265, 200, 200);
s1.add(t5);
s1.add(t1);
//添加按钮
JButton an1 = new JButton("开始唱歌");
JButton an2 = new JButton("收费标准");
JButton an3 = new JButton("退出");
an1.setBounds(300, 450, 100, 30);
an2.setBounds(300, 400, 100, 30);
an3.setBounds(300, 500, 100, 30);
t1.add(an2);
t1.add(an3);
t1.add(an1);
an2.addMouseListener(new MouseAdapter() {
@Override
public void mouseClicked(MouseEvent e) {
super.mouseClicked(e);
new 酒水服务();
}
});
//退出
an3.addMouseListener(new MouseAdapter() {
@Override
public void mouseClicked(MouseEvent e) {
ZhuYeMian.this.dispose();
}
});
//添加标签下拉列表
xl1 = new JComboBox();
xl1.setBounds(300, 350, 100, 30);
xl1.addItem("玲珑包");
xl1.addItem("小包");
xl1.addItem("中包");
xl1.addItem("大包");
xl1.addItem("商务包");
t1.add(xl1);
XXK.addTab("消费者", s1);
//添加消费者监听器
an1.addMouseListener(new MouseAdapter() {
@Override
public void mouseClicked(MouseEvent e) {
new kaishichangge();
}
});
}
private void GuanLiYuan() {
s2 = new JPanel();
s2.setLayout(new BorderLayout());
JLabel t2 = new JLabel();
//添加标签改变字体
JLabel t0 = new JLabel("管理员登录");
t0.setBounds(260, 140, 150, 100);
t0.setFont(new Font("草书", Font.BOLD, 20));
JLabel t3 = new JLabel("用户名");
t3.setBounds(250, 158, 200, 200);
JLabel t4 = new JLabel("密码");
t4.setBounds(250, 208, 200, 200);
//
t2.setBounds(400, 200, 500, 500);
//文本框的创建
w1 = new JTextField();
w1.setBounds(300, 250, 100, 30);
w1.setColumns(20);
//密码框
mm1 = new JPasswordField(10);
mm1.setBounds(300, 300, 100, 30);
mm1.setEchoChar('*');
//添加按钮
JButton an5 = new JButton("登录");
an5.setBounds(295, 380, 100, 30);
//给登录按钮添加监听事件
an5.addMouseListener(new MouseAdapter() {
@Override
public void mouseClicked(MouseEvent e) {
String yhk = w1.getText();
String mmk = mm1.getText();
if ("qxc".equals(yhk) && "123".equals(mmk)) {
JOptionPane.showConfirmDialog(null, "登录成功");//换成关闭按钮
new dengluhou();
ZhuYeMian.this.dispose();
} else {
JOptionPane.showConfirmDialog(null, "密码或者账号错误");
w1.setText("");
mm1.setText("");
}
}
});
//
t2.add(an5);
t2.add(w1);
t2.add(mm1);
s2.add(t4);
s2.add(t3);
s2.add(t0);
s2.add(t2);
XXK.addTab("管理员", s2);
}
@Override
public void mouseClicked(MouseEvent e) {
}
@Override
public void mousePressed(MouseEvent e) {
}
@Override
public void mouseReleased(MouseEvent e) {
}
@Override
public void mouseEntered(MouseEvent e) {
}
@Override
public void mouseExited(MouseEvent e) {
}
}
跳转到开始唱歌类去:kaishichangge.java
import javax.swing.*;
import java.awt.*;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
public class kaishichangge extends JFrame implements MouseListener {
public kaishichangge() {
this.setTitle("欢唱不停");
this.setBounds(100, 100, 800, 800);
this.setDefaultCloseOperation(3);
this.setLayout(null);
this.setVisible(true);
ImageIcon tp=new ImageIcon("C:\\JAVA\\图片\\IMG_20240905_102324.jpg");
JLabel bq=new JLabel(tp);
bq.setBounds(40,2,1200,600);
this.getContentPane().add(bq);//添加到隐藏容器
gongleng();
}
public void gongleng(){
ImageIcon tp1=new ImageIcon("C:\\JAVA\\图片\\acb.jpg");
JLabel bq1=new JLabel(tp1);
bq1.setBounds(540,600,80,80);
JLabel wenzi=new JLabel("分类");
wenzi.setBounds(569,670,80,60);
this.getContentPane().add(wenzi);
this.getContentPane().add(bq1);
//
JButton al=new JButton("播放歌曲类型");
al.setBounds(530,720,100,30);
this.getContentPane().add(al);
al.addMouseListener(new MouseAdapter() {
@Override
public void mouseClicked(MouseEvent e) {
new 播放歌曲类型();
}
});
}
@Override
public void mouseClicked(MouseEvent e) {
}
@Override
public void mousePressed(MouseEvent e) {
}
@Override
public void mouseReleased(MouseEvent e) {
}
@Override
public void mouseEntered(MouseEvent e) {
}
@Override
public void mouseExited(MouseEvent e) {
}
}
然后就是播放歌曲类型.java
import javax.swing.*;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
public class 播放歌曲类型 extends JFrame implements MouseListener {
public 播放歌曲类型(){
this.setTitle("播放歌曲类型");
this.setBounds(100,100,500,500);
this.setDefaultCloseOperation(3);
this.setLayout(null);
this.setVisible(true);
DJ();
DouYin();
LaoGe();
QingGe();
OuMei();
}
public void DJ(){
JButton anl1=new JButton("DJ");
anl1.setBounds(230, 383, 100, 40);
anl1.addMouseListener(new MouseAdapter() {
@Override
public void mouseClicked(MouseEvent e) {
new DJ音乐();
}
});
this.getContentPane().add(anl1);
}
public void DouYin(){
JButton anl2=new JButton("DouYin");
anl2.setBounds(340, 383, 100, 40);
anl2.addMouseListener(new MouseAdapter() {
@Override
public void mouseClicked(MouseEvent e) {
new DouYin音乐();
}
});
this.getContentPane().add(anl2);
}
public void LaoGe(){
JButton anl3=new JButton("LaoGe");
anl3.setBounds(440, 383, 100, 40);
anl3.addMouseListener(new MouseAdapter() {
@Override
public void mouseClicked(MouseEvent e) {
new 老音乐();
}
});
this.getContentPane().add(anl3);
}
public void QingGe(){
JButton anl4=new JButton("QingGe");
anl4.setBounds(550, 383, 100, 40);
anl4.addMouseListener(new MouseAdapter() {
@Override
public void mouseClicked(MouseEvent e) {
}
});
this.getContentPane().add(anl4);
}
public void OuMei(){
JButton anl5=new JButton("OuMei");
anl5.setBounds(550, 383, 100, 40);
anl5.addMouseListener(new MouseAdapter() {
@Override
public void mouseClicked(MouseEvent e) {
new OuMei音乐();
}
});
this.getContentPane().add(anl5);
}
@Override
public void mouseClicked(MouseEvent e) {
}
@Override
public void mousePressed(MouseEvent e) {
}
@Override
public void mouseReleased(MouseEvent e) {
}
@Override
public void mouseEntered(MouseEvent e) {
}
@Override
public void mouseExited(MouseEvent e) {
}
}
以抖音歌曲为例子,DouYin.java
import javax.sound.sampled.AudioInputStream;
import javax.sound.sampled.AudioSystem;
import javax.sound.sampled.Clip;
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.ItemEvent;
import java.io.File;
public class DouYin音乐 extends JDialog implements ActionListener {
JComboBox<String>choiceMusic;//创建一个字符串类型数据下拉列表框对象
Clip clip;//对引诱播放进行保护,在播放时任然可以点击其他东西
AudioInputStream srl;
File yywj;
JButton bfpaly,bfloop,bfstop;
JProgressBar jdt=new JProgressBar();//创建进度条对象
DouYin音乐(){
try {
clip= AudioSystem.getClip();//获取clip对象,播放音乐音频片段
}
catch (Exception exp){}
choiceMusic =new JComboBox<String>();//创建一个音乐列表
choiceMusic.addItem("选择歌曲");
choiceMusic.addItem("C:\\JAVA\\音乐\\张紫豪 - 可不可以.wav");
choiceMusic.addItem("C:\\JAVA\\音乐\\戒烟.wav");
choiceMusic.setSelectedIndex(0);
choiceMusic.addActionListener(this);
bfpaly=new JButton("播放");
bfloop=new JButton("循环");
bfstop=new JButton("停止");
bfpaly.addActionListener(this);
bfstop.addActionListener(this);
bfloop.addActionListener(this);
setLayout(new FlowLayout());
this.add(choiceMusic);
this.add(bfloop);
this.add(bfpaly);
this.add(bfstop);
this.setSize(550,420);
//创建进度条
jdt.setVisible(true);
this.add(jdt);
//
this.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
setVisible(true);
}
public void itemSC(ItemEvent e){
clip.close();
try {
String musicName=choiceMusic.getSelectedItem().toString();//把下拉列表中选择的项,转换为字符串表示音乐名称
yywj=new File(musicName);
clip.open(srl);
}
catch (Exception exp){}
}
public void actionPerformed(ActionEvent e) {
if (e.getSource() == bfpaly) {//触发当前事件源是否与bfplay按钮相等
if (clip.isRunning()) {
try {
clip = AudioSystem.getClip();
srl = AudioSystem.getAudioInputStream(yywj);
clip.open(srl);
} catch (Exception exp) {
JOptionPane.showMessageDialog(this, "播放时出现异常:" + exp.getMessage(), "错误", JOptionPane.ERROR_MESSAGE);
}
}
clip.start();//开始播放
//启动进度条
new Thread(()->{
while (clip.isRunning()){
int currenFrame=clip.getFramePosition();//目前位置帧数
int totalFrame=clip.getFrameLength();//总长
int progress=(int)((double)currenFrame/totalFrame*100);
SwingUtilities.invokeLater(()->{
//进度条更新
// jdt=(JProgressBar)this.getComponents()[this.getComponentCount()-1];
jdt.setValue(progress);
});
try {
Thread.sleep(50);//线程睡眠时间等0.05秒再去看进度
}catch (InterruptedException ex){
ex.printStackTrace();
}
}
}).start();
} else if (e.getSource() == bfloop) {
clip.loop(-1);//循环播放
} else if (e.getSource() == bfstop) {
clip.stop();
} else if (e.getSource() == choiceMusic) {
clip.close();
try {
String musicName = choiceMusic.getSelectedItem().toString();
yywj = new File(musicName);
srl = AudioSystem.getAudioInputStream(yywj);
clip.open(srl);
} catch (Exception exp) {
System.err.println("选择出现异常");
}
}
}
}