簡體   English   中英

將數據從左上方的單元格移至第一列的每一行

[英]Moving the data from the upper left cell to each row of the first column

我創建了一個顯示數據的小應用程序。 每個數字用空格分隔,但是所有數字都出現在表格的左上列中,而不是第一列的每一行中。 我努力工作了好一個小時才設法使它正常工作,但是不能-有人可以幫忙嗎

package stackoverflow;

import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.IOException;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.*;
import javax.swing.border.TitledBorder;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.select.Elements;
import javax.swing.JTextArea;
import com.sun.java.swing.*;
import javax.swing.table.*;
import java.awt.FlowLayout;
import javax.swing.JFrame;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.table.DefaultTableModel;

public class Learningfromscrach extends JFrame {

//declare all the parts that make up the GUI    
    private JLabel textJLabel;
    private JPanel PanelJlabel;//JLabel is actually a parameter in JAVA
    private TitledBorder PanelJborder;
    private JButton callsystem;
    private JTextArea txtarea;
//private JTable table;

    DefaultTableModel model;
    JTable table;
    String col[] = {"SBP", "SSP", "Period"};

    public Learningfromscrach() //tell java to initiate the create interface
    {
        createUserInterface();//create method private void createUserInterface//aframe is parameter this has to match the private void
    }

    //private Learningfromscrach() {
    //  throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
    // }
    private void createUserInterface() //all the parts to create the userinterface      
    {//from here
        Container contentPane = getContentPane();
        contentPane.setLayout(null);// i am responsible for setting positioning and size of components
        setTitle("Cashout Prices");//setTitle is also a JAVA Parameter

        Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
        setSize((int) (screenSize.width / 6), (int) (screenSize.height / 1.1));//cast int for width
        setVisible(true);    //makes the java application show

        PanelJborder = new TitledBorder("Cashout Prices");
        PanelJlabel = new JPanel(); //define new component
        PanelJlabel.setBounds(16, 50, 400, 2000);
        PanelJlabel.setLayout(null);

        contentPane.add(PanelJlabel); //add to Pane

        String url = "http://bmreports.com/bsp/additional/soapfunctions.php?element=SYSPRICE&dT=NRT";
        Document doc = null;
        try {
            doc = Jsoup.connect(url).get();
        } catch (IOException ex) {
            Logger.getLogger(Learningfromscrach.class.getName()).log(Level.SEVERE, null, ex);
        }
        Elements Periodparagraphs;
        Elements SSPparagraphs;
        Elements SBPparagraphs;

        Periodparagraphs = doc.select("SP");//counts the number of SSP Paragraphs in the entire document
        SSPparagraphs = doc.select("SSP");//counts the number of SSP Paragraphs in the entire document
        SBPparagraphs = doc.select("SBP");//counts the number of SBP Paragraphs in the entire document

        String[] numbers0 = Periodparagraphs.text().toString().split(" ");
        String[] numbers = SSPparagraphs.text().toString().split(" ");//takes the paragraph which contains all the SSP paragraphs and splits where the space occurs, stored in the array numbers
        String[] numbers1 = SBPparagraphs.text().split(" ");

        model = new DefaultTableModel(col, 90);//50 is number of rows    
        table = new JTable(model) {
            @Override

            public boolean isCellEditable(int arg0, int arg1) {
                return false;
            }
        };

        JTableHeader header = table.getTableHeader();
        header.setBackground(Color.yellow);
        contentPane.add(table);

        table.setValueAt(SBPparagraphs.text(), 1, 0); //first number is moves placing down by 2 rows//2nd number is next cclumn and so on
        //table.setValueAt("fgfg",0,0);

        table.setSize(screenSize.width / 4, (int) (screenSize.height / 1.1));
        table.setBounds(16, 50, 400, 2000);
        table.setLayout(null);
        table.setVisible(true);

        setDefaultCloseOperation(EXIT_ON_CLOSE);

        callsystem = new JButton();//creates new button
        callsystem.setText("Call System Prices");
        callsystem.setBounds(150, 16, 150, 24);
        contentPane.add(callsystem);
        callsystem.addActionListener(//created buttton that now Java listens for being pressed

                new ActionListener()//new listener event created by action
                {
                    public void actionPerformed(ActionEvent event) //action peformed action event event
                    {
                        callsystemActionPerformed(event);//call private void

                    }
                }
        );
    }

    private void callsystemActionPerformed(ActionEvent event) {

    }

//to hear this all refers to the contentpane

    public static void main(String[] args) {
        // TODO code application logic here
        Learningfromscrach application = new Learningfromscrach();//call learningfromscrach application

        application.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);//without this the userinterface cant be called

    }

    //next step to understand how to get data into the panel
}

您似乎沒有將數據添加到表中。

然后,嘗試添加以下內容:

  ...
    String[] numbers0 = Periodparagraphs.text().toString().split(" ");
    String[] numbers = SSPparagraphs.text().toString().split(" ");//takes the ...
    String[] numbers1 = SBPparagraphs.text().split(" ");

    int tableRows;

    if (numbers0.length > numbers.length && numbers0.length > numbers1.length)
    {
        tableRows = numbers0.length;
    }
    else if (numbers.length > numbers0.length && numbers.length > numbers1.length)
    {
        tableRows = numbers.length;
    }
    else
    {
        tableRows = numbers1.length;
    }
    //model = new DefaultTableModel(col, 90);//50 is number of rows --You don't seem to need this

    Object[][] data = new Object[tableRows][3];

    for (int x = 0; x < tableRows; x++ )
    {
        try
        {
            data[x][0] = numbers1[x];
        }
        catch (IndexOutOfBoundsException e)
        {
            data[x][0] = "-";
        }

        try
        {
            data[x][1] = numbers[x];
        }
        catch (IndexOutOfBoundsException e)
        {
            data[x][1] = "-";
        }

        try
        {
            data[x][2] = numbers0[x];
        }
        catch (IndexOutOfBoundsException e)
        {
            data[x][2] = "-";
        }
    }



    table = new JTable(data,col) 
    {
        @Override

        public boolean isCellEditable(int arg0, int arg1) 
        {
            return false;
        }
    };

另外,我也無法使您的項目正確顯示(真的)。 您可能需要探索Flow Layout(我更喜歡spring)之外的其他布局選項。 此外,JScrollPane確實會有所幫助。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM