简体   繁体   中英

How do I get input from JTextField from other panels

I am new to using JTextFields and was curious on how to get input from 3 different panels. The panels will take numbers from inputs and average the scores. For that, I want to access all 3 panels to do the math. I have a panel for three different people's scores that I want to average. So I would like to get mattsTotalScore and add it with timsTotalScore and BensTotalScore and make the average appear after a button on a fourth panel.

import javax.swing.*;
import java.awt.*;

public class MattPanel extends JPanel {

    public MattPanel(){
        Dimension size = getPreferredSize();
        size.width = 250;
        setPreferredSize(size);
        setBorder(BorderFactory.createTitledBorder("Matt's Scores"));

        JLabel memMatt = new JLabel("MEMORABILITY :");
        JTextField textMemMattPane = new JTextField(10);

        JLabel comMatt = new JLabel("COMPOSITION :");
        JTextField textComMattPane = new JTextField(10);

        JLabel mixMatt = new JLabel("MIX/MASTER :");
        JTextField textMixMattPane = new JTextField(10);

        JLabel soundMatt = new JLabel("SOUND DESIGN :");
        JTextField textSoundMattPane = new JTextField(10);

        JLabel enjoyMatt = new JLabel("ENJOYMENT :");
        JTextField textEnjoyMattPane = new JTextField(10);

        setLayout(new GridBagLayout());

        GridBagConstraints gc = new GridBagConstraints();

        ///First Column ///////////////////////
        gc.anchor = GridBagConstraints.LINE_END;
        gc.weightx = 0.5; gc.weighty = 0.5;
        gc.gridx = 0; gc.gridy = 0;
        add(memMatt, gc);

        gc.gridx = 0; gc.gridy = 1;
        add(comMatt, gc);

        gc.gridx = 0; gc.gridy = 2;
        add(mixMatt, gc);

        gc.gridx = 0; gc.gridy = 3;
        add(soundMatt, gc);

        gc.gridx = 0; gc.gridy = 4;
        add(enjoyMatt, gc);

        //Second Column //////////////////////
        gc.anchor = GridBagConstraints.LINE_START;
        gc.gridx = 1; gc.gridy = 0;
        add(textMemMattPane,gc);

        gc.gridx = 1; gc.gridy = 1;
        add(textComMattPane,gc);

        gc.gridx = 1; gc.gridy = 2;
        add(textMixMattPane,gc);

        gc.gridx = 1; gc.gridy = 3;
        add(textSoundMattPane,gc);

        gc.gridx = 1; gc.gridy = 4;
        add(textEnjoyMattPane,gc);
    }
}

You could try to use the getComponents() method from JPanel to get all components, although you would have to somehow identify the TextFields like:

MattPanel panel = new MattPanel();
int sum = 0;
int nr = 0;
for(Component comp : panel.getComponents()){
    if(comp instanceof JTextField){ 
        String text = ((JTextField)comp).getText();
        sum += Integer.parseInt(text);
        nr ++;
    }
}
double avg = sum/nr;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM