我正在构建一个方程式求解器,允许用户输入方程式系统,然后获得答案。因此,我需要构建一个允许用户输入方程组的GUI。我决定从头开始。这是我的代码:
private JFrame frame;
private JTextField[][] text;
private JLabel label;
private JLabel equalLabel;
private JLabel plusLabel;
private JTextField answerText;
private void displayGetEquations(int total)
{
frame = new JFrame("Enter Equations");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
equalLabel = new JLabel();
equalLabel.setText(" = ");
plusLabel = new JLabel();
plusLabel.setText(" + ");
answerText = new JTextField();
answerText.setColumns(2);
frame.setLayout(new GridLayout(0,10));
text = new JTextField[3][3];
for(int i = 0 ; i < 3 ; i++)
{
panel = new JPanel();
panel.setLayout(new FlowLayout());
for(int j = 0 ; j < 3 ; j++)
{
text[i][j] = new JTextField();
text[i][j].setColumns(2);
label = new JLabel();
Font font = label.getFont();
Font boldFont = new Font(font.getFontName(), Font.BOLD, font.getSize());
label.setFont(boldFont);
label.setText("x");
panel.add(text[i][j]);
panel.add(label);
panel.add(plusLabel);
}
panel.add(equalLabel);
panel.add(answerText);
frame.add(panel);
frame.revalidate();
frame.repaint();
}
frame.pack();
frame.setVisible(true);
}
这是我运行上面的代码时得到的:
但是我没有得到想要的输出,我希望是这样的:
这是使用NetBeans的拖放功能实现的。我在做什么错?我想要的甚至有可能吗?
任何帮助,将不胜感激。
谢谢 !
(这是使用NetBeans的拖放功能完成的)
最佳答案
您的代码有点混乱,这里发布的代码不是MCVE,因为它缺少main
方法和导入以及变量panel
,并且给我的输出不同于您的输出。发表:
从示例输出图像中,我做了两种方法,一种使用多个JPanel
和一个FlowLayout
,另一种使用BoxLayout
,另一个使用一个JPanel
和GridLayout
。另一种方法可能是使用GridBagLayout
我使用了x
,y
和z
变量名,因为我认为您正在使用方程式求解程序或类似的程序。
这是产生上述输出的代码:
import java.awt.Color;
import java.awt.FlowLayout;
import java.awt.Font;
import java.awt.GridLayout;
import javax.swing.BorderFactory;
import javax.swing.BoxLayout;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;
public class MultipleComponentsInRow {
public static final int ROWS = 3;
public static final int COLS = 4;
private JFrame frame;
private JPanel flowLayoutPanel;
private JPanel gridLayoutPanel;
private JTextField[][] flowFields;
private JTextField[][] gridFields;
private void createAndShowGui() {
frame = new JFrame("Multiple Components In Row");
flowFields = new JTextField[ROWS][COLS];
gridFields = new JTextField[ROWS][COLS];
flowLayoutPanel = new JPanel();
flowLayoutPanel.setLayout(new BoxLayout(flowLayoutPanel, BoxLayout.PAGE_AXIS));
gridLayoutPanel = new JPanel();
gridLayoutPanel.setLayout(new GridLayout(ROWS, 7));
for (int i = 0; i < ROWS; i++) {
for (int j = 0; j < COLS; j++) {
flowFields[i][j] = new JTextField(2);
gridFields[i][j] = new JTextField(2);
}
}
//FlowLayout approach
for (int i = 0; i < ROWS; i++) {
JPanel pane = new JPanel();
pane.setLayout(new FlowLayout());
for (int j = 0; j < COLS; j++) {
pane.add(flowFields[i][j]);
switch (j) {
case 0:
pane.add(new JLabel(" x + "));
break;
case 1:
pane.add(new JLabel(" y + "));
break;
case 2:
pane.add(new JLabel(" z = "));
break;
default:
break;
}
}
flowLayoutPanel.add(pane);
}
//GridLayout approach
for (int i = 0; i < ROWS; i++) {
for (int j = 0; j < COLS; j++) {
gridLayoutPanel.add(gridFields[i][j]);
switch (j) {
case 0:
gridLayoutPanel.add(new JLabel(" x + "));
break;
case 1:
gridLayoutPanel.add(new JLabel(" y + "));
break;
case 2:
gridLayoutPanel.add(new JLabel(" z = "));
break;
default:
break;
}
}
}
frame.setLayout(new GridLayout(1, 2, 5, 5));
flowLayoutPanel.setBorder(BorderFactory.createTitledBorder(BorderFactory.createLineBorder(Color.BLACK), "Flow Layout"));
gridLayoutPanel.setBorder(BorderFactory.createTitledBorder(BorderFactory.createLineBorder(Color.RED), "Grid Layout"));
frame.add(flowLayoutPanel);
frame.add(gridLayoutPanel);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.pack();
frame.setVisible(true);
}
public static void main(String[] args) {
new MultipleComponentsInRow().createAndShowGui();
}
}