Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
440 views
in Technique[技术] by (71.8m points)

java - How do i make the output come in different columns?

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 little nicer so the columns really differ. And for every column i like to add a Title(Number, number squared, number cube).

//Sorry for my bad English

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;

public class Del2upp3 extends JFrame implements ActionListener 
{
    int i;
    JLabel label2 = new JLabel();
    JPanel panel = new JPanel();
    JButton button = new JButton("Press");
    Del2upp3()
    {
        super ("Panel"); setSize (200,300);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        Container con = this.getContentPane();
        con.add(panel);
        button.addActionListener(this);
        panel.add(label2);
        panel.add(button);
        setVisible(true);

    } 
    public void actionPerformed(ActionEvent event)
    {        
            Object source = event.getSource();
            if (source == button)  
        {

            StringBuilder string = new StringBuilder();
                for( i = 1; i< 11; i++){

                string.append("
     ").append(i);
                string.append(", ").append(i*i);
                string.append(", ").append(i*i*i);

        }
               JOptionPane.showMessageDialog(null, string,"Data",
               JOptionPane.PLAIN_MESSAGE);
               setSize (200,200);
               setVisible(true); 
                 i = 0;
         }}
        public static void main(String[] args){new Del2upp3();} 
   }
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

It's better to use JTable in your case.

But you can use HTML Tags and do something like this:

builder.append("<html><table border=1><tr><td>column1</td><td>column2</td><td></tr>");
string.append("<tr><td>");
string.append(.....);
string.append("</td><td>");
...
string.append("</td></tr>");
string.append("</table></html>");

Or:

textArea.setText("column1column2
");  
textArea.append("column1column2
");  
textArea.append(...);  

But it's not recommended since your text can go out of the column.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...