简体   繁体   English

Java Swing 在 1 JFrame 中的多个 JPanels 中更改 JTextField 的上下文

[英]Java Swing Change Context of JTextField in several JPanels In 1 JFrame

Maybe you can help me out here :)也许你可以在这里帮助我:)
I want to "simulate" like say 10 machine-stations.我想“模拟”比如 10 个机器站。 In my JFrame / Container (I tried both) I put these 10 maschines ( = 10 JPanels containing x buttons, textfields, whatever of my desired design), and I want to have different informations on each one and change them for my needs.在我的JFrame / Container (我两个都试过)中,我放置了这 10 个 maschines(= 10 个JPanels包含 x 按钮、文本字段,无论我想要什么设计),我希望每个都有不同的信息并根据我的需要更改它们。

I tried to change the value of a JTextField with an JButton (like setting the priority of the machine + 1. But I cannot distinguish between the 10 "priority up" buttons :(我尝试使用JButton更改JTextField的值(例如设置机器的优先级 + 1。但我无法区分 10 个“优先级向上”按钮:(

How you do that?你如何做到这一点? My idea was to speak somehow to the JPanel it came from but I can´t.我的想法是以某种方式与它来自的JPanel对话,但我不能。

    Container wizardFrame = new JFrame();
    wizardFrame.setLayout(new GridLayout(2,5));     

    String Name;
    for(int i = 1; i < 11; i++){

    Name = "Maschine " + i;

    fillWizardFrame(wizardFrame, Name, i);      
    }       
    wizardFrame.setVisible(true);       
}

public void fillWizardFrame(Container wizardFrame, String Name, int i) {

    JPanel MaschineId = new JPanel();
    MaschineId.setLayout(new BorderLayout());

    JTextField maschineName = new JTextField(Name ,10);
    MaschineId.add(maschineName, BorderLayout.WEST);
    maschinePrioritaet = new JTextField("20",10);
    MaschineId.add(maschinePrioritaet,BorderLayout.CENTER);
    JButton Higher = new JButton("Higher " + i);   Higher.addActionListener(this); 

    MaschineId.add(Higher, BorderLayout.NORTH);
    wizardFrame.add(MaschineId);        
}

@Override
public void actionPerformed(ActionEvent event) {
    if(event.getActionCommand().contains("Higher")){
        System.out.println("Higher pressed " + event.getActionCommand());
    }
       // i tried .getID , .getSource etc... :/

}

I want to change the value of maschinePrioritaet with my "higher" button, but I can´t... This thing took me hours of searching and trying but wasn´t able to find something.我想用我的“更高”按钮更改maschinePrioritaet的值,但我不能......这件事花了我数小时的搜索和尝试,但无法找到一些东西。

Thank you so much for your help!非常感谢你的帮助!
Best, Andrea最好的,安德里亚

You have to work more with objects and especially Views (like in MVC).您必须更多地使用对象,尤其是视图(如在 MVC 中)。 A View represents an object, in this case it looks like some machine (which is a name, an id and a priority).一个视图代表一个对象,在这种情况下它看起来像一台机器(它是一个名称、一个 ID 和一个优先级)。 So you need to create a machine panel that is attached to that model.因此,您需要创建一个附加到该模型的机器面板。

Here is something closer to that (but there are still many improvements to do):这里有一些更接近的东西(但仍有许多改进要做):

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

import javax.swing.JButton;
import javax.swing.JFormattedTextField;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.SwingUtilities;

public class Wizard {
    public Wizard() {
        JFrame wizardFrame = new JFrame();
        wizardFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        wizardFrame.setLayout(new GridLayout(2, 5));
        String name;
        for (int i = 1; i < 11; i++) {
            name = "Maschine " + i;
            MashinePanel mashinePanel = new MashinePanel(name, i);
            wizardFrame.add(mashinePanel.getPanel());
        }
        wizardFrame.pack();
        wizardFrame.setVisible(true);
    }

    public static class MashinePanel implements ActionListener {
        private final String name;
        private final int id;
        private JTextField maschineNameTF;
        private JFormattedTextField maschinePrioritaetTF;
        private JButton higher;
        private JPanel machinePanel;

        public MashinePanel(String name, int i) {
            super();
            this.name = name;
            this.id = i;
            machinePanel = new JPanel();
            machinePanel.setLayout(new BorderLayout());
            maschineNameTF = new JTextField(name, 10);
            machinePanel.add(maschineNameTF, BorderLayout.WEST);
            maschinePrioritaetTF = new JFormattedTextField(20);
            maschinePrioritaetTF.setColumns(10);
            machinePanel.add(maschinePrioritaetTF, BorderLayout.CENTER);
            higher = new JButton("Higher " + i);
            higher.addActionListener(this);
            machinePanel.add(higher, BorderLayout.NORTH);
        }

        public JPanel getPanel() {
            return machinePanel;
        }

        public String getName() {
            return name;
        }

        public int getId() {
            return id;
        }

        @Override
        public void actionPerformed(ActionEvent event) {
            if (event.getActionCommand().contains("Higher")) {
                Object value = maschinePrioritaetTF.getValue();
                int priority = 20;
                if (value instanceof Integer) {
                    priority = (Integer) value;
                }
                maschinePrioritaetTF.setValue(priority + 1);
            }
        }
    }

    public static void main(String[] args) {
        SwingUtilities.invokeLater(new Runnable() {
            @Override
            public void run() {
                new Wizard();
            }
        });
    }
}

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

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