java - 循环中的多个jlabel,java

标签 java swing for-loop nullpointerexception jlabel

我试图在循环中创建 JLabels,但似乎出了问题。循环中没有出现任何标签。我缺少什么?我是否必须设置另一个面板或者我可以使用我现有的面板?

 public class searchFrame extends JFrame {

JLabel bsearch= new JLabel ("Search by name: ");
JTextField txsearch = new JTextField(25);
JButton bgo = new JButton("Go");
JLabel allsearch= new JLabel ("All files");
JLabel result=new JLabel ();
JPanel panel = new JPanel();

searchFrame(){
super("Search frame");
setSize(260,400);
setLocation(500,280);
panel.setLayout (null); 

bsearch.setBounds(10,40,100,20);
txsearch.setBounds(120,40,70,20);
bgo.setBounds(195,40,55,20);
allsearch.setBounds(10,70,80,20);
result.setBounds(10,100,80,20);

panel.add(bsearch);
panel.add(txsearch);
panel.add(bgo);
panel.add(allsearch);
panel.add(result);

getContentPane().add(panel);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setVisible(true);
actionsearch();
mouseactionlabel();
}

public void actionsearch(){
bgo.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent ae) {
    String path1="C:/Documents and Settings/giannis/Desktop/Server/";
    String fileName = "";
            try{
            fileName = txsearch.getText();
            File directory = new File(path1);
                        File[] listOfFiles = directory.listFiles();
                        int c=0;
                        for (File file : listOfFiles ) {
                                String name = file.getName();

                                    if (name.equals(fileName)) {
                                    result.setText(fileName);
                                    long size=file.length();
                                    long modified=file.lastModified();                                  
                                     c=1;
                                        System.out.println("File found..");
                                        System.out.println("File size"+size);
                                        System.out.println("File modified"+modified);
                                    }

                                    }if (c!=1){
                                        System.out.println("File not found..");
                                        result.setText("Not found");
                                    }}
        catch(Exception e){
            e.printStackTrace();
        }                              

}
});
}

void mouseactionlabel(){
allsearch.addMouseListener(new MouseListener() {
public void mouseClicked(MouseEvent arg0) {
    String path1="C:/Documents and Settings/giannis/Desktop/Server/";

            try{

                File directory = new File(path1);
                File[] listOfFiles = directory.listFiles();
                int c1 = directory.listFiles().length;
                System.out.println(c1);
                JLabel[] labels = new JLabel[c1];

                for (File file : listOfFiles ) {

                for (int i = 0 ;i < c1; i++)
                {     
                    for(int v = 120; v <= 300; v += 20){
                        String name = file.getName();
                        labels[i] = new JLabel();
                        labels[i].setBounds(10,v,80,20);
                        labels[i].setText(name);                        
                        panel.add(labels[i]);


                }}
                                    }

                                    }
        catch(Exception e){
            e.printStackTrace();
        }
}
public void mouseEntered(MouseEvent arg0) {
}
public void mouseExited(MouseEvent arg0) {
}
public void mousePressed(MouseEvent arg0) {
}
public void mouseReleased(MouseEvent arg0) {
}
});
}
}

最佳答案

您收到 NullPointerException 是因为您在调用 setBounds 方法之前尚未初始化 JLabel 。所以, 改变这个:

String name = file.getName();
labels[i].setBounds(10,v,80,20);

至:

String name = file.getName();
labels[i]= new JLabel();
labels[i].setBounds(10,v,80,20);

并摆脱 ArrayIndexOutOfBoundException 更改:

for (int i = 1 ;i <= c1; i++)

至:

for (int i = 0 ;i < c1; i++)

还有,作为旁注。切勿使用 setBounds 来定位 Swing 中的组件。请改用适当的布局。 swing 有很多可用的布局。看A Visual Guide to Layout Managers了解如何使用这些布局。

关于java - 循环中的多个jlabel,java,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17912264/

相关文章:

java - 将数据存储在数组中并使用 for 循环下推项目

java - 编写数据库支持的 Web 服务的最快方法

java - 计算给定时间内按钮的点击次数

java - ImageIcon,仅显示一半图像

java - 如何在 JDesktopPane 中打开一个 JInternalFrame 并覆盖另一个 JInternalFrame?

javascript - 有没有办法在javascript中的for循环的每次迭代中捕获变量的值?

C++ - For 循环不工作

java - java中如何动态设置PreparedStatement?

java - Eclipse 3.4 文本查看器

java - EMDK 安卓工作室