我正在为编码类编写矩阵项目的开始过程。我目前只是想让GUI看起来像我想要的那样。这是我希望它看起来的快速草图:
https://drive.google.com/file/d/1Hmt-9DNH6CGtV1eBJYSOu72Iu2cAqPe8/view?usp=sharing
(图片太大,无法上传,因此将云端硬盘连接起来是下一个最佳选择。)
基本上,用户可以输入2个矩阵的行和列,然后按下按钮进行计算。该程序将确保2个矩阵兼容,提示特定数字,然后最终计算将出现在标签中。
问题是我在Java方面仍然缺乏经验,这是我第一次使用超过1个JPanel。以下是我为Gui编写的代码:
import java.awt.*;
import javax.swing.*;
import javax.swing.border.Border;
public class GuiWindow {
public static void main(String[] args) {
// TODO Auto-generated method stub
JFrame myGUI = new JFrame();
myGUI.setTitle("Matrix Project");
myGUI.setSize(600,600);
myGUI.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
Border border = BorderFactory.createLineBorder(Color.BLACK, 1); //borders
JLabel AddLabel = new JLabel(); //creates JLabels
AddLabel.setBorder(border);
JLabel SubLabel = new JLabel();
SubLabel.setBorder(border);
JLabel MultLabel = new JLabel();
MultLabel.setBorder(border);
JPanel main = new JPanel(); //Adds JLabel panel
myGUI.add(main);
main.setLayout(new GridLayout(1, 3));
main.setAlignmentY(Component.TOP_ALIGNMENT);
main.add(MultLabel);
main.add(SubLabel);
main.add(AddLabel);
JTextField row1txt = new JTextField(); //textfields to input rows and columns
row1txt.setText("Matrix 1 rows...");
JTextField column1txt = new JTextField();
column1txt.setText("Matrix 1 columns...");
JTextField row2txt = new JTextField();
row2txt.setText("Matrix 2 rows...");
JTextField column2txt = new JTextField();
column2txt.setText("Matrix 2 columns...");
JPanel input = new JPanel(); //adds textfield panel
myGUI.add(input);
input.setLayout(new GridLayout (2, 2, 3, 3));
input.add(row1txt);
input.add(column1txt);
input.add(row2txt);
input.add(column2txt);
JButton Calc = new JButton(); //button to eventually calculate
Calc.setText("Calculate");
JPanel CalcButton = new JPanel(); //adds button panel
myGUI.add(CalcButton);
CalcButton.add(Calc);
myGUI.setVisible(true); //instantiates the gui
}
}
当我加载代码时,我看到的只是按钮。这是有道理的。我没有将3个JPanel分成不同的部分,因此Java只是将它们放在一起。当然,这里有一个问题:你如何将这些Jpanel分成JFrame的不同部分,这样它至少看起来像我的草图?
感谢任何帮助。谢谢!
答案 0 :(得分:0)
您要做的第一件事是阅读Laying Out Components Within a Container。
你可以使用复合GridLayout
来做到这一点,但是对于我的钱,GridBagLayout
提供了更大的灵活性。
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.GridLayout;
import javax.swing.JButton;
import javax.swing.JCheckBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;
public class Test {
public static void main(String[] args) {
new Test();
}
public Test() {
SwingUtilities.invokeLater(new Runnable() {
@Override
public void run() {
JFrame frame = new JFrame();
frame.add(new TestPane());
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
});
}
public class TestPane extends JPanel {
public TestPane() {
setLayout(new GridBagLayout());
GridBagConstraints gbc = new GridBagConstraints();
gbc.gridx = 0;
gbc.gridy = 0;
gbc.fill = GridBagConstraints.BOTH;
gbc.gridwidth = GridBagConstraints.REMAINDER;
gbc.weightx = 1;
gbc.weighty = 0.5;
add(new TopPane(), gbc);
gbc.weightx = 0.5;
gbc.gridy++;
gbc.fill = GridBagConstraints.BOTH;
gbc.gridwidth = 1;
add(new CheckerPane(), gbc);
gbc.gridx++;
add(new JButton("Button"), gbc);
}
}
public class TopPane extends JPanel {
public TopPane() {
setLayout(new GridLayout(0, 3));
add(new JLabel("label1", JLabel.CENTER));
add(new JLabel("label2", JLabel.CENTER));
add(new JLabel("label3", JLabel.CENTER));
}
}
public class CheckerPane extends JPanel {
public CheckerPane() {
setLayout(new GridBagLayout());
GridBagConstraints gbc = new GridBagConstraints();
gbc.gridx = 0;
gbc.gridy = 0;
add(new JCheckBox("Tick box 1"), gbc);
gbc.gridx++;
add(new JCheckBox("Tick box 2"), gbc);
gbc.gridx = 0;
gbc.gridy++;
add(new JCheckBox("Tick box 3"), gbc);
gbc.gridx++;
add(new JCheckBox("Tick box 4"), gbc);
}
}
}