Reputation:
I'm trying to build a calculator GUI using migLayout, but I'm not familiar with this layout.
My problem is that my GUI is a straight line of buttons.
1 2 3 + 4 5 6 - ... etc
I would like to get
1 2 3 +
4 5 6 -
7 8 etc...
import net.miginfocom.swing.MigLayout;
import javax.swing.*;
import java.awt.*;
public class Calculator1 {
public static void main(String args[]) {
JFrame frame = new JFrame("Calculator1");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setLayout(new MigLayout());
frame.add(new JTextField(" "),"wrap");
frame.add(new JButton("1"));
frame.add(new JButton("2"));
frame.add(new JButton("3"));
frame.add(new JButton("+"));
frame.add(new JButton("4"));
frame.add(new JButton("5"));
frame.add(new JButton("6"));
frame.add(new JButton("-"));
frame.add(new JButton("7"));
frame.add(new JButton("8"));
frame.add(new JButton("9"));
frame.add(new JButton("*"));
frame.add(new JButton("0"));
frame.add(new JButton("/"));
frame.add(new JButton("="));
frame.pack();
frame.setVisible(true);
}
}
Upvotes: 0
Views: 1035
Reputation: 22741
Try this:
import net.miginfocom.swing.MigLayout;
import javax.swing.*;
import java.awt.*;
public class Calculator1 {
public static void main(String args[]) {
JFrame frame = new JFrame("Calculator1");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setLayout(new MigLayout("fill, wrap 4", "[25%][25%][25%][25%]", "[shrink]"));
frame.add(new JTextField(" "),"span 4, wrap");
frame.add(new JButton("1"));
frame.add(new JButton("2"));
frame.add(new JButton("3"));
frame.add(new JButton("+"));
frame.add(new JButton("4"));
frame.add(new JButton("5"));
frame.add(new JButton("6"));
frame.add(new JButton("-"));
frame.add(new JButton("7"));
frame.add(new JButton("8"));
frame.add(new JButton("9"));
frame.add(new JButton("*"));
frame.add(new JButton("0"));
frame.add(new JButton("/"));
frame.add(new JButton("="));
frame.pack();
frame.setVisible(true);
}
}
Upvotes: 1