0

この JFrame にいくつかの JButton を追加すると、マウスの動きに合わせてボタンが表示されたり消えたりします...ちょっと不気味です。何か案は?他のプロジェクトにはこの問題はありません。したがって、私のコードであると確信しています...しかし、非常に単純で、何が問題なのかわかりません!

完全なコード: (私は自分の名前をぼかしました)

package explorerplus;

import java.awt.BorderLayout;
import java.awt.Container;
import java.awt.Toolkit;
import java.io.File;
import javax.swing.*;

public class ExplorerPlus {

//Declarations
Toolkit t = Toolkit.getDefaultToolkit();
 JFrame f = new JFrame();

public static void main(String[] args) {
   SwingUtilities.invokeLater(new Runnable() {
        public void run(){
            ExplorerPlus ep = new ExplorerPlus();
        }
    });
}
private ExplorerPlus(){
   // f.super("");
    f.setUndecorated(false);

    f.setSize(t.getScreenSize());
    f.setDefaultCloseOperation(f.EXIT_ON_CLOSE);
    Container c = null;
    c =new JScrollPane(c);
    f.setContentPane(c);
    f.setVisible(true);
    GoThrough(new File("C:/Users/*****/Pictures/"));
    f.repaint();
 }
  static int spc_count=-1;
  int curr_x = 10, curr_y = 10;
  void GoThrough(File aFile) {
spc_count++;
String spcs = "";
for (int i = 0; i < spc_count; i++)
  spcs += " ";
if(aFile.isFile()){
  System.out.println(spcs + "[FILE] " + aFile.getName());
  File file = aFile;
  String s = getExt(file) + ".png";
  JButton b = new JButton(new ImageIcon(s));
  b.setToolTipText(file.getName());
  b.setSize(256, 256);
  b.setLocation(curr_x, curr_y);
  b.setVisible(true);
  b.validate();
  f.validate();

  if(curr_x < f.getWidth() - 270){
      curr_x+=270;
  }else{
  curr_y+=270;
  curr_x = 10;
  }
  f.getContentPane().add(b);


}
else if (aFile.isDirectory()) {
  System.out.println(spcs + "[DIR] " + aFile.getName());
  File[] listOfFiles = aFile.listFiles();
  if(listOfFiles!=null) {
    for (int i = 0; i < listOfFiles.length; i++)
      GoThrough(listOfFiles[i]);
  } else {
    System.out.println(spcs + " [ACCESS DENIED]");
  }
}
spc_count--;
}

  String getExt(File f){

 if(f.getName().toLowerCase().endsWith(".jpg")|| f.getName().toLowerCase().endsWith(".jpeg")){
     return "jpeg";
 } else if(f.getName().toLowerCase().endsWith(".png")){
     return "png";
 }
 else if(f.getName().toLowerCase().endsWith(".html")){
     return "html";
 }else if(f.getName().toLowerCase().endsWith(".ini")){
     return "ini";
 }else{
     return "text";
 }
  }
  String AllButExt(File f){
      String name = f.getName();
      return name.replace(getExt(f), "");
  }
}
4

1 に答える 1

0

JFrame に新しい JButton を表示したいと考えています。しかし、それは表示されません。

もしそうなら、あなたは使用しようとすることができます:

f.updateUI();

ボタンをフレームに追加した後。

于 2012-07-03T08:56:13.137 に答える