Java JFrame中的格式化结果
我试图让我的结果在JFrame中显示如下:Java JFrame中的格式化结果,java,swing,jlabel,Java,Swing,Jlabel,我试图让我的结果在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 H
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]);
}
public void actionPerformed(ActionEvent e)
{
//输出JFrame变量
JLabel labelCylinder[]=新JLabel[3];
JLabel labelRadius[]=新JLabel[3];
JLabel labelHeight[]=新的JLabel[3];
JLabel labelVolume[]=新JLabel[3];
JFrame outputFrame=新JFrame(“结果”);
输出框.立根(150150325150);
outputFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setLayout(新的FlowLayout());
//用于循环输出结果
对于(int o=0;o将outputFrame.setLayout(new FlowLayout())
更改为(单列)或此。请参阅&使用BoxLayout
而不是FlowLayout
使用不可编辑的jTextArea要更快地获得更好的帮助,请发布一个.BTW-该数据似乎是最适合以表格形式显示的类型(使用JTable
)。如果您只想使用一个JLabel,那么JLabel支持html标记。您可以在一个标签中使用label1.setText('Radius:somevalue
Height:someHeight
……);
已经使用了一个方框布局,现在在线程“AWT-EventQueue-0”中得到:Exceptionjava.awt.AWTError:BoxLayout无法共享。已经阅读了您发送的链接,已经在这些链接上尝试解决我的问题。如果您可以与main共享完整的runnable类,我可以尝试更改它。