Java awt中想把一个按钮单机事件中的数据应用到另一个按钮单机事件去。
//按钮单击事件button.addMouseListener(newMouseAdapter(){publicvoidmouseClicked(MouseEvente)...
//按钮单击事件
button.addMouseListener(new MouseAdapter(){
public void mouseClicked(MouseEvent e){
Button b = (Button)e.getComponent();
//获得Button所在的容器
Panel p = (Panel)b.getParent();
Frame f = (Frame)p.getParent();
FileDialog fd = new FileDialog(f,"浏览",FileDialog.LOAD);
fd.setVisible(true);
String dir = fd.getDirectory();
String fileName = fd.getFile();
if(!(dir == null || fileName == null)){
String fileDir = dir + fileName;
File file = new File(fileDir);
if(file.exists()){
text.setText(fileDir);
}else{
text.setText("该文件不存在!");
}
}
f.setVisible(true);
}
});
想把获得的文件路径(dir)在另一个按钮单机事件里面使用。
//按钮单击事件
button1.addMouseListener(new MouseAdapter(){
public void mouseClicked(MouseEvent e){
在这里面取不到上个方法的文件路径。
} 展开
button.addMouseListener(new MouseAdapter(){
public void mouseClicked(MouseEvent e){
Button b = (Button)e.getComponent();
//获得Button所在的容器
Panel p = (Panel)b.getParent();
Frame f = (Frame)p.getParent();
FileDialog fd = new FileDialog(f,"浏览",FileDialog.LOAD);
fd.setVisible(true);
String dir = fd.getDirectory();
String fileName = fd.getFile();
if(!(dir == null || fileName == null)){
String fileDir = dir + fileName;
File file = new File(fileDir);
if(file.exists()){
text.setText(fileDir);
}else{
text.setText("该文件不存在!");
}
}
f.setVisible(true);
}
});
想把获得的文件路径(dir)在另一个按钮单机事件里面使用。
//按钮单击事件
button1.addMouseListener(new MouseAdapter(){
public void mouseClicked(MouseEvent e){
在这里面取不到上个方法的文件路径。
} 展开
2个回答
展开全部
//利用外部类的全局变量,并且利用set get 方法来修改数据。
import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
public class MyJFrame extends JFrame {
public static void main(String[] args) {
new MyJFrame();
}
private final class ButtonAction1 implements ActionListener {
@Override
public void actionPerformed(ActionEvent e) {
setFile("C:/");
}
}
private final class ButtonAction2 implements ActionListener {
@Override
public void actionPerformed(ActionEvent e) {
System.out.println(getFile());
}
}
public MyJFrame() {
setSize(400, 400);
setDefaultCloseOperation(EXIT_ON_CLOSE);
JButton b1 = new JButton("Button1");
JButton b2 = new JButton("Button2");
b1.addActionListener(new ButtonAction1());
b2.addActionListener(new ButtonAction2());
add(b1, BorderLayout.NORTH);
add(b2, BorderLayout.SOUTH);
setVisible(true);
}
private String getFile() {
return file;
}
private void setFile(String file) {
this.file = file;
}
private String file;
}
import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
public class MyJFrame extends JFrame {
public static void main(String[] args) {
new MyJFrame();
}
private final class ButtonAction1 implements ActionListener {
@Override
public void actionPerformed(ActionEvent e) {
setFile("C:/");
}
}
private final class ButtonAction2 implements ActionListener {
@Override
public void actionPerformed(ActionEvent e) {
System.out.println(getFile());
}
}
public MyJFrame() {
setSize(400, 400);
setDefaultCloseOperation(EXIT_ON_CLOSE);
JButton b1 = new JButton("Button1");
JButton b2 = new JButton("Button2");
b1.addActionListener(new ButtonAction1());
b2.addActionListener(new ButtonAction2());
add(b1, BorderLayout.NORTH);
add(b2, BorderLayout.SOUTH);
setVisible(true);
}
private String getFile() {
return file;
}
private void setFile(String file) {
this.file = file;
}
private String file;
}
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询