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())これを (一列)GridLayoutまたはに変更しBoxLayoutます。レイアウト マネージャーの使用とレイアウト マネージャービジュアル ガイドを参照してください。

于 2013-09-22T02:43:33.783 に答える