Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/user-interface/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java JButtons随鼠标出现和消失_Java_User Interface_Mouse_Jframe_Jbutton - Fatal编程技术网

Java JButtons随鼠标出现和消失

Java JButtons随鼠标出现和消失,java,user-interface,mouse,jframe,jbutton,Java,User Interface,Mouse,Jframe,Jbutton,当我在这个JFrame中添加几个jButton时,按钮会随着鼠标的移动而出现和消失……有点可怕。有什么想法吗?其他项目没有这个问题,所以我很确定这是我的代码…但它很简单,我不知道是什么错 完整代码:我模糊了我的名字 package explorerplus; import java.awt.BorderLayout; import java.awt.Container; import java.awt.Toolkit; import java.io.File; import javax.swi

当我在这个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();

将按钮添加到框架后。

欢迎使用堆栈溢出。请尝试将代码缩小到一个小的相关部分。阅读整个源代码既困难又耗时-更紧凑的示例将为您提供更多答案。更仔细地注意格式/缩进将有助于普通读者。至少可以说,您的代码看起来很恐怖!f、 getContentPane将安装布局管理器,但您使用的是绝对大小和定位。这可能是罪魁祸首,需要f.getContentPane.setLayoutnull。[主题外]:Java方法名称的约定是小写EstatingCamelCase。因此,GoThroughFile aFile应该是GoThroughFile aFile-类似于AllButExtFile f。