【问题标题】:How to create image slider in swing using grouplayout?如何使用 grouplayout 在 swing 中创建图像滑块?
【发布时间】:2014-04-15 08:13:46
【问题描述】:

嗨,我正在尝试创建桌面应用程序以使用我正在使用组布局创建 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);
    }
}

提前致谢

【问题讨论】:

    标签: java swing grouplayout


    【解决方案1】:

    组布局适用于 UI 设计软件,而不是人类。避免使用它。

    相反,您有相当多的内置布局管理器可供选择,如果您需要通过嵌套布局无法(或不应)实现的特殊行为(通过嵌套组件不同的布局)。

    我不确定您要实现的布局是什么,但如果您指的是可以在 JScrollPane 或类似内容中向下滚动的图像列表,您应该考虑 FlowLayout 甚至 BoxLayout。您可以使用其他布局来完成此任务,但这两种布局是最容易使用的。

    【讨论】:

    • 我使用了布局管理器,但在管理标签和按钮时遇到问题
    • 你能上传你想要达到的目标的草图吗?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-07-30
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多