我如何使输出进入不同的列? [英] How do i make the output come in different columns?

查看:134
本文介绍了我如何使输出进入不同的列?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



如何输出一个程序,一列中的数字是1-10,另一列中的数字是这个数字,第三个是立方体中的数字。我可以使程序更好一点,所以列真的不同。
对于每一列,我都希望添加一个标题(数字,数字平方,数字立方体)。

//对不起,我的英文不好

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

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

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

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

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

$ b JOptionPane.showMessageDialog(null,string,Data,
JOptionPane.PLAIN_MESSAGE);
setSize(200,200);
setVisible(true);
i = 0;
}}
public static void main(String [] args){new Del2upp3();}
}

使用HTML格式化一个字符串是一个很好的和经常被忽略的想法(+1到$ Maroun)

经常会看到的其他东西是 JOptionPane 使用 JComponent s



  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){
TableOptionPane(); (){

$ b $公共TableOptionPane(){
EventQueue.invokeLater(new Runnable(){
@Override
public void run(){
试试{
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
} catch(ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex){
}

整数[ ] [] values = new Integer [10] [3];
StringBuilder string = new StringBuilder();
(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 (型号);
JOptionPane.showMessageDialog(null,new JScrollPane(table),Data,JOptionPane.PLAIN_MESSAGE);
}
});






$你可以支持这样的操作,如复制,甚至数据的可编辑性,如果这样的东西对你来说是有用的... ...

你可以在如何使用对话框


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("\n     ").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();} 
   }

解决方案

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

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

这篇关于我如何使输出进入不同的列?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆