gpt4 book ai didi

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

转载 作者:塔克拉玛干 更新时间:2023-11-01 22:41:42 24 4
gpt4 key购买 nike

在编写程序时,我将一些字段设为静态,即 JTextField 用于 E-Number。但是现在这个字段的行为并不像预期的那样,在我的一些页面上它出现了,在其他一些页面上它消失了。由于我在处理大量静力学方面不是很有经验,因此可能有一个我不理解的概念。

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

它首先显示我的概览页面 - E-Number JTextField 丢失。

如果您单击搜索按钮,它会显示跟踪页面 - 显示 E-Number JTextField

两个页面都包含相同 workNumberPanel,我找不到可以解释行为的区别。

那么,为什么 E-Number JTextField 出现在概览页面上,而在跟踪页面上却不见了?感谢任何帮助/解释!

Static JTextField disappears

主程序.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() 中,因此该面板将“拥有”JTextField。一个 UI 组件在任何给定时间只能在一个地方才有意义,否则事情会变得很奇怪:)

你的陈述

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

根本不是真的。执行以下行时,您正在 OverViewPageTrackingPage 中创建 WorkNumberPanel 的新实例

WorkNumberPanel equipmentNumberPanel = new WorkNumberPanel();

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

关于java - JTextField 在静态时在某些页面上消失,而在其他页面上出现,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36329164/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com