[英]Question about making a JAVA GUI of a certain format
I am trying to make a GUI that looks something like this: 我正在尝试制作一个看起来像这样的GUI:
I only know how to use the BorderLayout which has space for 5 buttons. 我只知道如何使用具有5个按钮空间的BorderLayout。 North, West, Center, East, and South.
北,西,中,东和南。
Since I need to have 6 components on the top line, this approach can't work. 由于我需要在顶部有6个组件,因此这种方法行不通。 I'm not sure how to make it so that I can have more than 1 component on the top line.
我不确定如何做到这一点,这样我就可以在第一行上包含多个组件。 Are there other layouts that I can use or is there some way I can manipulate BorderLayout so that I can put 6 components on the top line?
还有其他可以使用的布局,还是可以操纵BorderLayout的某种方式,以便可以在顶部放置6个组件?
What you need to do is nest components inside of other components. 您需要做的是将组件嵌套在其他组件中。 For example, the top (North) should be one
JPanel
. 例如,顶部(北)应为一个
JPanel
。 That JPanel
will contain the 6 components on the top. 该
JPanel
将在顶部包含6个组件。
The code may look similar to the following: 该代码可能类似于以下内容:
JPanel northPane = new JPanel();
northPane.add(new JLabel("Principle: "));
northPane.add(principleTextBox);
... and so on
mainPanel.setLayout(new BorderLayout());
mainPanel.add(northPanel, BorderLayout.NORTH);
The Center component will probably be another JPanel
containing the two center buttons. 中心组件可能是另一个包含两个中心按钮的
JPanel
。 And the South component will be another JPanel
containing the single JLabel
or simply the JLabel
. South组件将是另一个
JPanel
其中包含单个JLabel
或仅包含JLabel
。
If you don't have to use a BorderLayout
for the main panel, it may be easier to use a BoxLayout
. 如果您不必在主面板上使用
BorderLayout
,则使用BoxLayout
可能会更容易。
Once again I turn to miglayout , the absolute best layout manager for Java. 我再次转向miglayout ,这是Java绝对最佳的布局管理器。 No nested JPanels, just a simple layout using string based constraints.
没有嵌套的JPanels,只有使用基于字符串的约束的简单布局。
With debug mode on: 打开调试模式:
After resizing the window (note the ratio of the size of the textfields remains the same) 调整窗口大小后(请注意文本字段的大小比例保持不变)
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;
import net.miginfocom.swing.MigLayout;
/**
*
* @author nicholasdunn
*/
public class InterestCalculator extends JPanel {
public InterestCalculator() {
super(new MigLayout("debug, fill", "align center"));
// Make 6 components cram into one cell
add(new JLabel("Principal:"), "split 6");
// This textfield grows at twice the normal rate
add(new JTextField(), "growx 200");
add(new JLabel("Interest rate (percentage):"));
// This one at a normal rate
add(new JTextField(), "growx 100");
add(new JLabel("Years:"));
// This one at half the normal rate
add(new JTextField(), "growx 50, wrap");
// The row with the two buttons
add(new JButton("Compute simple interest"), "split 2");
add(new JButton("Compute compound interest"), "wrap");
// The result label
add(new JLabel("The result with simple interest would be"));
}
public static void main(String[] args) {
JFrame frame = new JFrame("");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JPanel panel = new InterestCalculator();
frame.add(panel);
frame.pack();
frame.setVisible(true);
}
}
If I were recreating that UI, I would start with a JPanel using a GridLayout with 3 rows and 1 column. 如果要重新创建该UI,我将从使用3行1列GridLayout的JPanel开始。 In each column I would add a child JPanel.
在每一列中,我将添加一个子JPanel。
Then for each row I would use a GridBagLayout to position the components. 然后,对于每一行,我将使用GridBagLayout定位组件。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.