【问题标题】:multiple jlabels in a loop, java循环中的多个jlabel,java
【发布时间】:2013-07-28 19:58:52
【问题描述】:

我正在尝试在循环中创建 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) {
}
});
}
}

【问题讨论】:

  • 你还没有初始化数组中的元素,所以它们都是null,给你例外。这是我在初学者第一次开始使用对象数组时看到的常见错误。
  • 1) 对代码块使用一致且符合逻辑的缩进。代码的缩进是为了帮助人们理解程序流程。 2) Java GUI 可能必须在多个平台、不同的屏幕分辨率和使用不同的 PLAF 上工作。因此,它们不利于组件的精确放置。要为强大的 GUI 组织组件,请改用布局管理器或 combinations of them,以及 white space 的布局填充和边框。

标签: java swing for-loop nullpointerexception jlabel


【解决方案1】:

你得到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 有很多可用的布局。看看A Visual Guide to Layout Managers就知道如何使用这些布局了。

【讨论】:

  • 我已经按照你所说的进行了更改,但现在我收到 java.lang.ArrayIndexOutOfBoundsException:。为什么? c1=2
  • 您收到此异常是因为您尝试访问该索引处的元素,该索引在数组中不存在。任何数组都以索引0 开始,以索引lengh - 1 结束。看看我的更新。
  • 你是对的!是因为 setBounds 我没有在我的 gui 中得到标签吗?
  • 然后删除 setbounds 并且不要将面板的布局更改为 null 如果你已经这样做了。!!
  • 不要将布局设置为空。从您的代码中删除以下行: panel.setLaout(null)。如果你没有设置布局null然后没有setBounds,那么每个循环中都会出现yes标签。
猜你喜欢
  • 2012-08-03
  • 1970-01-01
  • 2015-02-11
  • 2023-03-18
  • 1970-01-01
  • 1970-01-01
  • 2016-02-14
  • 1970-01-01
  • 2023-04-01
相关资源
最近更新 更多