在java中垂直创建网格

时间:2016-06-21 18:47:54

标签: java netbeans-8 netbeans-6.9 netbeans-plugins

我正在使用GridLayout一次创建一行网格。我想垂直创建这样的网格并在每个网格中放置一个图像。这是我目前的代码段。请给我一些关于如何继续的建议:

public ButtonGrid(int width, int length){

        frame.setLayout(new GridLayout()); //set layout
        grid=new JLabel[length][width]; //allocate the size of grid
    try {
        file = new FileInputStream("I:\\cache1\\newtask_list.txt");
        bout = new BufferedReader(new InputStreamReader(file));

        } catch (Exception e) {Logger.getLogger(ButtonGrid.class.getName()).log(Level.SEVERE, null, e);}
        for(int y=0; y<length; y++){

                for(int x=0; x<width; x++){
                        BufferedImage image=null;
                    try {
                       str=bout.readLine();
                       str1=str.substring(0,23);

                       image=ImageIO.read(new File("I:\\cache1\\newtask\\"+str1));
                       } catch (IOException e) {Logger.getLogger(ButtonGrid.class.getName()).log(Level.SEVERE, null, e);
                     }
                       ImageIcon icon = new ImageIcon(image);

                       grid[y][x]=new JLabel(icon); //creates new button 
                       grid[y][x]=new JLabel("("+x+","+y+")");
                       System.out.println(x+"\t"+y);
                       frame.add(grid[y][x]); //adds button to grid
          }

       }
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.pack(); //sets appropriate size for frame
        frame.setVisible(true); //makes frame visible
}
public static void main(String[] args) {
        new ButtonGrid(12,16);//makes new ButtonGrid with 2 parameters
}

0 个答案:

没有答案