简体   繁体   中英

How to create image slider in swing using grouplayout?

Hi i am trying to create to create desktop application in using i am using Group Layout for creating mu Gui i tried to create image slider but i am not able to achieve how can i achieve this when i run my progrram it throws exception ArrayIndexOutofBound pleas solve my problem

public class MyGui3 extends JFrame {

      private ImageIcon myImage1 = new ImageIcon ("E:\\SOFTWARE\\TrainPIS\\res\\drawable\\yellow.png");
    private ImageIcon myImage2 = new ImageIcon ("E:\\SOFTWARE\\TrainPIS\\res\\drawable\\c.jpg");
    private ImageIcon myImage3 = new ImageIcon ("E:\\SOFTWARE\\TrainPIS\\res\\drawable\\yellow.png");
    private ImageIcon myImage4 = new ImageIcon ("E:\\SOFTWARE\\TrainPIS\\res\\drawable\\yellow.png");
    private ImageIcon[] myImages = new ImageIcon[4];

 private int curImageIndex=0;
 private JButton jButton1;
    private JButton jButton2;
    private JPanel jPanel1;
      private JLabel jLabel1;

    public MyGui3() {
        jPanel1 = new JPanel();
        jLabel1 = new JLabel(myImage1);

         myImages[0]=myImage1;
            myImages[1]=myImage2;
            myImages[2]=myImage3;
            myImages[3]=myImage4;
        jButton1 = new JButton();
        jButton2 = new JButton();

       //ImageGallery.add(new JLabel (myImage1));
        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        setPreferredSize(new java.awt.Dimension(1386, 768));

        jPanel1.setBackground(new java.awt.Color(153, 153, 255));

        jLabel1.setText("jLabel1");

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addGap(197, 197, 197)
                .addComponent(jLabel1, javax.swing.GroupLayout.PREFERRED_SIZE, 249, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(141, Short.MAX_VALUE))
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addGap(54, 54, 54)
                .addComponent(jLabel1, javax.swing.GroupLayout.PREFERRED_SIZE, 284, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(130, Short.MAX_VALUE))
        );

        jButton1.setText("jButton1");

        jButton2.setText("jButton2");

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addContainerGap(455, Short.MAX_VALUE)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jButton1, javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(jButton2, javax.swing.GroupLayout.Alignment.TRAILING))
                .addGap(124, 124, 124)
                .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(137, 137, 137))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addContainerGap(157, Short.MAX_VALUE)
                .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(143, 143, 143))
            .addGroup(layout.createSequentialGroup()
                .addGap(325, 325, 325)
                .addComponent(jButton1)
                .addGap(18, 18, 18)
                .addComponent(jButton2)
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );



  jButton1.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent ae){
//OptionPane.showMessageDialog(frame,"Welcome to allhabad High Court");

if(curImageIndex>0 && curImageIndex <= 3)
                    { jPanel1.remove(0);
                        curImageIndex=curImageIndex-1;
                        ImageIcon TheImage= myImages[curImageIndex];

                        jLabel1 = new JLabel(TheImage);
                        jPanel1.validate();
                        jPanel1.repaint(); 
                    }
                else 
                    {   
                        jPanel1.remove(0);
                      jLabel1 = new JLabel(myImage1);
                        curImageIndex=0;
                        jPanel1.validate();
                        jPanel1.repaint();
                    }

}
});

   jButton2.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent ae){
//JOptionPane.showMessageDialog(frame,"Welcome to allhabad High Court");


    if(curImageIndex>=0 && curImageIndex < 3)
                {    jPanel1.remove(0);
                    curImageIndex = curImageIndex + 1;
                    ImageIcon TheImage= myImages[curImageIndex];
                   jLabel1 = new JLabel(TheImage);
                    jPanel1.validate();
                    jPanel1.repaint(); 
                }
            else 
                {   
                    jPanel1.remove(0);
                   jLabel1 = new JLabel(myImage4);
                    curImageIndex=3;
                    jPanel1.validate();
                     jPanel1.repaint();
                }


}
});

        setTitle("Find");
        pack();
        setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
    }

    public static void main(String args[]) {
       MyGui3 g1=new MyGui3();

                g1.setVisible(true);
                g1.setExtendedState(JFrame.MAXIMIZED_BOTH);
                g1.setBackground(Color.yellow);
    }
}

Thanks in advance

The group layout is intended for UI designing software, rather than humans. Avoid using it.

Instead, you have quite a selection of built-in layout managers, and it's not very difficult to implement a custom one in case you need special behavior that cannot (or should not) be achieved by nesting layouts (by nesting components with different layout).

I'm not sure what's the layout you're trying to achieve, but if you're referring to a list of images that you can scroll down in a JScrollPane or something like that, you should consider FlowLayout or even BoxLayout. There are other layouts you can wield to this task, but these two would be the simplest to use.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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