24小时热门版块排行榜    

查看: 285  |  回复: 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 的主题更新
信息提示
请填处理意见