kanonmoon kanonmoon
关注数: 25 粉丝数: 92 发帖数: 1,082 关注贴吧数: 17
一个播放音乐的程式 求除错 import java.applet.*; import java.awt.*; import java.io.File; import java.net.MalformedURLException; import javax.swing.*; import javax.swing.filechooser.FileNameExtensionFilter; public class Practice5 extends JFrame { private static final long serialVersionUID = 1L; private JPanel jContentPane = null; private JTextField filePath = null; private JButton openFile = null; private JButton playButton = null; private File selectedFile; private AudioClip audioClip; private JTextField getJTextField() { if (filePath == null) { filePath = new JTextField(); filePath.setPreferredSize(new Dimension(200, 22)); filePath.setEditable(false); } return filePath; } // 获取选择文件按钮的方法 private JButton getOpenFile() { if (openFile == null) { openFile = new JButton(); openFile.setText("选择文件"); openFile.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent e) { JFileChooser fileChooser = new JFileChooser(); fileChooser .setFileFilter(new FileNameExtensionFilter( "支持的音频文件(*.mid、*.wav、*.au", "wav", "au", "mp3")); fileChooser.showOpenDialog(Practice5.this); selectedFile = fileChooser.getSelectedFile(); filePath.setText(selectedFile.getAbsolutePath()); } }); } return openFile; } // 获取播放按钮的方法 private JButton getPlayButton() { if (playButton == null) { playButton = new JButton(); playButton.setText("播放"); playButton .addActionListener(new java.awt.event.ActionListener() { public void actionPerformed( java.awt.event.ActionEvent e) { if (selectedFile != null) { try { if (audioClip != null) audioClip.stop(); audioClip = Applet .newAudioClip(selectedFile .toURI().toURL()); audioClip.play(); } catch (MalformedURLException e1) { e1.printStackTrace(); } } } }); } return playButton; } public static void main(String[] args) { Practice5 thisClass = new Practice5(); thisClass.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); thisClass.setVisible(true); } public Practice5() { super(); initialize(); } // 初始化方法 private void initialize() { this.setSize(408, 79); this.setContentPane(getJContentPane()); this.setTitle("JFrame"); } // 初始化内容面板的方法 private JPanel getJContentPane() { if (jContentPane == null) { jContentPane = new JPanel(); jContentPane.setLayout(new FlowLayout()); jContentPane.add(getJTextField(), null); jContentPane.add(getOpenFile(), null); jContentPane.add(getPlayButton(), null); } return jContentPane; } } 不知道为什麽播放不到音乐 不知道错在那里 求高手指教
问一个小问题 import java.awt.*; import java.awt.event.*; import javax.swing.*; public class Practice6 extends JFrame { publicPractice6() { super(); setTitle("右键菜单"); setBounds(100,100, 500, 375); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); JLabellabel1 = new JLabel("Please press right chick!"); label1.setBounds(140,460, 150, 20); // <----- getContentPane().add(label1); finalJPopupMenu popupMenu = new JPopupMenu(); getContentPane().addMouseListener(newMouseAdapter() { publicvoid mouseReleased(MouseEvent e) { if(e.isPopupTrigger()) popupMenu.show(e.getComponent(),e.getX(), e.getY()); } }); JMenuItemaPage1 = new JMenuItem("剪切"); aPage1.addActionListener(newItemListener()); popupMenu.add(aPage1); JMenuItemaPage2 = new JMenuItem("复制"); aPage2.addActionListener(newItemListener()); popupMenu.add(aPage2); JMenuItemaPage3 = new JMenuItem("贴上"); aPage3.addActionListener(newItemListener()); popupMenu.add(aPage3); JMenuaPage4 = new JMenu("编辑"); popupMenu.add(aPage4); JMenuItembPage1 = new JMenuItem("只读"); bPage1.addActionListener(newItemListener()); aPage4.add(bPage1); JMenuItembPage2 = new JMenuItem("可写"); bPage2.addActionListener(newItemListener()); aPage4.add(bPage2); JMenubPage3 = new JMenu("字体"); aPage4.add(bPage3); JMenuItemcPage1 = new JMenuItem("加粗"); cPage1.addActionListener(newItemListener()); bPage3.add(cPage1); JMenuItemcPage2 = new JMenuItem("斜体"); cPage2.addActionListener(newItemListener()); bPage3.add(cPage2); } privateclass ItemListener implements ActionListener { publicvoid actionPerformed(ActionEvent e) { JMenuItemmenuItem = (JMenuItem) e.getSource(); System.out.println("你点击的是:" + menuItem.getText()); } } publicstatic void main(String args[]) { Practice6frame = new Practice6(); frame.setVisible(true); } } 不知道出了什麽事 怎样改label的位置都没有反应 求高手指教
想问一下swing触发事件的写法 import java.awt.*; import java.awt.image.BufferedImage; import javax.swing.*; import javax.imageio.ImageIO; import java.io.File; import java.io.IOException; public class Practice extends JFrame { BufferedImage image = null; public Practice() { initialize(); } private void initialize() { setTitle("I am title"); setSize(500, 500); setLocationRelativeTo(null); setResizable(false); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); try { image = ImageIO.read(new File("D:/Java/image/allgray.jpg")); } catch (IOException e) { e.printStackTrace(); } MainPane mainPane = new MainPane(); mainPane.setLayout(null); JButton button1 = new JButton("This is Button"); button1.setBounds(50, 30, 150, 20); mainPane.add(button1); JLabel label1 = new JLabel(""); label1.setBounds(50, 50, 150, 20); mainPane.add(label1); JTextArea textarea1 = new JTextArea("Please input something"); textarea1.setBounds(50, 110, 150, 20); mainPane.add(textarea1); setContentPane(mainPane); setVisible(true); } class MainPane extends JPanel { protected void paintComponent(Graphics g) { super.paintComponent(g); BufferedImage bi = new BufferedImage(650, 500, BufferedImage.TYPE_INT_ARGB); Graphics g2 = bi.createGraphics(); g2.setColor(Color.BLACK); g2.drawImage(image, 0, 0, this); g.drawImage(bi, 0, 0, this); } } public static void main(String[] args) { Practice pp1 = new Practice(); } } 当按testarea的时候文字会自动清空 之后按BUTTON会把testarea的字显示在label上 应该要怎样写才好
问一个SWING的问题 import java.awt.*; import java.awt.image.BufferedImage; import javax.swing.*; import javax.imageio.ImageIO; import java.io.File; import java.io.IOException; public class Practice extends JFrame { int width = Toolkit.getDefaultToolkit().getScreenSize().width; int height = Toolkit.getDefaultToolkit().getScreenSize().height; BufferedImage image = null; String message = "i am word"; public Practice() { this.setTitle("I am title"); this.setSize(500, 500); this.setLocation((width - 500) / 2, (height - 500) / 2); this.setResizable(false); this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); try { image = ImageIO.read(new File("D:/Java/image/allgray.jpg")); } catch (IOException e) { e.printStackTrace(); } this.repaint(); } public void simplewidget() { Container container = getContentPane(); container.setLayout(null); JButton button1 = new JButton("This is Button"); button1.setBounds(50, 30, 150, 20); container.add(button1); Label label1 = new Label("This is Label"); label1.setBounds(50, 50, 150, 20); container.add(label1); JCheckBox checkbox1 = new JCheckBox("This is Checkbox"); checkbox1.setBounds(50, 70, 150, 20); container.add(checkbox1); JRadioButton radiobutton1 = new JRadioButton("This is Radiobutton"); radiobutton1.setBounds(50, 90, 150, 20); container.add(radiobutton1); JTextArea textarea1 = new JTextArea("This is TextArea"); textarea1.setBounds(50, 110, 150, 20); container.add(textarea1); setVisible(true); } public void paint(Graphics g) { BufferedImage bi = new BufferedImage(650, 500, BufferedImage.TYPE_INT_ARGB); Graphics g2 = bi.createGraphics(); g2.setColor(Color.BLACK); g2.drawImage(image, 0, 0, this); g2.drawString(message, 50, 200); g.drawImage(bi, 0, 0, this); } public static void main(String[] args) { Practice pp1 = new Practice(); pp1.simplewidget(); } }图片在附图 想问一下在加了图片之后 显示的工具元件都会怪怪的 要用滑鼠扫过才能显示.. 求解决方法
遇到一个奇怪的问题 import java.awt.*; import javax.swing.*; public class Practice { JFrame frame = new JFrame(); String[] name = { "Input", "Output", "hint...", "New", "Load", "Save", "Encode", "Decode", "Clear", "Copy" }; public void run() { frame.setSize(600, 160); frame.setLayout(new GridBagLayout()); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.setVisible(true); JLabel n0 = new JLabel(name[0]); GridBagConstraints c0 = new GridBagConstraints(); c0.gridx = 0; c0.gridy = 0; c0.gridwidth = 1; c0.gridheight = 1; c0.weightx = 0; c0.weighty = 0; c0.fill = GridBagConstraints.NONE; c0.anchor = GridBagConstraints.WEST; frame.add(n0, c0); JLabel n1 = new JLabel(name[1]); GridBagConstraints c1 = new GridBagConstraints(); c1.gridx = 0; c1.gridy = 1; c1.gridwidth = 1; c1.gridheight = 1; c1.weightx = 0; c1.weighty = 0; c1.fill = GridBagConstraints.NONE; c1.anchor = GridBagConstraints.WEST; frame.add(n1, c1); JLabel n2 = new JLabel(name[2]); GridBagConstraints c2 = new GridBagConstraints(); c2.gridx = 0; c2.gridy = 3; c2.gridwidth = 1; c2.gridheight = 1; c2.weightx = 0; c2.weighty = 0; c2.fill = GridBagConstraints.BOTH; c2.anchor = GridBagConstraints.WEST; frame.add(n2, c2); JTextField n3 = new JTextField(); GridBagConstraints c3 = new GridBagConstraints(); c3.gridx = 1; c3.gridy = 0; c3.gridwidth = 6; c3.gridheight = 1; c3.weightx = 0; c3.weighty = 0; c3.fill = GridBagConstraints.BOTH; c3.anchor = GridBagConstraints.WEST; frame.add(n3, c3); JTextField n4 = new JTextField(); GridBagConstraints c4 = new GridBagConstraints(); c4.gridx = 1; c4.gridy = 1; c4.gridwidth = 6; c4.gridheight = 1; c4.weightx = 0; c4.weighty = 0; c4.fill = GridBagConstraints.BOTH; c4.anchor = GridBagConstraints.WEST; frame.add(n4, c4); for (int i = 0; i < 7; i++) { JButton n5 = new JButton(name[i + 3]); GridBagConstraints c5 = new GridBagConstraints(); c5.gridx = i; c5.gridy = 2; c5.gridwidth = 1; c5.gridheight = 1; c5.weightx = 0; c5.weighty = 0; c5.fill = GridBagConstraints.BOTH; c5.anchor = GridBagConstraints.CENTER; frame.add(n5, c5); } } public static void main(String[] args) { Practice gui = new Practice(); gui.run(); } } 执行的时候有时候没问题 但有时候textfield会变长或缩小 有时候甚至连testfield都不见了 为什麽会这样?
求多线程倒数用法 import java.awt.Color; import java.awt.Graphics; import java.awt.Toolkit; import java.awt.event.MouseEvent; import java.awt.event.MouseListener; import java.awt.image.BufferedImage; import java.io.File; import java.io.IOException; import javax.imageio.ImageIO; import javax.swing.JFrame; public class Practice extends JFrame implements MouseListener, Runnable { int width = Toolkit.getDefaultToolkit().getScreenSize().width; int height = Toolkit.getDefaultToolkit().getScreenSize().height; BufferedImage image = null; Thread t = new Thread(this); int maxTime = 20; public void simpleFrame() { this.setTitle("I am title"); this.setSize(500, 500); this.setLocation((width - 500) / 2, (height - 500) / 2); this.setResizable(false); this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); this.addMouseListener(this); this.setVisible(true); t.start(); this.repaint(); } public void paint(Graphics g) { BufferedImage bi = new BufferedImage(650, 500, BufferedImage.TYPE_INT_ARGB); Graphics g2 = bi.createGraphics(); g2.setColor(Color.lightGray); g2.fillRect(0, 0, bi.getWidth(), bi.getHeight()); g2.setColor(Color.BLACK); g2.drawString("" + maxTime, 50, 200); g.drawImage(bi, 0, 0, this); } public void mouseClicked(MouseEvent e) { } public void mouseEntered(MouseEvent e) { } public void mouseExited(MouseEvent e) { } public void mousePressed(MouseEvent e) { } public void mouseReleased(MouseEvent e) { } public static void main(String[] args) { Practice gg = new Practice(); gg.simpleFrame(); } public void run() { this.repaint(); try { maxTime--; Thread.sleep(1000); } catch (InterruptedException e) { // TODO Auto-generated catch block e.printStackTrace(); } } } 求视窗里的数字20 想用多线程每秒减1 直到0 之后会印出”倒数结束” 求高手指教
想问个JFrame防文字重叠问题 package PPP; import java.awt.Color; import java.awt.Graphics; import java.awt.Toolkit; import java.awt.event.MouseEvent; import java.awt.event.MouseListener; import java.awt.image.BufferedImage; import javax.swing.JFrame; public class Practice2 extends JFrame implements MouseListener { int width = Toolkit.getDefaultToolkit().getScreenSize().width; int height = Toolkit.getDefaultToolkit().getScreenSize().height; BufferedImage image = null; String message = "i am word"; int pressCount = 0; public void simpleFrame() { this.setTitle("I am title"); this.setSize(500, 500); this.setLocation((width - 500) / 2, (height - 500) / 2); this.setResizable(false); this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); this.setBackground(Color.lightGray); this.addMouseListener(this); this.setVisible(true); this.repaint(); } public void paint(Graphics g) { BufferedImage bi = new BufferedImage(650, 500, BufferedImage.TYPE_INT_ARGB); Graphics g2 = bi.createGraphics(); g2.setColor(Color.BLACK); g2.drawString(message, 50, 200); g.drawImage(bi, 0, 0, this); } public void mouseClicked(MouseEvent e) { } public void mouseEntered(MouseEvent e) { } public void mouseExited(MouseEvent e) { } public void mousePressed(MouseEvent e) { message = "You press " + pressCount + " time"; pressCount++; this.repaint(); } public void mouseReleased(MouseEvent e) { } public static void main(String[] args) { Practice gg = new Practice(); gg.simpleFrame(); } } 本来每按一下都会增加count 但是突然文字都重叠了 求解决文字重叠问题
首页 1 2 下一页