24小时热门版块排行榜    

Znn3bq.jpeg
查看: 293  |  回复: 0

尹……《口》

新虫 (初入文坛)

[求助] 本人编的程序,实现定时播放音乐吗,但执行时窗口出现两次,应该怎样修改代码?

package frame;


import java.awt.*;
import java.awt.event.*;
import java.io.IOException;

import javax.swing.*;   

public class boyin extends JFrame implements ActionListener{  
        JMenuBar chuangkou=new JMenuBar();
        JMenu jMenu1= new JMenu("文件");
        JMenu jMenu2= new JMenu("定时");
        JMenu jMenu3= new JMenu("播放器");
        JMenu jMenu4= new JMenu("帮助");
        JMenuItem item1 = new JMenuItem("启动");
        JMenuItem item2 = new JMenuItem("音乐");
        JMenuItem item3 = new JMenuItem("视频");
        JMenuItem item4 = new JMenuItem("Window Media Player");
        JMenuItem item5 = new JMenuItem("自定义");
        public boyin()     {  
                setLayout(new BorderLayout(5,10));
                setBackground(Color.darkGray);;
                chuangkou.add(jMenu1);
                chuangkou.add(jMenu2);
                chuangkou.add(jMenu3);
                chuangkou.add(jMenu4);
                setJMenuBar(chuangkou);
                jMenu2.add(item1);
                jMenu1.add(item2);
                jMenu1.add(item3);
                jMenu3.add(item4);
                jMenu3.add(item5);
                item1.addActionListener(new ActionListener()            
                {                 
                        public void actionPerformed(ActionEvent event)                 
                        {                     
                                new frame();                 
                                }            
                        });
                jMenu4.addActionListener(this);
                item2.addActionListener(this);
                item3.addActionListener(this);
                item4.addActionListener(this);
                item5.addActionListener(this);        
                setTitle("主窗口");         
                setSize(400, 300);
                setLayout(new FlowLayout());
                setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);         
                setLocationByPlatform(true);         
                setVisible(true);     
                }           
        public void actionPerformed(ActionEvent e) {
                // TODO Auto-generated method stub
               
        }
        public static void main(String[] args)     {  
                boyin f=new boyin();
                try {
                        // 设置界面的外观,为系统外观
                                     UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
                                      SwingUtilities.updateComponentTreeUI(f);
                                      } catch (Exception e) {
                                      e.printStackTrace();
                                       }
                                       }
        }   
class frame extends JFrame implements ActionListener{
        public String name;
        private static int result;
        public static int minute;
        public static String command;
        public static String command1;
        String currentDirectory;
        FileDialog fileDialog = new FileDialog(this, "打开媒体文件", FileDialog.LOAD);
        FileDialog fileDialog1 = new FileDialog(this, "打开媒体文件", FileDialog.LOAD);
        Font f1=new Font("宋体",Font.BOLD,18);
        Font f2=new Font("宋体",Font.BOLD,25);
        JPanel p1=new JPanel(new FlowLayout(FlowLayout.CENTER,2,2));
        JLabel l=new JLabel("欢迎使用定时软件");
        JLabel l1=new JLabel("播放工具");
        JLabel l2=new JLabel("文件选择");
        JLabel l3=new JLabel("启动时间");
        JTextField w1=new JTextField(8);
        JTextField w2=new JTextField(8);
        JTextField w3=new JTextField(8);
        JButton x=new JButton("选择");
        JButton x2=new JButton("选择");
        JButton x3=new JButton("输入");
        JButton x4=new JButton("运行");
        public frame(){
                p1.add(l);
                p1.add(l1);
                p1.add(w1);
                p1.add(x);
                p1.add(l2);
                p1.add(w2);
                p1.add(x2);
                p1.add(l3);
                p1.add(w3);
                p1.add(x3);
                p1.add(x4);
                p1.setLayout(null);
                p1.setBackground(Color.darkGray);
                add(p1);
                l.setBounds(100, 20, 300, 30);
                l.setFont(f2);
                l.setForeground(Color.cyan);
                l1.setBounds(50, 80, 100, 15);
                l1.setFont(f1);
                l1.setForeground(Color.orange);
                w1.setBounds(150, 70, 100, 30);
                w1.setFont(f1);
                x.setBounds(260, 70, 80, 30);
                x.setFont(f1);
            x.setForeground(Color.red);
                l2.setBounds(50, 130, 100, 15);
                l2.setFont(f1);
                l2.setForeground(Color.orange);
                w2.setBounds(150, 120, 100, 30);
                w2.setFont(f1);
                x2.setBounds(260, 120, 80, 30);
                x2.setFont(f1);
                x2.setForeground(Color.red);
                l3.setBounds(50, 180, 100, 15);
                l3.setFont(f1);
                l3.setForeground(Color.orange);
                w3.setBounds(150, 170, 100, 30);
                w3.setFont(f1);
                x3.setBounds(260, 170, 80, 30);
                x3.setFont(f1);
                x3.setForeground(Color.red);
                x4.setBounds(260, 220, 80, 30);
                x4.setFont(f1);
                x4.setForeground(Color.red);
                x.addActionListener(this);
                x2.addActionListener(this);
                x3.addActionListener(this);
                x4.addActionListener(this);
                setTitle("定时");         
                setSize(400, 300);         
                setLocationRelativeTo(null);
                setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                setVisible(true);   
                }
        public void actionPerformed(ActionEvent e) {
                // TODO Auto-generated method stub
                Object source = e.getSource();
                if(source==x){
                        x();
               w1.setText(command);
                }
                if(source==x2){
                        x2();
                        w2.setText(command1);
                }
                if(source==x3){
                        x3();
                        w3.setText(name);
                }
                if(source==x4){
                        dingshi t=new dingshi();
                        JOptionPane.showMessageDialog(null,"您成功设定了时间");
                        t.start();
                }
        }
        public String x(){
                FileDialog fileDialog = new FileDialog(this, "打开媒体文件", FileDialog.LOAD);
        fileDialog.setDirectory(currentDirectory);
        fileDialog.setVisible(true);
        command =fileDialog.getDirectory().replace("\\\\","/")
                           +fileDialog.getFile().replace("\\\\","/");
         return command;
        }
        public String x2() {
                // TODO Auto-generated method stub
                FileDialog fileDialog1 = new FileDialog(this, "打开媒体文件", FileDialog.LOAD);
        fileDialog1.setDirectory(currentDirectory);
        fileDialog1.setVisible(true);
        command1 =fileDialog1.getDirectory().replace("\\\\","/")
                           +fileDialog1.getFile().replace("\\\\","/");
         return command1;
        }
        public int x3() {
                // TODO Auto-generated method stub
                String resultString = JOptionPane.showInputDialog
                                ("请输入启动时间:");
                result = Integer.parseInt(resultString);
                name=Integer.toString(result);
                return result;
        }
        public int getResult() {
                return result;
        }
        public void player() {
                // TODO Auto-generated method stub
                try {
                        String player=command+" "+command1;
                        Runtime r = Runtime.getRuntime();  
                        r.exec(player);  
                        } catch (IOException e) {  
                        // TODO Auto-generated catch block   
                        e.printStackTrace();  
                        }
    }
}
下一段代码
package frame;


import java.util.*;

public class dingshi extends TimerTask{
        /**
         * @param args
         */
        frame s=new frame();
        Timer timer = new Timer();
        public void run(){
          //必须要有run方法
                s.player();
    }
        public void start()
        {
                        //一分钟执行一次
                timer.scheduleAtFixedRate(this, 1000*s.getResult(), 1000*s.getResult());
        }
}
非常感谢!

» 猜你喜欢

已阅   关注TA 给TA发消息 送TA红花 TA的回帖
相关版块跳转 我要订阅楼主 学员iPd8Fl 的主题更新
最具人气热帖推荐 [查看全部] 作者 回/看 最后发表
[考研] 求调剂 +16 张zic 2026-04-05 17/850 2026-04-12 11:01 by zhouwenxian
[考研] 316求调剂 +5 想读研究生( ?∵ 2026-04-07 5/250 2026-04-12 00:43 by 蓝云思雨
[考研] 22408调剂315分 +3 zhuangyan123 2026-04-09 3/150 2026-04-12 00:25 by 蓝云思雨
[考研] 0854调剂 +8 长弓傲 2026-04-11 9/450 2026-04-11 21:44 by 蓝云思雨
[考研] 调剂 +10 只叙离别辞 2026-04-09 12/600 2026-04-11 20:57 by 逆水乘风
[考研] 化学308分求调剂 +22 你好明天你好 2026-04-07 24/1200 2026-04-11 11:14 by ChemPharm
[考研] 269电子信息求调剂,可转专业 +11 独酌wl 2026-04-06 11/550 2026-04-11 11:12 by 逆水乘风
[考研] 0854求调剂 +7 assdll 2026-04-05 7/350 2026-04-11 10:34 by Delta2012
[考研] 308求调剂 +21 倘若起风了呢 2026-04-05 21/1050 2026-04-10 08:13 by Sammy2
[考研] 复试调剂,一志愿郑州大学材料与化工289分 +31 硕星赴 2026-04-08 31/1550 2026-04-09 16:54 by Delta2012
[考研] 285求调剂 +12 AZMK 2026-04-05 18/900 2026-04-08 20:43 by 逆水乘风
[考研] 0703调剂,一志愿天津大学319分 +23 haaaabcd 2026-04-05 26/1300 2026-04-08 16:19 by luoyongfeng
[考研] 化学0703-一志愿211-338分求调剂 +10 vants 2026-04-05 11/550 2026-04-08 16:02 by screening
[考研] 一志愿哈工大,初试329,求环境科学与工程调剂! +11 余未辛 2026-04-06 11/550 2026-04-08 15:21 by screening
[考研] 307求调剂 +14 超级伊昂大王 2026-04-06 14/700 2026-04-08 07:03 by 无际的草原
[考研] 318求调剂 +5 李青山山山 2026-04-07 5/250 2026-04-07 18:24 by 蓝云思雨
[考研] (调剂)一志愿报考哈尔滨工业大学0857资源与环境专业378分考生 +7 狠狠加油 2026-04-05 8/400 2026-04-06 16:52 by momo皓
[考研] 377求调剂 +6 by.ovo 2026-04-05 6/300 2026-04-05 22:18 by dongzh2009
[考研] 283求调剂 +5 baiiyu 2026-04-05 6/300 2026-04-05 20:35 by 啵啵啵0119
[考研] 308求调剂 +3 终不似从前 2026-04-05 3/150 2026-04-05 20:07 by 啵啵啵0119
信息提示
请填处理意见