使用3个可变大小和TextFields的JPanel创建JInternalFrame

时间:2014-10-29 17:01:23

标签: java swing arraylist jinternalframe

我正在尝试使用内部窗口创建一个窗口,使用JSpinner设置整数值,使用按钮生成JInternalFrame,并将JSpinner值作为参数传递。

这个JInternalFrame将包含3个JPannel,它们必须有colmuns,行等于JSpinner的值。

所以,我想到使用JFormatedTextFields的ArrayList和for将TextFields添加到ArrayList,我之前创建的类用一行创建JFormatedTextFields,但我不知道如何使用for添加值。

有代码:

的JDialog:

package br.edu.faculdadedosguararapes;

import java.awt.EventQueue;
import javax.swing.JDialog;
import javax.swing.JSpinner;
import java.awt.BorderLayout;
import javax.swing.JPanel;
import java.awt.FlowLayout;
import java.beans.PropertyVetoException;
import javax.swing.border.TitledBorder;
import javax.swing.border.EtchedBorder;
import javax.swing.JButton;
import javax.swing.SpinnerNumberModel;
import javax.swing.JScrollPane;
import java.awt.Color;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;

public class Soma extends JDialog {

    private JSpinner spinner;
    private JPanel panelLinhasEColunas;
    private JPanel panelTop;
    private JScrollPane scrollPanePrincipal;
    private JButton btnGerarMatrizes;
    public static void main(String[] args) {
        EventQueue.invokeLater(new Runnable() {
            public void run() {
                try {
                    Soma dialog = new Soma();
                    dialog.setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);
                    dialog.setVisible(true);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        });
    }

    /**
     * Create the dialog.
     * @throws PropertyVetoException 
     */
    public Soma() throws PropertyVetoException {
        setTitle("Soma de Matrizes");
        setModalityType(ModalityType.APPLICATION_MODAL);
        setBounds(100, 100, 573, 447);

        panelTop = new JPanel();
        getContentPane().add(panelTop, BorderLayout.NORTH);
        panelTop.setLayout(new FlowLayout(FlowLayout.CENTER, 5, 5));

        panelLinhasEColunas = new JPanel();
        panelLinhasEColunas.setBorder(new TitledBorder(new EtchedBorder(EtchedBorder.LOWERED, null, null), "Numero de Colunas e Linhas", TitledBorder.CENTER, TitledBorder.TOP, null, new Color(51, 51, 51)));
        panelTop.add(panelLinhasEColunas);

        spinner = new JSpinner();
        spinner.setModel(new SpinnerNumberModel(2, 2, 10, 1));
        panelLinhasEColunas.add(spinner);

        btnGerarMatrizes = new JButton("Gerar Matrizes");
        btnGerarMatrizes.addActionListener(new ActionListener() {

            public void actionPerformed(ActionEvent arg0) {
                int valor = (Integer) spinner.getValue();

                GerarMatrizes soma = new GerarMatrizes();
                soma.GerarSoma(valor, scrollPanePrincipal, btnGerarMatrizes);

                btnGerarMatrizes.setEnabled(false);
            }
        });
        panelLinhasEColunas.add(btnGerarMatrizes);

        scrollPanePrincipal = new JScrollPane();
        getContentPane().add(scrollPanePrincipal, BorderLayout.CENTER);

    }

}

创建JInternalFrame的类:

package br.edu.faculdadedosguararapes;

import java.awt.BorderLayout;
import java.awt.Component;
import java.awt.Dimension;
import java.awt.GridLayout;
import java.util.ArrayList;
import javax.swing.Box;
import javax.swing.JButton;
import javax.swing.JFormattedTextField;
import javax.swing.JInternalFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.border.EtchedBorder;
import javax.swing.border.TitledBorder;
import javax.swing.event.InternalFrameAdapter;
import javax.swing.event.InternalFrameEvent;

public class GerarMatrizes {

    public void GerarSoma(int valor, JScrollPane pane, final JButton botao){

        ArrayList<JFormattedTextField> campos = new ArrayList<JFormattedTextField>();

        JInternalFrame internalFrame = new JInternalFrame("");
        internalFrame.addInternalFrameListener(new InternalFrameAdapter() {
            @Override
            public void internalFrameClosed(InternalFrameEvent arg0) {
                botao.setEnabled(true);
            }
        });
        internalFrame.setClosable(true);
        internalFrame.setBorder(null);
        internalFrame.setVisible(true);
        pane.setViewportView(internalFrame);

        Component rigidArea = Box.createRigidArea(new Dimension(20, 20));
        internalFrame.getContentPane().add(rigidArea, BorderLayout.NORTH);

        Component rigidArea_1 = Box.createRigidArea(new Dimension(20, 20));
        internalFrame.getContentPane().add(rigidArea_1, BorderLayout.SOUTH);

        JPanel panel = new JPanel();
        internalFrame.getContentPane().add(panel, BorderLayout.CENTER);

        JPanel panelMatrizA = new JPanel();
        panelMatrizA.setBorder(new TitledBorder(new EtchedBorder(EtchedBorder.LOWERED, null, null), "Matriz A", TitledBorder.CENTER, TitledBorder.TOP, null, null));
        panel.add(panelMatrizA);
        panelMatrizA.setLayout(new GridLayout(valor, valor, 5, 5));

        Component horizontalStrut = Box.createHorizontalStrut(20);
        panel.add(horizontalStrut);

        JPanel panelMatrizB = new JPanel();
        panelMatrizB.setBorder(new TitledBorder(new EtchedBorder(EtchedBorder.LOWERED, null, null), "Matriz B", TitledBorder.CENTER, TitledBorder.TOP, null, null));
        panel.add(panelMatrizB);
        panelMatrizB.setLayout(new GridLayout(valor, valor, 5, 5));

        Component horizontalStrut_1 = Box.createHorizontalStrut(20);
        panel.add(horizontalStrut_1);

        JButton btnSomar = new JButton("Somar");
        panel.add(btnSomar);

        JPanel panelMatrizRes = new JPanel();
        panelMatrizRes.setBorder(new TitledBorder(new EtchedBorder(EtchedBorder.LOWERED, null, null), "Matriz Resultante", TitledBorder.CENTER, TitledBorder.TOP, null, null));
        panel.add(panelMatrizRes);
        panelMatrizRes.setLayout(new GridLayout(valor, valor, 5, 5));

        /*for (int i=1; i < valor; i++){
            campos.add(i);
        }*/

    }
}

创建JFormattedTextFields的类:

package br.edu.faculdadedosguararapes;

import javax.swing.JFormattedTextField;
import javax.swing.JPanel;
import javax.swing.SwingConstants;
import javax.swing.text.AbstractDocument;
import javax.swing.text.DocumentFilter;

public class GeraObjeto {

        public JFormattedTextField novoFtf(JFormattedTextField nome, JPanel panel){
            nome = new JFormattedTextField();
            nome.setHorizontalAlignment(SwingConstants.CENTER);
            nome.setColumns(2);

            DocumentFilter filtro = new FiltroNumero();
            ((AbstractDocument) nome.getDocument()).setDocumentFilter(filtro);

            panel.add(nome);
            return nome;
        }
}

1 个答案:

答案 0 :(得分:0)

好吧,我已经改变了

ArrayList<JFormattedTextField> campos = new ArrayList<JFormattedTextField>();

JFormattedTextField[] camposA = new JFormattedTextField[valor*valor]; GeraObjeto textField = new GeraObjeto();

所以用一个简单的方法来创建JFormattedTextFields(GeraObjeto)就像

for (int i=0; i < valor*valor; i++){
    camposA[i] = textField.novoFtf(camposA[i], panelMatrizA);
}

它运作良好。