2
I’m having trouble managing the layout of a Jframe that is a test.
How are you:
How I wish it were:
import java.awt.Color;
import java.awt.GridLayout;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;
import net.miginfocom.swing.MigLayout;
public class TesteMigImagem {
public TesteMigImagem() {
JFrame frame = new JFrame();
frame.setLayout(new GridLayout(1,1));
JPanel panel = new JPanel(new MigLayout());
JPanel imagem = new JPanel();
imagem.setBackground(Color.red);
panel.add(new JLabel("Codigo"));
panel.add(new JTextField(), "width 100%, wrap");
panel.add(new JLabel("Nome"));
panel.add(new JTextField(),"split, grow x");
panel.add(imagem, "width :100:, height :100:, spany 2, wrap");
panel.add(new JLabel("Endreço"));
panel.add(new JTextField(), "grow x, split");
panel.add(new JLabel("Numero"));
panel.add(new JTextField(), "grow x");
frame.add(panel);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(300, 300);
frame.setVisible(true);
}
public static void main(String args[]) {
new TesteMigImagem();
}
}
Basically you need to make a composition of layouts, try using Borderpane as the main, within it another pane, for example Flowlayout containing the label and the textfield of the code in NORTH; A Gridbaglayout or Miglayout containing name, address and number in the CENTER; the loose image inside the East. More details A Visual Guide to Layout Managers
– Math
@Math With the Miglayout it is not necessary to make panel composition. With it it is possible to make very flexible designs.
– utluiz
@utluiz you’re right, is that I hit my eye on his layout and I already saw good old Borderpane, rs.. I find it simpler, although there are numerous alternatives
– Math