编程语言
首页 > 编程语言> > java – JTextField在静态时会在某些页面上消失,而在其他页面上会出现

java – JTextField在静态时会在某些页面上消失,而在其他页面上会出现

作者:互联网

在处理程序时,我将一些字段静态化,即E-Number的JTextField.但是现在这个字段的行为并不像预期的那样,在我看来的某些页面上,在其他一些页面上消失了.
由于我在处理大量静力学方面不是很有经验,所以可能有一个我不理解的概念.

如果你想测试它,我已经创建了一个非常简化但有效的程序示例(MCVE).

它首先显示我的概述页面 – 缺少E-Number JTextField.

如果单击搜索按钮,它将显示跟踪页面 – 存在E-Number JTextField.

两个页面都包含相同的workNumberPanel,我找不到差异,这可以解释行为.

那么为什么电子数字JTextField出现在概述页面上并且在跟踪页面上丢失了?任何帮助/解释表示赞赏!

Static JTextField disappears

MainProgram.java

import java.awt.CardLayout;
import java.awt.EventQueue;

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.UIManager;

import net.miginfocom.swing.MigLayout;

public class MainProgram extends JFrame {
    private static final long serialVersionUID = 1L;

    public static JPanel centerPanel = new JPanel();

    public static CardLayout contentCardsLayout = new CardLayout();

    OverviewPage overviewPage = new OverviewPage();
    TrackingPage trackingPage = new TrackingPage();

    public void initialize() {
        createCenterPanel();
    }

    private void createCenterPanel() {
        centerPanel.setLayout(contentCardsLayout);

        overviewPage.setName("overviewPage");
        trackingPage.setName("trackingPage");

        centerPanel.add(overviewPage, "overviewPage");
        centerPanel.add(trackingPage, "trackingPage");

        add(centerPanel, "growx, wrap");
    }

    public MainProgram() {
        setBounds(300, 50, 1200, 900);
        setLayout(new MigLayout());
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    }

    public static void main(String[] args) {
        EventQueue.invokeLater(new Runnable() {
            public void run() {
                try {
                    UIManager.setLookAndFeel("com.sun.java.swing.plaf.nimbus.NimbusLookAndFeel");
                    MainProgram window = new MainProgram();
                        window.setVisible(true);
                        window.initialize();
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        });
    }
}

OverviewPage.java

import javax.swing.JPanel;

import net.miginfocom.swing.MigLayout;

public class OverviewPage extends JPanel {
    WorkNumberPanel workNumberPanel = new WorkNumberPanel();

    private static final long serialVersionUID = 1L;

    public OverviewPage() {
        setLayout(new MigLayout());
        add(workNumberPanel, "wrap, growx");
    }
}

TrackingPage.java

import javax.swing.JPanel;

import net.miginfocom.swing.MigLayout;

public class TrackingPage extends JPanel {
    private static final long serialVersionUID = 1L;

    WorkNumberPanel equipmentNumberPanel = new WorkNumberPanel();

    public TrackingPage(){
        setLayout(new MigLayout("", "grow, fill"));
        add(equipmentNumberPanel, "wrap, growx");
    }
}

WorkNumberPanel.java

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

import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;

import net.miginfocom.swing.MigLayout;

public class WorkNumberPanel extends JPanel {
    private static final long serialVersionUID = 1L;
    private static final Integer TEXTFIELD_LENGTH = 20;

    JPanel mainWorkNumberPanel = new JPanel();

    JLabel lblWorkNumber = new JLabel("E-Nr: ");
    JLabel lblN_Number = new JLabel("N-Nr.: ");
    JLabel lblSNumber = new JLabel("W-Nr.: ");

    public static JTextField txtWorkNumber = new JTextField(TEXTFIELD_LENGTH);
    JTextField txtNNumber = new JTextField(TEXTFIELD_LENGTH);
    JTextField txtSNumber = new JTextField(TEXTFIELD_LENGTH);

    JButton btnSearchEntry = new JButton("Search");

    public WorkNumberPanel() {
        createEquipmentNumberPanel();
        btnSearchEntry.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                MainProgram.contentCardsLayout.show(MainProgram.centerPanel, "trackingPage");
            }
        });
    }

    private void createEquipmentNumberPanel() {
        setLayout(new MigLayout());
        mainWorkNumberPanel.setLayout(new MigLayout("", "[][grow, fill][][grow, fill][][grow, fill][]"));
        mainWorkNumberPanel.add(lblWorkNumber);
        mainWorkNumberPanel.add(txtWorkNumber);
        mainWorkNumberPanel.add(lblN_Number);
        mainWorkNumberPanel.add(txtNNumber);
        mainWorkNumberPanel.add(lblSNumber);
        mainWorkNumberPanel.add(txtSNumber);
        mainWorkNumberPanel.add(btnSearchEntry);

        add(mainWorkNumberPanel, "push, span, growx");
    }
}

解决方法:

可能是因为当您使用此代码创建“页面”时

OverviewPage overviewPage = new OverviewPage();
TrackingPage trackingPage = new TrackingPage();

TrackingPage将是执行以下行的最后一个

mainWorkNumberPanel.add(txtWorkNumber);

在private void createEquipmentNumberPanel()中,因此Panel将“拥有”JTextField.只有在任何给定时间,UI组件只能在一个地方才有意义,否则事情会变得非常奇怪:)

你的陈述

Both pages contain the same workNumberPanel and I cant find a difference, that would explain the behaviour.

根本不是真的.您在执行以下行时在OverViewPage和TrackingPage中创建WorkNumberPanel的新实例

WorkNumberPanel equipmentNumberPanel = new WorkNumberPanel();

因此,我的建议是,您可以找到另一种实现所需内容的方法,而无需使用静态JTextField(或任何其他UI组件).

标签:miglayout,java,static,swing
来源: https://codeday.me/bug/20190824/1705367.html