How do i make the output come in different columns?

后端 未结 2 1584
一个人的身影
一个人的身影 2020-12-21 19:12

Im making a program that outputs, the numbers 1-10 in one column, the square of this in another and in the third the number in cube.

How can i make the program a lit

2条回答
  •  眼角桃花
    2020-12-21 19:35

    Formatting a String using HTML is an excellent and often overlooked idea (+1 to Maroun)

    Something else that often gets over looked is the ability for JOptionPane to use JComponents

    enter image description here

    import java.awt.EventQueue;
    import javax.swing.JOptionPane;
    import javax.swing.JScrollPane;
    import javax.swing.JTable;
    import javax.swing.UIManager;
    import javax.swing.UnsupportedLookAndFeelException;
    import javax.swing.table.DefaultTableModel;
    
    public class TableOptionPane {
    
        public static void main(String[] args) {
            new TableOptionPane();
        }
    
        public TableOptionPane() {
            EventQueue.invokeLater(new Runnable() {
                @Override
                public void run() {
                    try {
                        UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
                    } catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
                    }
    
                    Integer[][] values = new Integer[10][3];
                    StringBuilder string = new StringBuilder();
                    for (int i = 1; i < 11; i++) {
                        values[i - 1][0] = i;
                        values[i - 1][1] = i * i;
                        values[i - 1][2] = i * i * i;
                    }
                    DefaultTableModel model = new DefaultTableModel(values, new Object[]{"i", "i * i", "i * i * i"});
                    JTable table = new JTable(model);
                    JOptionPane.showMessageDialog(null, new JScrollPane(table), "Data", JOptionPane.PLAIN_MESSAGE);
                }
            });
        }
    }
    

    The has the added benifit of providing a bases from which you could support such operations as "copy" or even edibility of the data, if such things were of use to you...

    You may find more details at How to use Dialogs

提交回复
热议问题