0

我试图让我的结果在 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]);


                }
4

1 回答 1

1

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

于 2013-09-22T02:43:33.783 回答