1
데이터를 표시하는 작은 응용 프로그램을 만들었습니다. 각 숫자는 공백으로 구분되지만 모든 숫자는 첫 번째 열의 각 행보다는 테이블의 왼쪽 위 열에 나타납니다. 누군가가 당신이 테이블에 데이터를 추가 할 수하지 않는 것왼쪽 위 셀에서 첫 번째 열의 각 행으로 데이터 이동
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
}
먼저, 배열 'col []'을 String 유형 대신 Object 유형으로 변경하십시오 (JTable 생성자가 객체를 원함). 필요하지 않으며 덜 명확하고 명확한 코드가됩니다. – Joeblade
수정 됨 Joris 수정 됨. – barna10
@ Barna10 완벽하게 작동합니다. 나는 당신이 올바르게 표시한다는 것을 이해하지 못하거나 Spring과 같은 다른 옵션을 탐색합니다. 자바를 이해하는데 느린 진전을 보이고 있지만, 이제 코드가 사물의 전반적인 구조와 루프에 속하는지 어디 까지나 이해할 수 있습니다. 나는 꽤 많은 자바 서적을 샀지 만 JTables에는 그다지 많은 책이 없다. 나는 "SAMS stream"에서 "SAMS stream"을 통해 Java 2를 가르칩니다.이 모든 것이 어떻게 맞는지는 모르지만 오늘 연습 문제를 시도 할 것입니다. – Ingram