我用的是swing组件,在jFrame上做的!谢谢了!
解决方案 »
- 还是对象的问题 请帮我找出错误
- 问一个关于repaint()方法是怎么工作的?
- JSP学习需要注意什么?
- 求一段解析字符串中数据的最优代码~~~~~~~~~!!!
- 一个简单的dwr的测试,出现问题,郁闷!
- 我在命令行下的jdk设置运行程序时,编译没问题,执行是老是出现NoClassDefFoundError哪个高手能告诉我怎么回师 ?我在命令行下的jdk设置
- java入门菜鸟请教个问题。
- 菜鸟 求救,搞了一下午,也没搞懂。关于java连接数据库的。
- 【Android开发】请问Android怎么打开Word、Excel文件?
- 为什么该段程序不响应键盘?
- double类型赋值为什么提示错误
- J2SE中怎么画抛物线和双曲线
import java.awt.event.*;
import java.io.File;
import java.io.IOException;import javax.imageio.ImageIO;
import javax.swing.*;
import java.io.*;import sun.audio.*;public class ButtonEvent
{
public static void main(String[] args)
{
ButtonFrame frame = new ButtonFrame();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
}
}
class ButtonFrame extends JFrame
{
public ButtonFrame()
{
setTitle("ButtonTest");
setSize(400, 400);
ButtonPanel panel = new ButtonPanel();
add(panel);
}
}
class ButtonPanel extends JPanel
{
public ButtonPanel()
{
JButton yellowButton = new JButton("Yellow");
JButton buleButton = new JButton("Blue");
JButton redButton = new JButton("read");
add(yellowButton);
add(buleButton);
add(redButton);
ColorAction yellowAction = new ColorAction("111");
ColorAction blueAction = new ColorAction("222");
ColorAction redAction = new ColorAction("333");
yellowButton.addActionListener(yellowAction);
buleButton.addActionListener(blueAction);
redButton.addActionListener(redAction);
}
public void paintComponent(Graphics g)
{
super.paintComponent(g);
} private class ColorAction implements ActionListener
{
public ColorAction(String s)
{
w = s;
}
public void actionPerformed(ActionEvent event)
{
JFrame frame = new JFrame(); frame.setVisible(true);
frame.setTitle(w); frame.setSize(300, 200); ImagePanel panel = new ImagePanel();
frame.add(panel); try
{
FileInputStream fileau = new FileInputStream("C:\\WINDOWS\\Media\\tada.wav");
AudioStream as = new AudioStream(fileau);
AudioPlayer.player.start(as);
}
catch (FileNotFoundException ex)
{
frame.setTitle("Error");
}
catch (IOException ex)
{
frame.setTitle("Error");
} } class ImagePanel extends JPanel
{
ImagePanel()
{
try
{
image = ImageIO.read(new File("d:\\1.jpg"));
}
catch (IOException e)
{
e.printStackTrace();
}
}
public void paintComponent(Graphics g)
{
super.paintComponent(g);
g.drawImage(image, 0, 0, null);
}
}
} private String w;
private Image image;
}
fileDlg=new FileDialog(this,"保存文件",FileDialog.SAVE);
fileDlg.show();
fileName=fileDlg.getFile();
str=ta.getText();
byteBuf=str.getBytes();
try{
FileOutputStream out=new FileOutputStream(fileName);
out.write(byteBuf);
out.close();
}catch(IOException ioe){}
} 这是保存功能,另存效仿这个就OK了,你自己想一想。
咱只写了awt做的,还没用Swing呢
{
try
{
fw=new FileWriter("t102output.txt");//t102output.txt你想要存放的文件名,可以任意。
fw.write(area.getText());
fw.close();
}catch(Exception ex)
{
ex.printStackTrace();
}
}