java JFileChooser问题
1个回答
2013-07-29
展开全部
import java.awt.*;
import java.awt.event.*;
import java.io.File;
import javax.swing.*;
public class Test implements ActionListener {
JFileChooser fileChooser;
JLabel label;
Test() {
fileChooser = new JFileChooser(".");
boolean hide = true;
disableTextComponent(fileChooser, hide);
fileChooser.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
if (e.getActionCommand().equals(JFileChooser.APPROVE_SELECTION)) {
System.out.println("**按钮");
} else if (e.getActionCommand().equals(
JFileChooser.CANCEL_SELECTION)) {
System.out.println("关闭按钮");
}
}
});
}
public void actionPerformed(ActionEvent e) {
int retVal = fileChooser.showOpenDialog(null);
if(retVal == JFileChooser.APPROVE_OPTION) {
File file = fileChooser.getSelectedFile();
if(file != null) {
label.setText(file.getPath());
}
}
}
private void disableTextComponent(Container parent, boolean hide) {
Component[] c = parent.getComponents();
for(int j = 0; j < c.length; j++) {
//System.out.println(unpack(c[j]));
if(unpack(c[j]).equals("MetalFileChooserUI$3")) {
if(hide)
c[j].getParent().setVisible(false);
else
c[j].setEnabled(false);
}
if(((Container)c[j]).getComponentCount() > 0) {
disableTextComponent((Container)c[j], hide);
}
}
}
private String unpack(Component c) {
String s = c.getClass().getName();
int dot = s.lastIndexOf(".");
if(dot != -1)
s = s.substring(dot+1);
return s;
}
private JPanel getContent() {
label = new JLabel();
label.setHorizontalAlignment(JLabel.CENTER);
JButton button = new JButton("open dialog");
button.addActionListener(this);
JPanel panel = new JPanel(new GridBagLayout());
GridBagConstraints gbc = new GridBagConstraints();
gbc.insets = new Insets(5,5,5,5);
gbc.weighty = 1.0;
gbc.gridwidth = GridBagConstraints.REMAINDER;
panel.add(label, gbc);
gbc.anchor = GridBagConstraints.SOUTH;
panel.add(button, gbc);
return panel;
}
public static void main(String[] args) {
JFrame f = new JFrame();
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.setContentPane(new Test().getContent());
f.setSize(360,140);
f.setLocation(200,200);
f.setVisible(true);
}
}
import java.awt.event.*;
import java.io.File;
import javax.swing.*;
public class Test implements ActionListener {
JFileChooser fileChooser;
JLabel label;
Test() {
fileChooser = new JFileChooser(".");
boolean hide = true;
disableTextComponent(fileChooser, hide);
fileChooser.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
if (e.getActionCommand().equals(JFileChooser.APPROVE_SELECTION)) {
System.out.println("**按钮");
} else if (e.getActionCommand().equals(
JFileChooser.CANCEL_SELECTION)) {
System.out.println("关闭按钮");
}
}
});
}
public void actionPerformed(ActionEvent e) {
int retVal = fileChooser.showOpenDialog(null);
if(retVal == JFileChooser.APPROVE_OPTION) {
File file = fileChooser.getSelectedFile();
if(file != null) {
label.setText(file.getPath());
}
}
}
private void disableTextComponent(Container parent, boolean hide) {
Component[] c = parent.getComponents();
for(int j = 0; j < c.length; j++) {
//System.out.println(unpack(c[j]));
if(unpack(c[j]).equals("MetalFileChooserUI$3")) {
if(hide)
c[j].getParent().setVisible(false);
else
c[j].setEnabled(false);
}
if(((Container)c[j]).getComponentCount() > 0) {
disableTextComponent((Container)c[j], hide);
}
}
}
private String unpack(Component c) {
String s = c.getClass().getName();
int dot = s.lastIndexOf(".");
if(dot != -1)
s = s.substring(dot+1);
return s;
}
private JPanel getContent() {
label = new JLabel();
label.setHorizontalAlignment(JLabel.CENTER);
JButton button = new JButton("open dialog");
button.addActionListener(this);
JPanel panel = new JPanel(new GridBagLayout());
GridBagConstraints gbc = new GridBagConstraints();
gbc.insets = new Insets(5,5,5,5);
gbc.weighty = 1.0;
gbc.gridwidth = GridBagConstraints.REMAINDER;
panel.add(label, gbc);
gbc.anchor = GridBagConstraints.SOUTH;
panel.add(button, gbc);
return panel;
}
public static void main(String[] args) {
JFrame f = new JFrame();
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.setContentPane(new Test().getContent());
f.setSize(360,140);
f.setLocation(200,200);
f.setVisible(true);
}
}
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询