简体   繁体   English

JButton用鼠标出现和消失

[英]JButtons appearing and disappearing with mouse

When I add several JButtons to this JFrame, the buttons appear and disappear with mouse movements...kinda spooky. 当我向此JFrame中添加几个JButton时,这些按钮会随着鼠标的移动而出现和消失……有点怪异。 Any Ideas? 有任何想法吗? Other Projects dont have this issue, therefore im pretty sure its my code...but its so simple im not sure whats wrong! 其他项目没有这个问题,因此我非常确定它是我的代码...但是它如此简单,所以我不确定有什么问题!

Full Code: (I blurred out my name) 完整代码:(我模糊了我的名字)

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), "");
  }
}

I hope I did undestand you right: you wants to display a new JButton on your JFrame. 我希望我确实对你不理解:您想在JFrame上显示一个新的JButton。 But it doesn't appear. 但是它没有出现。

If so you can try to use: 如果是这样,您可以尝试使用:

f.updateUI();

After you have added the button to the frame. 将按钮添加到框架后。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM