我有一个项目要求我创建一个简单的GUI,顶部有一个jTextArea,JButtons从0到9添加。这很简单,但程序要求我将0置于底部。下面的代码完成了,但是我必须使用Grid Layout而不是flowLayout。当我用GridLayout编写它时,除了左侧,我无法将0对齐到任何东西。如何使用GridLayout,并将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
}
}
答案 0 :(得分:6)
通过使用BorderLayout就是这么简单。将TextField添加到North。现在只需将九个按钮添加到JPanel。将该JPanel添加到中心。最后将Zero按钮添加到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
}
}
<强>结果强>
答案 1 :(得分:4)
将空的JPanel添加到您想要为空的网格的方块中。
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
编辑:删除了一个额外无意中出现的单词&#34;为空。&#34;
答案 2 :(得分:3)
让布局和简单的2D字符串数组帮助您。例如,如果为按钮文本声明一个2D字符串数组,如下所示:
private static final String[][] TEXTS = {
{"7", "8", "9"},
{"4", "5", "6"},
{"1", "2", "3"},
{"", "0", ""}
};
然后遍历数组,在文本不是""
的地方创建按钮,在空的JLabel中创建空白,并使用JPanel将按钮放入GridLayout,你就在那里。我还建议嵌套JPanels,外部使用BorderLayout,将JTextField保持在顶部 - BorderLayout.PAGE_START,另一个JPanel使用GridLayout,按住按钮并使用JPanel放置在外部BorderLayout的BorderLayout.CENTER位置。例如:
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();
}
});
}
}