在JFrame中格式化结果

吉姆

我试图使我的结果在JFrame中看起来像下面的样子:

Cylinder 1
Radius = 5
Height = 5
Volume = 392.7

Cylinder 2
Radius = 5
Height = 5
Volume = 392.7

Cylinder 3
Radius = 5
Height = 5
Volume = 392.7

相反,我得到:

Cylinder 1 Radius = 5 Height = 5
Volume = 392.7  Cylinder 2
Radius = 5  Height = 5
Volume = 392.7 Cylinder 3
Radius = 5 Height = 5
Volume = 392.7

我尝试重新调整框架大小,\ n等,但是没有运气。如果我加大它,只会延长数据线的长度。我不能做的足够小,无法在每一行上得到结果。我的代码在下面,任何人都可以告诉我我想念的是什么。谢谢!!

public void actionPerformed(ActionEvent e)
            {
                // Output JFrame variables
                JLabel labelCylinder[] = new JLabel[3];
                JLabel labelRadius[] = new JLabel[3];
                JLabel labelHeight[] = new JLabel[3];
                JLabel labelVolume[] = new JLabel[3];
                JFrame outputFrame = new JFrame("Results");
                outputFrame.setBounds(150, 150, 325, 150);
                outputFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                outputFrame.setLayout(new FlowLayout());

                // For loop to output results
                for (int o=0; o<myCylinder.length;o++)
                {
                    myCylinder[o] = new Cylinder(Double.parseDouble(textRadius[o].getText()), Double.parseDouble(textHeight[o].getText()));


                    labelCylinder[o] = new JLabel();
                    labelCylinder[o].setText(" Cylinder " + (o+1) + "\n");
                    labelRadius[o] = new JLabel();
                    labelRadius[o].setText("\nRadius = " + myCylinder[o].getRadius() + "\n");
                    labelHeight[o] = new JLabel();
                    labelHeight[o].setText("\nHeight = " + myCylinder[o].getHeight() + "\n");
                    labelVolume[o] = new JLabel();
                    labelVolume[o].setText("\nVolume = " + myCylinder[o].volume() + "\n");

                    outputFrame.add(labelCylinder[o]);
                    outputFrame.add(labelRadius[o]);
                    outputFrame.add(labelHeight[o]);
                    outputFrame.add(labelVolume[o]);


                }
选修的

更改outputFrame.setLayout(new FlowLayout())为(单列)GridLayoutBoxLayout为此。请参阅使用布局管理器布局管理器视觉指南

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章