24小时热门版块排行榜    

查看: 343  |  回复: 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 的主题更新
最具人气热帖推荐 [查看全部] 作者 回/看 最后发表
[基金申请] 我的“飞艇+直升机”专利中了VFS的TVF 2027,求高校师生带写论文并参会 +3 95217 2026-11-26 3/150 2026-11-28 10:04 by kudofaye
[考研] 售SCI一区文章,我:8.O.55.1.O.54,科目齐全,可伽急 +3 1VHV9UkX7sid 2026-11-26 6/300 2026-11-27 22:00 by Uywcrj8sNxLZ
[硕博家园] 售SCI一区文章,我:8O5.5.1.O5.4,科目全,可伽急 +3 I5Uih4YWUbaa 2026-11-27 3/150 2026-11-27 19:55 by FlRlcTrDsDoO
[教师之家] 售SCI一区T0P文章,我:8.O.55.1.O.54,科目齐全,可+急 +3 p1TifTPkqG3A 2026-11-27 3/150 2026-11-27 19:48 by FlRlcTrDsDoO
[论文投稿] 售SCI文章,我:8O.5.5.1O.54,科目全,可十急 +3 p1TifTPkqG3A 2026-11-27 3/150 2026-11-27 19:20 by XTPLXDTeEwbs
[公派出国] 售SCI一区T0P文章,我:8.O.55.1.O54,科目全,可伽急 +3 kTIKZRt5HnP0 2026-11-27 3/150 2026-11-27 19:05 by XTPLXDTeEwbs
[论文投稿] 售一区SCI文章T0P,我:8O.551.O54,科目全,可十急 +3 kTIKZRt5HnP0 2026-11-27 3/150 2026-11-27 18:58 by XTPLXDTeEwbs
[硕博家园] 售SCI一区T0P文章,我:8.O.55.1.O.5.4,科目全,可+急 +3 J9NGDvAaPB9L 2026-11-26 3/150 2026-11-27 18:16 by XTPLXDTeEwbs
[硕博家园] 售SCI一区T0P文章,我:8.O55.1.O.54,科目全,可十急 +3 J9NGDvAaPB9L 2026-11-26 3/150 2026-11-27 18:14 by XTPLXDTeEwbs
[找工作] 售SCI一区T0P文章,我:8O.55.1.O.54,科目全,可伽急 +5 Gp6vMAD5C53H 2026-11-26 6/300 2026-11-27 17:52 by HaAiYedD27eq
[硕博家园] 售SCI一区文章,我:8O5.5.1.O5.4,科目全,可伽急 +4 Gp6vMAD5C53H 2026-11-26 5/250 2026-11-27 17:40 by HaAiYedD27eq
[硕博家园] 售SCI-T0P文章,我:8O.5.5.1.O.54,科目齐全,可+急 +6 mzU8LdVQMwOf 2026-11-26 7/350 2026-11-27 17:25 by HaAiYedD27eq
[博后之家] 售一区SCI文章T0P,我:8O.551.O54,科目全,可十急 +5 1VHV9UkX7sid 2026-11-26 6/300 2026-11-27 17:18 by HaAiYedD27eq
[论文投稿] 售SCI一区T0P文章,我:8.O.55.1.O.54,科目齐全,可+急 +3 J9NGDvAaPB9L 2026-11-27 3/150 2026-11-27 14:21 by YGs3orGKTyBO
[考博] 售一区SCI文章T0P,我:8O.551.O54,科目全,可十急 +4 N80hyqiqCeT2 2026-11-26 4/200 2026-11-27 13:52 by aTZfRIGzowsS
[硕博家园] 售SCI一区T0P文章,我:8O.55.1.O.5.4,科目齐全,可+急 +3 N80hyqiqCeT2 2026-11-26 4/200 2026-11-27 13:33 by aTZfRIGzowsS
[考博] 售SCI一区T0P文章,我:8O.55.1.O.5.4,科目齐全,可+急 +3 mzU8LdVQMwOf 2026-11-26 3/150 2026-11-27 13:05 by aTZfRIGzowsS
[论文投稿] 售一区SCI文章T0P,我:8O.551.O54,科目全,可十急 +5 1VHV9UkX7sid 2026-11-26 7/350 2026-11-27 12:38 by aTZfRIGzowsS
[文学芳草园] 做梦 +3 myrtle 2026-11-25 3/150 2026-11-27 09:14 by asi19
[考博] ⚽求2027博士自荐 求材料/催化/绿色化学/环境 方向博导 +3 Hutao6 2026-11-23 5/250 2026-11-26 20:29 by greychen00
信息提示
请填处理意见