如何生成可以插入面板的表格?

时间:2016-03-30 15:12:34

标签: java swing jtable

我真的很难将这些代码组装在一起。最后的计划是我有两个输入字段,一个要求输入名称,一个要求标记。然后我会输入一个名称和一个标记,只需按一下按钮,程序就会将这些值输入一个表格,通过一个总和可以为该标记指定一个等级(A,B,C等);最后,当输入所有值时,用户将按下另一个按钮,该按钮将在文本文件中打印一个名称列表及其适当的等级。那是最后一场比赛。

目前我正在努力创建表格并将其放置在我的框架的下半部分,以便每次按下一个按钮时它都会更新。我的框架的下半部分是'myBottomPanel'。如果你能告诉我并在我结束游戏的路上帮助我,我将非常感激。

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.BorderFactory;
import javax.swing.Box;
import javax.swing.BoxLayout;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.JTextField;

public class GradePanel2 extends JPanel {

    private JButton addEntry, calculate;
    private JLabel name, grade, nameResult, gradeResult;
    private JTextField nameField, gradeField;

    public GradePanel2() {
        // Button to add entry to list
        addEntry = new JButton("Add entry to list");
        addEntry.addActionListener(new buttonListener());
        // Button to print all entries in correct format
        calculate = new JButton("Print all user grades");
        calculate.addActionListener(new buttonListener());
        // Create Labels
        name = new JLabel("Enter student name: ");
        nameField = new JTextField(10);
        nameField.addActionListener(new buttonListener());
        grade = new JLabel("Enter students mark: ");
        gradeField = new JTextField(5);
        gradeField.addActionListener(new buttonListener());
        // Bottom segment for result
        setLayout(new BorderLayout());
        // Button Panel
        JPanel ButtonPane = new JPanel();
        ButtonPane.setLayout(new BoxLayout(ButtonPane, BoxLayout.PAGE_AXIS));
        addEntry.setAlignmentX(CENTER_ALIGNMENT);
        calculate.setAlignmentX(CENTER_ALIGNMENT);
        ButtonPane.add(Box.createVerticalStrut(15));
        ButtonPane.add(addEntry);
        ButtonPane.add(Box.createVerticalStrut(15));
        ButtonPane.add(calculate);
        // Label Panel
        JPanel labelPane = new JPanel();
        labelPane.setLayout(new BoxLayout(labelPane, BoxLayout.PAGE_AXIS));
        labelPane.add(name);
        labelPane.add(Box.createRigidArea(new Dimension(5, 0)));
        labelPane.add(nameField);
        labelPane.add(Box.createRigidArea(new Dimension(0, 2)));
        labelPane.add(grade);
        labelPane.add(Box.createRigidArea(new Dimension(5, 0)));
        labelPane.add(gradeField);
        myBottomPanel mybottompanel = new myBottomPanel();
        // Add all panels to the main panel
        add(labelPane, BorderLayout.NORTH);
        add(ButtonPane, BorderLayout.CENTER);
        add(mybottompanel, BorderLayout.SOUTH);
        setBackground(Color.WHITE);
        setPreferredSize(new Dimension(400, 300));
    }

    public class resultTable extends myBottomPanel {

        public void mainTable() {
            GridLayout mainLayout = (new GridLayout(1, 0));
            String[] columnNames = {"NAME", "GRADE"};
            Object[][] data = {{nameField.getText(), gradeField.getText()}};
            JTable table = new JTable(data, columnNames);
            table.setPreferredScrollableViewportSize(new Dimension(500, 70));
            table.setFillsViewportHeight(true);
            JScrollPane scrollPane = new JScrollPane(table);
            table.setFillsViewportHeight(true);
        }
    }

    public class myBottomPanel extends JPanel {

        public myBottomPanel() {
            resultTable results = new resultTable();
            add(results);
            setBorder(BorderFactory.createTitledBorder("Students/Results"));
            setOpaque(false);
        }
    }

    public class buttonListener implements ActionListener {

        @Override
        public void actionPerformed(ActionEvent event) {
            String studentName;
            int studentMark;
            if (event.getSource() == addEntry) {
                studentName = nameField.getText();
                String intMark = gradeField.getText();
                studentMark = Integer.parseInt(intMark);
                System.out.println(studentName);
                System.out.println(studentMark);
            }
        }
    }
}

最后我不确定如何为论坛正确粘贴代码,所以请原谅前几行错位;我刚给它们添加了4个空格,以便它们显示在代码中。

1 个答案:

答案 0 :(得分:2)

现有代码在尝试从java.lang.StackOverflowError构造函数递归调用MyBottomPanel构造函数时抛出GradePanel2

关注如何向表中添加行,您可以从这个完整的工作example开始。更改TableModel以符合您的使用案例,将一对文本字段添加到panel,然后更改addRow()以使用字段值。

image