java,关于swing选择框和文件读取求助!!!

V再见诺丁山 2011-12-12 02:32:46
一个简单的代码如下:
问题一:public class Testaction extends JFrame implements ActionListener{
JToolBar tool;
JButton open;
JLabel img1;
JFrame f;
public Testaction(){
open=new JButton("open");
tool=new JToolBar();
tool.add(open);
add(tool,BorderLayout.NORTH);//把工具栏放上面
add(img1,BorderLayout.CENTER);//把标签放在下面
open.addActionListener(this);

}
public void actionPerformed(ActionEvent e) {
/*显示文件选择框,读取一张图片*/
JFileChooser fc=new JFileChooser();
int i=fc.showOpenDialog(this);
if(i==JFileChooser.APPROVE_OPTION){
File file=fc.getSelectedFile();
String URL=file.getPath();
ImageIcon image = new ImageIcon(URL);
img1 = new JLabel(image);//把图片放到标签img1里。
}

}
public static void main(String args[]){
new Testaction();
}


}
我是想点击工具栏中的open按钮时,弹出选择图片的文件选择框,并将图片读入到窗口。
为什么我这段代码运行的时候出现问题,运行不了..
问题二:
我想再设置一个按钮为"查看",点击此按钮时,直接从电脑中弹出一个.txt文件(这个文件的目录在D:\test\gameProject\比赛过程.txt),请帮我写一下此按钮事件响应的代码。谢谢大家了!!!
...全文
290 7 打赏 收藏 转发到动态 举报
写回复
用AI写文章
7 条回复
切换为时间正序
请发表友善的回复…
发表回复
V再见诺丁山 2011-12-12
  • 打赏
  • 举报
回复
多谢了![Quote=引用 3 楼 huntor 的回复:]
Java code
import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;
import javax.swing.ImageIcon;
import javax.swing.JButton;
i……
[/Quote]
V再见诺丁山 2011-12-12
  • 打赏
  • 举报
回复
多谢![Quote=引用 4 楼 lqh_it 的回复:]
若果这是你的全部代码的话 代码本身就有问题。还有,为什么非要用Label去显示图片呢?
弟二个问题 你可以使用FileDialog来打开想要的文件。
[/Quote]
lqh_it 2011-12-12
  • 打赏
  • 举报
回复
三楼正解
lqh_it 2011-12-12
  • 打赏
  • 举报
回复
若果这是你的全部代码的话 代码本身就有问题。还有,为什么非要用Label去显示图片呢?
弟二个问题 你可以使用FileDialog来打开想要的文件。
huntor 2011-12-12
  • 打赏
  • 举报
回复
import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JToolBar;

public class TestAction extends JFrame implements ActionListener {
JToolBar tool;
JButton open;
JLabel img1;

public TestAction(){
super("Test");
open=new JButton("Open");
tool=new JToolBar();
tool.add(open);
add(tool,BorderLayout.NORTH);//把工具栏放上面
img1 = new JLabel();
add(img1,BorderLayout.CENTER);//把标签放在下面
open.addActionListener(this);
pack();
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setVisible(true);
}
public void actionPerformed(ActionEvent e) {
/*显示文件选择框,读取一张图片*/
JFileChooser fc=new JFileChooser();
int i=fc.showOpenDialog(this);
if(i==JFileChooser.APPROVE_OPTION){
File file=fc.getSelectedFile();
String URL=file.getPath();
ImageIcon image = new ImageIcon(URL);
img1.setIcon(image);
pack();
}
}
public static void main(String args[]){
new TestAction();
}
}
V再见诺丁山 2011-12-12
  • 打赏
  • 举报
回复
文件读取我知道,但是你可以看下我的代码。。不知道为什么出问题。
[Quote=引用 1 楼 arikara 的回复:]
Java code


public class CopyFileDemo extends JFrame{
JFileChooser fileChooser; //文件选择器
JTextField jtfSourceFile; //源文件路径
JTextField jtfTargetFile; //目标文件路径
JButton sel……
[/Quote]
arikara 2011-12-12
  • 打赏
  • 举报
回复

public class CopyFileDemo extends JFrame{
JFileChooser fileChooser; //文件选择器
JTextField jtfSourceFile; //源文件路径
JTextField jtfTargetFile; //目标文件路径
JButton selectFile1; //选择文件按钮
JButton selectFile2;
JButton copyButton; //拷贝按钮
public CopyFileDemo(){
super("拷贝文件演示"); //调用父类构造函数
fileChooser=new JFileChooser(); //实例化文件选择器
Container container=getContentPane(); //得到容器
jtfSourceFile=new JTextField(16); //实例化组件
jtfTargetFile=new JTextField(16);
selectFile1=new JButton("选择");
selectFile2=new JButton("选择");
copyButton=new JButton("拷贝");
Box box=Box.createVerticalBox(); //实例化Box,用于容纳组件
JPanel panel=new JPanel();
panel.add(new JLabel("源 文 件")); //增加组件到面板(panel)上
panel.add(jtfSourceFile);
panel.add(selectFile1);
box.add(panel); //增加组件到Box上
panel=new JPanel();
panel.add(new JLabel("目标文件"));
panel.add(jtfTargetFile);
panel.add(selectFile2);
box.add(panel);
box.add(copyButton);
container.add(box); //增加组件到容器上
selectFile1.addActionListener(new SelectFileListener()); //设置选择文件的事件处理
selectFile2.addActionListener(new SelectFileListener());
copyButton.addActionListener(new ActionListener(){ //拷贝按钮事件处理
public void actionPerformed(ActionEvent event) {
String sourceFile=jtfSourceFile.getText(); //得到源文件路径
String targetFile=jtfTargetFile.getText(); //得到目标文件路径
if (copy(sourceFile,targetFile)){ //拷贝文件
JOptionPane.showMessageDialog(CopyFileDemo.this,"拷贝成功"); //显示拷贝成功信息
}
else{
JOptionPane.showMessageDialog(CopyFileDemo.this,"拷贝失败"); //发生错误
}
}
});
setSize(340,160); //设置窗口尺寸
setVisible(true); //设置窗口可视
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); //关闭窗口时退出程序
}
class SelectFileListener implements ActionListener { //取得目录内容的事件处理
public void actionPerformed(ActionEvent event) {
if (fileChooser.showOpenDialog(CopyFileDemo.this)==JFileChooser.APPROVE_OPTION){ //弹出文件选择器,并判断是否点击了打开按钮
String fileName=fileChooser.getSelectedFile().getAbsolutePath(); //得到选择文件的绝对路径
if (event.getSource().equals(selectFile1)){ //判断事件来自于哪个按钮
jtfSourceFile.setText(fileName); //设置源文件路径
}
else{
jtfTargetFile.setText(fileName); //设置目标文件路径
}
}
}
}
public boolean copy(String file1,String file2){ //拷贝文件方法
try{
File file=new File(file1);
File fileout=new File(file2);
FileInputStream in=new FileInputStream(file);
FileOutputStream out=new FileOutputStream(file2);
byte[] b=new byte[1024];
int c;
while((c=in.read(b))!=-1){
out.write(b, 0, c);
}
in.close();
out.close();
return true;
}
catch(Exception e){
return false;
}
}
public static void main(String[] args){
new CopyFileDemo();
}
}

62,614

社区成员

发帖
与我相关
我的任务
社区描述
Java 2 Standard Edition
社区管理员
  • Java SE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧