简体   繁体   English

如何从其他面板从 JTextField 获取输入

[英]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.我是使用 JTextFields 的新手,并且对如何从 3 个不同的面板获取输入感到好奇。 The panels will take numbers from inputs and average the scores.小组将从输入中获取数字并对分数进行平均。 For that, I want to access all 3 panels to do the math.为此,我想访问所有 3 个面板来进行数学计算。 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.因此,我想获取 mattsTotalScore 并将其与 timsTotalScore 和 BensTotalScore 相加,并使平均值出现在第四个面板上的按钮之后。

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:您可以尝试使用 JPanel 中的getComponents()方法来获取所有组件,尽管您必须以某种方式识别 TextField,例如:

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;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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