Inputdialog vertically

Asked

Viewed 45 times

0

My problem is that I want to create a graphical interface with input fields vertically, but I can only create horizontally.

Code:

JTextField mapField = new JTextField(5);
JTextField tamField = new JTextField(5);
JTextField wordField = new JTextField(5);
JTextField politicaField = new JTextField(5);
JTextField numViasField = new JTextField(5);
JPanel myPanel = new JPanel();
myPanel.add(new JLabel("Mapeamento:"));
myPanel.add(mapField);
myPanel.add(Box.createVerticalStrut(15)); // a spacer
myPanel.add(new JLabel("Tamanho da Cache:"));
myPanel.add(tamField);
myPanel.add(Box.createVerticalStrut(15)); // a spacer
myPanel.add(new JLabel("Wors por bloco:"));
myPanel.add(wordField);
myPanel.add(Box.createVerticalStrut(15)); // a spacer
myPanel.add(new JLabel("Politica de substituição:"));
myPanel.add(politicaField);
myPanel.add(Box.createVerticalStrut(15)); // a spacer
myPanel.add(new JLabel("Numero de vias:"));
myPanel.add(numViasField);
  • 3

    Stackoverflow in Portuguese is not a forum. If you have found the answer to your question, do not edit the question, just answer it and mark it as accepted. It is also not necessary to put "solved" in the post title.

1 answer

3

Your problem is you’re not wearing any layout:

frame.setLayout(new GridLayout(x, y));
//x - é o numero de linhas
//y - é o numero de colunas

Example:

import java.awt.GridLayout;

import javax.swing.JButton;
import javax.swing.JFrame;

public class GridLayoutTest {

  public static void main(String[] args) {
    JFrame.setDefaultLookAndFeelDecorated(true);
    JFrame frame = new JFrame("GridLayout Test");
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.setLayout(new GridLayout(10, 1));
    frame.add(new JButton("Button 1"));
    frame.add(new JButton("Button 2"));
    frame.add(new JButton("Button 3"));
    frame.add(new JButton("Button 4"));
    frame.add(new JButton("Button 5"));
    frame.add(new JButton("Button 6"));
    frame.add(new JButton("Button 7"));
    frame.add(new JButton("Button 8"));
    frame.pack();
    frame.setVisible(true);
  }
}

Source

To help you understand better layout:

https://docs.oracle.com/javase/tutorial/uiswing/layout/using.html

Browser other questions tagged

You are not signed in. Login or sign up in order to post.