java - 在 JFrame 中格式化结果

标签 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  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为了这。请参阅Using Layout Managers & A Visual Guide to Layout Managers

关于java - 在 JFrame 中格式化结果,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18939700/

相关文章:

java - 涉及 Swing 和 AWT-EventQueue 的无响应线程

java - Swing Netbeans 构建器中不显示背景图像

java - Java 中的多行标签/区域/ Pane ,带有对齐文本

java - 如何在 JLabel 和 JTextField 上显示非科学格式的值?

java - 如何在 JLabel 中同时设置位置和设置文本?

java - 查找 .jar 文件运行的文件的名称

java - 如何从 Java 日志文件中提取错误信息(日志记录后)

Java:myComboBoxItemStateChanged 被触发了两次?

java - css 按钮图像未与 id 和样式类一起加载

java - 从 Jar 调用 HTML 文件