gpt4 book ai didi

java - 将数据从左上角单元格移动到第一列的每一行

转载 作者:太空宇宙 更新时间:2023-11-04 14:28:45 27 4
gpt4 key购买 nike

我创建了一个显示数据的小应用程序。每个数字都用空格分隔,但是所有数字都出现在表的左上列中,而不是第一列的每一行中。我已经奋斗了一个小时试图让它工作但不能 - 有人可以帮忙

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 确实会有帮助。

关于java - 将数据从左上角单元格移动到第一列的每一行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26391972/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com