My problem is making my Main class and Journal class connect together, in the Main class users will input two variables and click the button to create the table, in the Journal class it will read the two variables.
I used netbeans to create the Main class and my own methods to create the table class please help! Thanks!
This is the part of the Main class Netbeans tells me to edit when I right click > Events > Action > Action Performed
private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
}
and my Journal class, I tried making it read JTextField 1 and 2 but I do not know how to combine them.
import java.awt.*;
import java.util.*;
import javax.swing.*;
import javax.swing.table.*;
public class Journal extends JPanel
{
public Journal()
{
String colN1 = "Date";
String colN2 = "Student";
int a = Integer.parseInt(newJournal.jTextField1.getText());
int b = Integer.parseInt(newJournal.jTextField2.getText());
int c = a*2/b;
int col = c*2;
String[] columnNames = new String[col];
for(int colF = 0; colF < col; colF++)
{
if(colF%2 == 0)
{
columnNames[colF] = colN1;
}
else
{
columnNames[colF] = colN2;
}
}
int row = b;
int d = 1;
int s = 1;
int x = 0;
Integer f = new Integer(1);
Object[][] data = new Object[row][col];
for (int col1 = 0; col1 < data[0].length; col1++)
{
for (int row1 = 0; row1<data.length; row1++)
{
if(d > b)
{
d = 1;
}
else if(s > a || x > 2)
{
s = 1;
x++;
}
else if (row1 > row)
{
row1 = 0;
}
else if(col1%2 == 0)
{
data[row1][col1]= "Day " + d;
d++;
}
else
{
data[row1][col1]= s;
s++;
}
}
}
for (int col2 = 0; col2 < data[0].length; col2++)
{
for (int row2 = 0; row2<data.length; row2++)
{
if(s > a || x > 2)
{
s = 1;
x++;
}
else if(s!=data[row2][col2]&&col2%2!=0)
{
data[row2][col2] = s;
s++;
}
}
}
for (int row3 = 0; row3<data.length; row3++)
{
for (int col3 = 0; col3 < data[0].length; col3++)
{
int rowA = row3 + 1;
int rowS = row3 - 1;
int rows = 1;
int colA = col3 + 1;
int colS = col3 - 1;
int cols = 1;
if(s > a || x > 2)
{
s = 1;
x++;
}
else if(s==data[rowA][cols] && s == data[rowS][cols])
{
cols=cols+2;
}
else if (s == data[rows][colA] && s == data [rows][colS])
{
rows++;
}
else if(s!=data[row3][col3]&&col3%2!=0)
{
data[row3][col3] = s;
s++;
}
}
}
JTable table = new JTable(data, columnNames);
table.setPreferredScrollableViewportSize(table.getPreferredSize());
JScrollPane scrollPane = new JScrollPane(table);
add(scrollPane);
}
private static void gui()
{
JFrame gui = new JFrame();
gui.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
gui.setTitle("Journal List");
gui.setSize(700,200);
gui.setLocationRelativeTo(null);
gui.setVisible(true);
gui.add(new Journal());
}
public static void main(String[] args)
{
EventQueue.invokeLater(new Runnable()
{
public void run()
{
gui();
}
});
}
}
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…