我试图让我的结果看起来像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]);
}
答案 0 :(得分:1)
为此,将outputFrame.setLayout(new FlowLayout())
更改为(单列)GridLayout
或BoxLayout
。见Using Layout Managers&amp; A Visual Guide to Layout Managers