ascending buttons from 0 to 99

32 Views Asked by At

I need my buttons to start on 0 on the bottom left corner and go to 99 all the way to the right top corner.. Can you please help me correct my code. Every chage I make to fix it, takes all the buttons lower and I dont need that

   private void crear()
   {
      for (i=0; i<100; i++)
      {
         btn_boton = new JButton(String.valueOf(i));
         btn_boton.setBounds(50+55*(i%10),325+25*(i/10),50,20);
         btn_boton.addActionListener(this);
         ventana.add(btn_boton);
      }
      ventana.repaint();

   }

enter image description here

Quest Button... and text fields, et

9
8
7
6
5
4
3
2
1
0  10  20  30  40  50  60  70  80  90
1

There are 1 best solutions below

0
MadProgrammer On

Make use of appropriate layouts. Start by having a looking at Laying Out Components Within a Container

The following example makes use of a GridBagLayout, I do this so I can move from the right to left across the row.

enter image description here

import java.awt.EventQueue;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;

public class Main {
    public static void main(String[] args) {
        new Main();
    }

    public Main() {
        EventQueue.invokeLater(new Runnable() {
            @Override
            public void run() {
                JFrame frame = new JFrame();
                frame.add(new TestPane());
                frame.pack();
                frame.setLocationRelativeTo(null);
                frame.setVisible(true);
            }
        });
    }

    public class TestPane extends JPanel {
        public TestPane() {
            setLayout(new GridBagLayout());
            GridBagConstraints gbc = new GridBagConstraints();

            int index = 99;
            for (int y = 0; y < 10; y++) {
                gbc.gridy = y;
                for (int x = 0; x < 10; x++) {
                    gbc.gridx = 10 - x;
                    add(new JButton(Integer.toString(index--)), gbc);
                }
            }
        }
    }
}