Reputation: 11
I have a project that requires me to create a simple GUI that has a jTextArea at the top, and JButtons added from 0 to 9. This is pretty simple to do, however the program requires me to center the 0 at the bottom. The below code accomplishes that, however I have to use Grid Layout instead of flowLayout. When I write it with GridLayout, I cannot get the 0 to align to anything but the left side. How can I use a GridLayout, and center the 0?
public class CalculatorProject {
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
GUI gui = new GUI();
}
}
public class GUI extends JFrame {
public GUI() {
JFrame aWindow = new JFrame("Calculator");
aWindow.setBounds(30, 30, 175, 215); // Size
aWindow.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JTextField jta = new JTextField(20);
FlowLayout flow = new FlowLayout(); // Create a layout manager
flow.setHgap(5); // Set the horizontal gap
flow.setVgap(5);
flow.setAlignment(FlowLayout.CENTER);
Container content = aWindow.getContentPane(); // Get the content pane
content.setLayout(new GridLayout(4,3,5,5));
content.setLayout(flow); // Set the container layout mgr
content.add(jta);
// Now add six button components
int array[] = {7, 8, 9, 4, 5, 6, 1, 2, 3, 0};
for (int i = 0; i <= 9; i++) {
content.add(new JButton("" + array[i]));
}
aWindow.setVisible(true); // Display the window
}
}
Upvotes: 1
Views: 2373
Reputation: 867
Add an empty JPanel to the squares of the grid that you would like to be empty.
int array[] = {7, 8, 9, 4, 5, 6, 1, 2, 3, 0};
for (int i = 0; i <= 8; i++) {
content.add(new JButton("" + array[i]));
}
content.add(new JPanel());
content.add(new JButton("" + array[9]));
content.add(new JPanel()); // not needed, but fills the last square of the grid
Edit: Removed an extra unintentional occurrence of the word "empty."
Upvotes: 4
Reputation: 687
By using BorderLayout it is that simple. Add the TextField to the North. Now just add the nine buttons to JPanel. Add that JPanel to the Center. At last add the Zero button to the South.
public class CalculatorProject {
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
@Override
public void run() {
GUI gui = new GUI();
}
});
}
}
class GUI extends JFrame {
GUI() {
super("Calculator");
setBounds(30, 30, 160, 190); // Size
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JTextField jta = new JTextField(20);
JPanel panel = new JPanel();
setLayout(new BorderLayout(5, 5));
Container content = this.getContentPane(); // Get the content pane
content.add(jta, BorderLayout.NORTH);
// Now add six button components
int array[] = {7, 8, 9, 4, 5, 6, 1, 2, 3};
for (int i = 0; i < 9; i++) {
panel.add(new JButton("" + array[i]));
}
content.add(panel, BorderLayout.CENTER);
content.add(new JButton("0"), BorderLayout.SOUTH);
setVisible(true); // Display the window
}
}
Result
Upvotes: 6
Reputation: 285450
Let layouts and a simple 2D String array help you out. For example, if you declare a 2D String array for your button texts like so:
private static final String[][] TEXTS = {
{"7", "8", "9"},
{"4", "5", "6"},
{"1", "2", "3"},
{"", "0", ""}
};
And then loop through the array, creating buttons wherever the text is not ""
empty, and an empty JLabel where it is empty, and place the buttons into a GridLayout using JPanel, you're there. I also recommend nesting JPanels, the outer using a BorderLayout, holding the JTextField at the top -- BorderLayout.PAGE_START, and the other JPanel using a GridLayout, holding the buttons and placed in the BorderLayout.CENTER position of the outer BorderLayout using JPanel. For example:
import java.awt.BorderLayout;
import java.awt.GridLayout;
import java.util.ArrayList;
import java.util.List;
import javax.swing.*;
public class Gui2 extends JPanel {
private static final String[][] TEXTS = {
{"7", "8", "9"},
{"4", "5", "6"},
{"1", "2", "3"},
{"", "0", ""}
};
private List<JButton> buttons = new ArrayList<>();
private JTextField textField = new JTextField(5);
public Gui2() {
int rows = TEXTS.length;
int cols = TEXTS[0].length;
int gap = 2;
JPanel gridPanel = new JPanel(new GridLayout(rows, cols, gap, gap));
for (int r = 0; r < TEXTS.length; r++) {
for (int c = 0; c < TEXTS[r].length; c++) {
String text = TEXTS[r][c];
if (!text.trim().isEmpty()) {
JButton button = new JButton(text);
gridPanel.add(button);
buttons.add(button);
// add ActionListener here
} else {
// empty String, so add a blank place-holder JLabel
gridPanel.add(new JLabel());
}
}
}
setBorder(BorderFactory.createEmptyBorder(gap, gap, gap, gap));
setLayout(new BorderLayout(gap, gap));
add(textField, BorderLayout.PAGE_START);
add(gridPanel, BorderLayout.CENTER);
}
private static void createAndShowGui() {
Gui2 mainPanel = new Gui2();
JFrame frame = new JFrame("Gui2");
frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
frame.getContentPane().add(mainPanel);
frame.pack();
frame.setLocationByPlatform(true);
frame.setVisible(true);
}
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
public void run() {
createAndShowGui();
}
});
}
}
Upvotes: 3