你如何使用JButton为JPanel打开一个新的JPanel + Transfer整数变量?

时间:2014-02-10 16:59:01

标签: java swing jpanel jbutton

我需要 1.JButton在Journal类中打开JTable 2.将JTextFields中的int转换为Journal Class

中的变量a和b

我使用netbeans来创建Main类和我自己创建表类的方法请帮忙!谢谢!这是主要类Netbeans告诉我在右键单击>时编辑的部分。活动>行动>行动执行

private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {                                         
     int data1 = Integer.parseInt(jTextField1.getText());
     int data2 = Integer.parseInt(jTextField2.getText());

}   

这是我的代码:

package Components;

/**
 *
 * @author dustinpx2014
 */

import java.awt.*;
import java.util.*;
import javax.swing.*;
import javax.swing.table.*;
public class Journal extends JPanel
{
    private JTable table;
    private int a;//Number of Students
    private int b;// Length of Trip
    public Journal() 
    {

        String colN1 = "Date";
        String colN2 = "Student"; 

        int c = a*2/b; // Determining # of Journals
        int col = c*2; // Creating two columns for every journal(Day and Student)
        String[] columnNames = new String[col]; //For Loop: inserting column names
        for(int colF = 0; colF < col; colF++)
        {
            if(colF%2 == 0)
            {
                columnNames[colF] = colN1;
            }
            else
            {
                columnNames[colF] = colN2;
            }
        }
        int row = b; //row = number of days
        int d = 1; //day number
        int s = 1; //student number
        int x = 0; //counter for the # of times students write
        int z = 1; // student number(no limit)
        int z1 = a/2; // student number 2
        int x1 = 0; // counter for the # of times students write 2
        int x2 = 0;
        int w = 1;
        Object[][] data = new Object[row][col];

        for (int col1 = 0; col1 < data[0].length; col1++)
        {
            for (int row1 = 0; row1<data.length; row1++)//goes through the table by column
            {
                if(d > b) //reseting the date
                {
                    d = 1;
                }
                if(s > a && x <= 2) //reseting student number and adds count
                {
                    s = 1;
                    x++;
                }
                if(z > a)
                {
                    z = 1;
                }
                if(z1 > a && x1 <= 2)
                {
                    z1 = 1;
                    x1++;
                }
                if (w>a)
                {
                    w++;
                }
                if(col1%2 == 0) //inserts the day
                {
                    data[row1][col1]= "Day " + d;
                    d++;
                }
                else if (s!=data[row1][col1])//inserts student number
                {
                    data[row1][col1]= s;
                    s++;
                    z++;
                    w++;
                }
                for (int col2 = 1; col2 < data[0].length; col2++)
                {
                    for (int row2 = 0; row2<data.length; row2++)//goes through the table by column
                    {
                        if(z == data[row2][col2] && col2%2!=0)//checks for repeats and replaces them
                        {

                            for (int y = 0; y < col; y++) //checking all columns
                            {
                                if (z1 == a/2 && x1 <= 5) //adds count
                                {
                                    x1++;
                                }
                                else if(z1 > a && x1 <= 5)
                                {
                                    z1 = 1;
                                    x1++;

                                }
                                if(z == data[row2][y])
                                {
                                    data[row2][col2] = z1;
                                    z1++;
                                    w++;
                                }

                            }
                        }
                    }
                }
                for (int row3 = 1; row3 < data.length; row3++)
                {
                    for (int col3 = 0; col3<data[0].length; col3++)//goes through the table by rows
                    {
                        if(w == data[row3][col3] && col3%2!=0)//checks for repeats and replaces them
                        {
                            for(int y2 = 0; y2 < col; y2++)
                            {
                                if(
                                row3<row-1 && 
                                row3> 1 && 
                                w==data[row3+1][y2] && 
                                w==data[row3-1][y2] &&
                                w==data[row3][y2]
                                ) //checks rows
                                {
                                    data[row3][col3] = 0;

                                }
                            }
                        }
                    }
                }
            }
        }

        JTable table = new JTable(data, columnNames);
        table.setPreferredScrollableViewportSize(table.getPreferredSize());
        JScrollPane scrollPane = new JScrollPane(table);
        add(scrollPane);
    }

    public JTable getTable() {
        return table;
    }

    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());
    }   

请帮忙!谢谢:))

1 个答案:

答案 0 :(得分:2)

  

我有两个类Main使用JTextFields,Jbutton和Journal使用Jtable扩展JPanel。我需要使用Main类来操作Journal类,如何使用JButton打开JPanel,如何在JTextField中使用变量并将它们应用于变量a和b?

我假设你的Main类有一个Journal字段,比如叫做journal,并且这个JPanel已经添加到你的main中。要调用Journal实例的方法,只需调用journal变量上的方法即可。例如,Journal应该有公共getter方法,允许Main查询其字段所持有的数据。

请注意,我没有在Journal中看到任何JTextField,但是如果Journal是我的类,我再给它一个公共方法,允许它从JTable中获取信息,并且在那个方法中,我' d调用JTable的一个方法来获取Journal方法可以返回的信息。您可以使用的一个合适的JTable方法是getValueAt(int row, int column)。实际上,您的Journal方法可以简单地包装该方法:

public class Journal {
  private JTable table;

  // ...... lots more code

  public Object getValueAt(int row, int column) {
    // first check that row and column are not out of bounds, and deal with it
    return table.getValueAt(row, column);
  }
}

现在,如果您还希望按下按钮显示此JTable,则需要将其添加到Main类中的一个容器中,然后在更改其内容后调用revalidate并在同一容器上重新绘制。了解和理解该容器使用的布局管理器非常重要,因为有些人比其他容器更容易接受新组件。