简体   繁体   English

如何使用grouplayout创建图像滑块?

[英]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 嗨,我正在尝试创建创建桌面应用程序,我正在使用组布局创建mu Gui我试图创建图像滑块,但是当我运行我的程序时,我无法实现该方法,它抛出ArrayIndexOutofBound异常,请解决我的问题

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. 小组布局旨在用于UI设计软件,而不是人类。 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. 我不确定要实现的布局是什么,但是如果要引用的图像列表可以在JScrollPane中向下滚动或类似的内容,则应考虑FlowLayout或BoxLayout。 There are other layouts you can wield to this task, but these two would be the simplest to use. 您可以使用其他布局来完成此任务,但是这两种布局最容易使用。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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