我的意圖是將JFileChooser嵌入到其他組件中,例如,可以選擇一個文件並單擊「添加」按鈕,以便將文件添加到JList中(在運行時)。我已經以這種形式創建了一個例子GUI:在另一個組件中嵌入JFileChooser
我無法創建JFileChooser中和的JList之間的聯繫。任何人都可以幫忙嗎?
您還可以看看我曾嘗試:
public Converter() {
setForeground(Color.BLACK);
getContentPane().setLayout(null);
textField = new JTextField();
textField.setBounds(20, 12, 714, 20);
getContentPane().add(textField);
textField.setColumns(10);
final JScrollPane scrollPane = new JScrollPane();
setTitle("ABC");
scrollPane.setBounds(0, 470, 766, -438);
getContentPane().add(scrollPane);
list = new JList();
list.setBackground(Color.LIGHT_GRAY);
list.setForeground(Color.GRAY);
vector = new Vector<File>();
field = new JTextField();
final JFileChooser fileChooser = new JFileChooser();
fileChooser.setBounds(10, 43, 485, 463);
getContentPane().add(fileChooser);
list = new JList(vector);
list.setBackground(Color.LIGHT_GRAY);
JButton btnNewButton = new JButton("ADD");
btnNewButton.setBounds(505, 106, 89, 23);
btnNewButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
add();
}
private void add() {
// TODO Auto-generated method stub
{
for (File file : fileChooser.getSelectedFiles()) {
field.setText(file.getAbsolutePath());
vector.add(file);
System.out.println("Added..!!");
}
//list.updateUI();
}
}
});
getContentPane().add(btnNewButton);
JButton btnNewButton_1 = new JButton("REMOVE");
btnNewButton_1.setBounds(505, 190, 89, 23);
btnNewButton_1.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
remove();
}
private void remove() {
if(list.getSelectedIndices().length > 0) {
int[] selectedIndices = list.getSelectedIndices();
for (int i = selectedIndices.length-1; i >=0; i--) {
vector.removeElementAt(i);
System.out.println("Removed..!!");
}
}
list.updateUI();
}
});
getContentPane().add(btnNewButton_1);
JButton btnNewButton_2 = new JButton("DECODE");
btnNewButton_2.setBounds(505, 278, 89, 23);
getContentPane().add(btnNewButton_2);
JList list_1 = new JList();
list_1.setForeground(Color.BLACK);
list_1.setBackground(Color.LIGHT_GRAY);
list_1.setBounds(604, 109, 162, 328);
getContentPane().add(list_1);
final JFrame Jframe = new JFrame();
Jframe.setFont(new Font("Arial", Font.BOLD, 14));
Jframe.setForeground(Color.WHITE);
Jframe.setTitle("Additional Loader Information");
Jframe.getContentPane().setLayout(null);
}
static class PreviewPane extends JPanel implements PropertyChangeListener {
/**
*
*/
private static final long serialVersionUID = 1L;
private JLabel label;
private int maxImgWidth;
public PreviewPane() {
setLayout(new BorderLayout(5,5));
setBorder(BorderFactory.createEmptyBorder(5,5,5,5));
add(new JLabel("Preview:"), BorderLayout.NORTH);
label = new JLabel();
label.setBackground(Color.WHITE);
label.setOpaque(true);
label.setPreferredSize(new Dimension(200, 200));
maxImgWidth = 195;
label.setBorder(BorderFactory.createEtchedBorder());
add(label, BorderLayout.CENTER);
}
public void propertyChange(PropertyChangeEvent evt) {
Icon icon = null;
if(JFileChooser.SELECTED_FILE_CHANGED_PROPERTY.equals(evt
.getPropertyName())) {
File newFile = (File) evt.getNewValue();
if(newFile != null) {
String path = newFile.getAbsolutePath();
if(path.endsWith(".gif") || path.endsWith(".jpg")
|| path.endsWith(".png") || path.endsWith(".bmp")) {
try {
BufferedImage img =
ImageIO.read(newFile);
float width = img.getWidth();
float height = img.getHeight();
float scale = height/width;
width = maxImgWidth;
height = (width * scale);
// height should be scaled from new width
}
catch(IOException e) {
// couldn't read image.
}
}
}
label.setIcon(icon);
this.repaint();
}
}
}
public static void main(String args[]) {
// Create an instance of the test application
Converter frame = new Converter();
frame.pack();
frame.setVisible(true);
}
}
這將是真正有用的,如果有人可以幫助我與此有關。
如何你無法創建鏈接?你是否遇到錯誤,異常或意外行爲?請更徹底地描述你的問題。此外,創建[SSCCE](http://sscce.org)將是一個巨大的幫助。 – Jeffrey 2012-04-07 18:56:37
嗨,它只是不起作用。我沒有收到任何錯誤,但是當我單擊按鈕時沒有發生任何操作,並且這完全不起作用:( – dmurali 2012-04-07 19:00:03
對您的代碼進行窺視:'新的JList(fileChooser)'(第一行)應該拋出請考慮製作一個[SSCCE](http://sscce.org),或者在最低限度發佈更多的代碼。 – Jeffrey 2012-04-07 19:06:37