簡體   English   中英

JButton用鼠標出現和消失

[英]JButtons appearing and disappearing with mouse

當我向此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), "");
  }
}

我希望我確實對你不理解:您想在JFrame上顯示一個新的JButton。 但是它沒有出現。

如果是這樣,您可以嘗試使用:

f.updateUI();

將按鈕添加到框架后。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM