當我點擊激活文件選擇器的按鈕,並添加結果文件面板顏色消失。有誰知道爲什麼會發生這種情況?面板丟失顏色
import javax.swing.JPanel;
import javax.swing.JButton;
import javax.swing.JLabel;
import java.awt.Component;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;
import java.io.IOException;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;
import javax.swing.filechooser.FileSystemView;
import javax.swing.JFileChooser;
import javax.swing.plaf.FileChooserUI;
@SuppressWarnings("serial")
public class pan extends JPanel implements DropTargetListener {
private DefaultListModel listModel = new DefaultListModel();
private JButton addbutton;
private JButton removebutton;
private JButton selectbutton;
private JButton lockbutton;
private JButton unlockbutton;
/**
* Create the panel.
*/
public pan() {
setLayout(null);
addbutton = new JButton("New button");
addbutton.setBounds(10, 10, 90, 100);
addbutton.addActionListener(new Action());
add(addbutton);
removebutton = new JButton("New button");
removebutton.setBounds(110, 10, 90, 100);
add(removebutton);
selectbutton = new JButton("New button");
selectbutton.setBounds(210, 10, 90, 100);
add(selectbutton);
lockbutton = new JButton("New button");
lockbutton.setBounds(310, 10, 90, 100);
add(lockbutton);
unlockbutton = new JButton("New button");
unlockbutton.setBounds(410, 10, 90, 100);
add(unlockbutton);
JLabel headerLabel = new JLabel("New label");
headerLabel.setBorder(new BevelBorder(BevelBorder.RAISED,
Color.LIGHT_GRAY, Color.GRAY, null, null));
headerLabel.setUI(new ModifLabelUI());
headerLabel.setBounds(10, 120, 635, 30);
add(headerLabel);
}
class Action implements ActionListener {
@Override
public void actionPerformed(ActionEvent e) {
if(e.getSource()==addbutton){
JFileChooser filechooser=new JFileChooser();
filechooser.setMultiSelectionEnabled(true);
filechooser.updateUI();
filechooser.showOpenDialog(new pan());
File files=filechooser.getSelectedFile();
listModel.addElement(files);
}
}
}
我會建議讓你的問題標題更多地描述問題是什麼。這可能會給它更多的關注。 – John
1.在調用showOpenDialog時,使用'pan.this'而不是'new pan()',否則就沒有任何意義。 2.使用Java命名約定(類以大寫字母開頭)3.使用適當的LayoutManager和嵌套佈局,而不是此絕對佈局。 4.發佈一個[SSCCE](http://sscce.org),說明你的問題,並詳細說明什麼是不工作。從您發佈的代碼中無法分辨出來。此外,這是什麼'listModel',我沒有看到任何'JList'? –
我試過你的例子,除了想要在空佈局上尖叫,沒有問題 – MadProgrammer