Java:GUI-JButton打开新的JPanel并读取JTextFields [英] Java: GUI - JButton opening new JPanel and reading the JTextFields

查看:88
本文介绍了Java:GUI-JButton打开新的JPanel并读取JTextFields的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的问题是使Main类和Journal类连接在一起,在Main类中,用户将输入两个变量,然后单击创建表的按钮,在Journal类中,它将读取这两个变量.

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.

我使用netbeans创建Main类,并使用自己的方法创建表类,请帮忙!谢谢! 这是主类的一部分,当我右键单击>事件>操作>执行的操作时,Netbeans告诉我进行编辑

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:
}                                        

和我的Journal类,我尝试将其读取为JTextField 1和2,但是我不知道如何将它们组合在一起.

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

推荐答案

当前,您的JTableJournal构造函数中位于本地范围内,因此 不能从其他任何地方访问.您要做的就是将其声明为类成员,并为其指定getter.

Currently your JTable is locally scoped in the Journal constructor, so it can't be accessed from anywhere else. What you want to do is declare it as a class-member and have a getter for it.

  public class Journal extends JPanel {
      private JTable table;

      public Journal() {
          table = new JTable(...);
      }

      public JTable getTable() {
          return table;
      }
  }

现在,可以使用getTable()Main类访问和检索table.

Now the table can be accessed and retrieved from the Main class using the getTable().

此外,您还想使用TableModel动态更新数据.可以使用已经实现的简单 DefaultTableModel .您想要将此模型设置为JTable.像这样

Also you'll want to use a TableModel to update data dynamically. Yo can use a simple, already implemented DefaultTableModel. You want to set this model to the JTable. Something like this

 public Journal() {
     ...
     DefaultTableModel model = new DefaultTableModel(data, columns);
     table = new JTable(model);
 }

因此,您已经为Main类设置了所有内容.在您的actionPerfomed中,您可以执行以下操作

So you have everything set in for your Main class. In your actionPerfomed you can do something like this

private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {                                         
    String data1 = textField1.getText();
    String data2 = textField2.getText();

    JTable table = journal.getTable();
    DefaultTableMode model = (DefaultTableModel)table.getModel();

    model.addRow( new Object[] { data1, data2 } );
}  


附带说明

  • 您不需要两个main方法,因为我假设Main类已经有一个main方法.
  • 也请参阅使用多个JFrame,很好/不良做法?.看来您的Journal类在其main方法中使用了JFrame,并且我假设您的Main类也是JFrame.您可以做的只是将Journal设为JDialog,然后将JTable添加到其中.
  • You don't need two main methods, as I assume the Main class already has a main method.
  • Also take a look at The Use of Multiple JFrames, Good/Bad Practice?. It looks like your Journal class use a JFrame in its main method, and I assume your Main class is a JFrame also. What you can do is just make Journal a JDialog and add the JTable to it.

这篇关于Java:GUI-JButton打开新的JPanel并读取JTextFields的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆