使用 CardLayout 切换面板后不出现 JTextField
JTextField doesn't appear after switching panel using CardLayout
我有 4 个面板关联到一个使用卡片布局的内容面板。如果我尝试使用按钮在卡片之间切换,我可以看到 JLabel,但看不到 JTextFields。我假设它们在那里是因为 JLabel 之间有 space,所以我在切换到它们后尝试重新验证和重新绘制面板,但那没有用。我必须在其他地方重新粉刷它们吗?还是我遗漏了什么?
package data;
import javax.swing.\*;
import java.awt.\*;
public class Frame {
JFrame frame;
JPanel panelCont, panelStart, panelAns, panelGT, panelSyn;
JButton buttonAns, buttonGT, buttonSyn, buttonSendAns, buttonSendGT, buttonSendSyn;
JTextField jtfAnsCaseID, jtfAnsKeywords, jtfAnsAnswer;
JLabel jlAnsCaseID, jlAnsKeywords, jlAnsAnswer;
JTextField jtfGTID, jtfGTGenericTerm;
JLabel jlGTID, jlGTGenericTerm;
JTextField jtfSynSynonym, jtfSynID;
JLabel jlSynSynonym, jlSynID;
CardLayout cl;
Frame() {
frame = new JFrame("naoImporter");
panelCont = new JPanel();
cl = new CardLayout();
panelCont.setLayout(cl);
//Initialize components for start
panelStart = new JPanel();
buttonAns = new JButton("Answer");
buttonGT = new JButton("Generic terms");
buttonSyn = new JButton("Synonyms");
//Initialize components for answers
panelAns = new JPanel();
jlAnsCaseID = new JLabel("CaseID:");
jtfAnsCaseID = new JTextField();
jlAnsKeywords = new JLabel("Keywords:");
jtfAnsKeywords = new JTextField();
jlAnsAnswer = new JLabel("Answer:");
jtfAnsAnswer = new JTextField();
buttonSendAns = new JButton("Send Data!");
//Initialize components for generic terms
panelGT = new JPanel();
jlGTID = new JLabel("ID");
jtfGTID = new JTextField();
jlGTGenericTerm = new JLabel("Generic term:");
jtfGTGenericTerm = new JTextField();
buttonSendGT = new JButton("Send Data!");
//Initialize components for synonyms
panelSyn = new JPanel();
jlSynSynonym = new JLabel("Synonym:");
jtfSynSynonym = new JTextField();
jlSynID = new JLabel("ID:");
jtfSynID = new JTextField();
buttonSendSyn = new JButton("Send Data!");
//Configuring each component
panelStart.setLayout(new FlowLayout());
panelAns.setLayout(new FlowLayout());
panelGT.setLayout(new FlowLayout());
panelSyn.setLayout(new FlowLayout());
jtfAnsCaseID.setPreferredSize(new Dimension(30, jtfAnsCaseID.getHeight()));
jtfAnsKeywords.setPreferredSize(new Dimension(200, jtfAnsKeywords.getHeight()));
jtfAnsAnswer.setPreferredSize(new Dimension(200, jlAnsAnswer.getHeight()));
jtfGTID.setPreferredSize(new Dimension(30, jtfGTID.getHeight()));
jtfGTGenericTerm.setPreferredSize(new Dimension(100, jtfGTGenericTerm.getHeight()));
jtfSynSynonym.setPreferredSize(new Dimension(100, jtfSynSynonym.getHeight()));
jtfSynID.setPreferredSize(new Dimension(30, jtfSynID.getHeight()));
//Button listener logic
buttonAns.addActionListener(e -> cl.show(panelCont, "2"));
buttonGT.addActionListener(e -> cl.show(panelCont, "3"));
buttonSyn.addActionListener(e -> cl.show(panelCont, "4"));
buttonSendAns.addActionListener(e -> {
cl.show(panelCont, "1");
});
buttonSendGT.addActionListener(e -> {
cl.show(panelCont, "1");
});
buttonSendSyn.addActionListener(e -> {
cl.show(panelCont, "1");
});
//Adding component ot each panel
panelStart.add(buttonAns);
panelStart.add(buttonGT);
panelStart.add(buttonSyn);
panelAns.add(jlAnsCaseID);
panelAns.add(jtfAnsCaseID);
panelAns.add(jlAnsKeywords);
panelAns.add(jtfAnsKeywords);
panelAns.add(jlAnsAnswer);
panelAns.add(jtfAnsAnswer);
panelAns.add(buttonSendAns);
panelGT.add(jlGTID);
panelGT.add(jtfGTID);
panelGT.add(jlGTGenericTerm);
panelGT.add(jtfGTGenericTerm);
panelGT.add(buttonSendGT);
panelSyn.add(jlSynSynonym);
panelSyn.add(jtfSynSynonym);
panelSyn.add(jlSynID);
panelSyn.add(jtfSynID);
panelSyn.add(buttonSendSyn);
panelCont.add(panelStart, "1");
panelCont.add(panelAns, "2");
panelCont.add(panelGT, "3");
panelCont.add(panelSyn, "4");
cl.show(panelCont, "1");
frame.add(panelCont);
frame.pack();
frame.setLocationRelativeTo(null);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
}
}
Picture of start panel
Picture of answer panel after switching to it
i can see the JLabels but no JTextFields
tfAnsCaseID.setPreferredSize(new Dimension(30, jtfAnsCaseID.getHeight()));
在框架打包或可见之前,Swing 组件没有大小。
默认高度为 0,因此您为组件指定的首选高度为 0。FlowLayout 将遵循首选大小,因此无需绘制任何内容。
所有 Swing 组件都负责确定自己的首选大小,因此不要尝试使用一些随机值自行管理首选大小。
改为使用:
jtfAnsCaseID = new JTextField(5);
构造函数中指定的数字将允许文本字段计算其首选大小,以根据使用的字体和边框显示 5 个“W”字符。
我有 4 个面板关联到一个使用卡片布局的内容面板。如果我尝试使用按钮在卡片之间切换,我可以看到 JLabel,但看不到 JTextFields。我假设它们在那里是因为 JLabel 之间有 space,所以我在切换到它们后尝试重新验证和重新绘制面板,但那没有用。我必须在其他地方重新粉刷它们吗?还是我遗漏了什么?
package data;
import javax.swing.\*;
import java.awt.\*;
public class Frame {
JFrame frame;
JPanel panelCont, panelStart, panelAns, panelGT, panelSyn;
JButton buttonAns, buttonGT, buttonSyn, buttonSendAns, buttonSendGT, buttonSendSyn;
JTextField jtfAnsCaseID, jtfAnsKeywords, jtfAnsAnswer;
JLabel jlAnsCaseID, jlAnsKeywords, jlAnsAnswer;
JTextField jtfGTID, jtfGTGenericTerm;
JLabel jlGTID, jlGTGenericTerm;
JTextField jtfSynSynonym, jtfSynID;
JLabel jlSynSynonym, jlSynID;
CardLayout cl;
Frame() {
frame = new JFrame("naoImporter");
panelCont = new JPanel();
cl = new CardLayout();
panelCont.setLayout(cl);
//Initialize components for start
panelStart = new JPanel();
buttonAns = new JButton("Answer");
buttonGT = new JButton("Generic terms");
buttonSyn = new JButton("Synonyms");
//Initialize components for answers
panelAns = new JPanel();
jlAnsCaseID = new JLabel("CaseID:");
jtfAnsCaseID = new JTextField();
jlAnsKeywords = new JLabel("Keywords:");
jtfAnsKeywords = new JTextField();
jlAnsAnswer = new JLabel("Answer:");
jtfAnsAnswer = new JTextField();
buttonSendAns = new JButton("Send Data!");
//Initialize components for generic terms
panelGT = new JPanel();
jlGTID = new JLabel("ID");
jtfGTID = new JTextField();
jlGTGenericTerm = new JLabel("Generic term:");
jtfGTGenericTerm = new JTextField();
buttonSendGT = new JButton("Send Data!");
//Initialize components for synonyms
panelSyn = new JPanel();
jlSynSynonym = new JLabel("Synonym:");
jtfSynSynonym = new JTextField();
jlSynID = new JLabel("ID:");
jtfSynID = new JTextField();
buttonSendSyn = new JButton("Send Data!");
//Configuring each component
panelStart.setLayout(new FlowLayout());
panelAns.setLayout(new FlowLayout());
panelGT.setLayout(new FlowLayout());
panelSyn.setLayout(new FlowLayout());
jtfAnsCaseID.setPreferredSize(new Dimension(30, jtfAnsCaseID.getHeight()));
jtfAnsKeywords.setPreferredSize(new Dimension(200, jtfAnsKeywords.getHeight()));
jtfAnsAnswer.setPreferredSize(new Dimension(200, jlAnsAnswer.getHeight()));
jtfGTID.setPreferredSize(new Dimension(30, jtfGTID.getHeight()));
jtfGTGenericTerm.setPreferredSize(new Dimension(100, jtfGTGenericTerm.getHeight()));
jtfSynSynonym.setPreferredSize(new Dimension(100, jtfSynSynonym.getHeight()));
jtfSynID.setPreferredSize(new Dimension(30, jtfSynID.getHeight()));
//Button listener logic
buttonAns.addActionListener(e -> cl.show(panelCont, "2"));
buttonGT.addActionListener(e -> cl.show(panelCont, "3"));
buttonSyn.addActionListener(e -> cl.show(panelCont, "4"));
buttonSendAns.addActionListener(e -> {
cl.show(panelCont, "1");
});
buttonSendGT.addActionListener(e -> {
cl.show(panelCont, "1");
});
buttonSendSyn.addActionListener(e -> {
cl.show(panelCont, "1");
});
//Adding component ot each panel
panelStart.add(buttonAns);
panelStart.add(buttonGT);
panelStart.add(buttonSyn);
panelAns.add(jlAnsCaseID);
panelAns.add(jtfAnsCaseID);
panelAns.add(jlAnsKeywords);
panelAns.add(jtfAnsKeywords);
panelAns.add(jlAnsAnswer);
panelAns.add(jtfAnsAnswer);
panelAns.add(buttonSendAns);
panelGT.add(jlGTID);
panelGT.add(jtfGTID);
panelGT.add(jlGTGenericTerm);
panelGT.add(jtfGTGenericTerm);
panelGT.add(buttonSendGT);
panelSyn.add(jlSynSynonym);
panelSyn.add(jtfSynSynonym);
panelSyn.add(jlSynID);
panelSyn.add(jtfSynID);
panelSyn.add(buttonSendSyn);
panelCont.add(panelStart, "1");
panelCont.add(panelAns, "2");
panelCont.add(panelGT, "3");
panelCont.add(panelSyn, "4");
cl.show(panelCont, "1");
frame.add(panelCont);
frame.pack();
frame.setLocationRelativeTo(null);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
}
}
Picture of start panel
Picture of answer panel after switching to it
i can see the JLabels but no JTextFields
tfAnsCaseID.setPreferredSize(new Dimension(30, jtfAnsCaseID.getHeight()));
在框架打包或可见之前,Swing 组件没有大小。
默认高度为 0,因此您为组件指定的首选高度为 0。FlowLayout 将遵循首选大小,因此无需绘制任何内容。
所有 Swing 组件都负责确定自己的首选大小,因此不要尝试使用一些随机值自行管理首选大小。
改为使用:
jtfAnsCaseID = new JTextField(5);
构造函数中指定的数字将允许文本字段计算其首选大小,以根据使用的字体和边框显示 5 个“W”字符。