| 1 | 1/1 | 返回列表 |
| 查看: 291 | 回復(fù): 0 | ||
尹……《口》新蟲 (初入文壇)
|
[求助]
本人編的程序,實現(xiàn)定時播放音樂嗎,但執(zhí)行時窗口出現(xiàn)兩次,應(yīng)該怎樣修改代碼?
|
|
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 { // 設(shè)置界面的外觀,為系統(tǒng)外觀 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,"您成功設(shè)定了時間"); 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() { //一分鐘執(zhí)行一次 timer.scheduleAtFixedRate(this, 1000*s.getResult(), 1000*s.getResult()); } } 非常感謝! |
| 1 | 1/1 | 返回列表 |
| 最具人氣熱帖推薦 [查看全部] | 作者 | 回/看 | 最后發(fā)表 | |
|---|---|---|---|---|
|
[考研] 085701環(huán)境工程求調(diào)劑 +11 | 多久上課 2026-03-27 | 12/600 |
|
|---|---|---|---|---|
|
[考研] 285求調(diào)劑 +6 | AZMK 2026-03-29 | 9/450 |
|
|
[考研] 材料工程專碩求調(diào)劑 +8 | hyl3153942 2026-03-29 | 8/400 |
|
|
[考研] 286分調(diào)劑 +10 | Faune 2026-03-30 | 11/550 |
|
|
[考研] 26考研-291分-廈門大學(xué)(085601)-柔性電子學(xué)院材料工程專業(yè)求調(diào)劑 +5 | min3 2026-03-24 | 6/300 |
|
|
[考研] 286求調(diào)劑 +5 | Faune 2026-03-30 | 5/250 |
|
|
[考研] 342求調(diào)劑 +4 | 加油a李zs 2026-03-26 | 4/200 |
|
|
[考研] 環(huán)境科學(xué)與工程334分求調(diào)劑 +6 | 王一一依依 2026-03-30 | 8/400 |
|
|
[考研] 一志愿北京理工大學(xué)本科211材料工程294求調(diào)劑 +8 | mikasa的圍巾 2026-03-28 | 8/400 |
|
|
[考研] 356求調(diào)劑 +3 | gysy?s?a 2026-03-28 | 3/150 |
|
|
[考研] 數(shù)一英一271專碩(085401)求調(diào)劑,可跨 +7 | 前行必有光 2026-03-28 | 8/400 |
|
|
[考研] 316求調(diào)劑 +7 | 江辭666 2026-03-26 | 7/350 |
|
|
[考研] 求調(diào)劑 +7 | 爭取九點睡 2026-03-28 | 8/400 |
|
|
[考研] 311(085601)求調(diào)劑 +4 | liziyeyeye 2026-03-28 | 4/200 |
|
|
[考研] 339求調(diào)劑,想調(diào)回江蘇 +6 | 烤麥芽 2026-03-27 | 8/400 |
|
|
[考研] 求調(diào)劑推薦 材料 304 +15 | 荷包蛋hyj 2026-03-26 | 15/750 |
|
|
[考研] 265求調(diào)劑11408 +3 | 劉小鹿lu 2026-03-27 | 3/150 |
|
|
[考研] 341求調(diào)劑 +7 | 青檸檬1 2026-03-26 | 7/350 |
|
|
[考研] 085601求調(diào)劑總分293英一數(shù)二 +4 | 鋼鐵大炮 2026-03-24 | 4/200 |
|
|
[考研] 一志愿武理085500機(jī)械專業(yè)總分300求調(diào)劑 +3 | an10101 2026-03-24 | 7/350 |
|