(Card Layout)为JComboBox设置CardPanels的卡片名称
(Card Layout) Setting Card Name of CardPanels for JComboBox
伙计们。我的项目设计结合了硬编码设计和使用 Netbeans 的 GUI Builder 进行的设计。由于我们落后于计划,我决定使用 GUI Builder 进行 RAD 和快速设计。
所以我的问题是在为面板使用卡片布局时如何为卡片面板分配名称。我知道如果不使用 GUI Builder,您可以简单地写 cardContainerPanel.add(card1Panel,"card1")
。 card1
作为 card1Panel 的 String
标识符。但我希望能够做的是传递 String variable
作为卡名。
喜欢,
String card1 = "card1";
然后
cardContainerPanel.add(card1Panel, card1)
1.) 我如何在 Netbeans GUI Builder 上使用面板的 Properties 选项执行此操作,因为它仅将您在 Card Name 上键入的内容作为字符串
我需要在 JComboBox
的 itemChange
活动期间切换到不同的卡片。
这是我到目前为止所写的内容。
private void settingsComboBoxItemStateChanged(java.awt.event.ItemEvent evt) {
CardLayout cl = (CardLayout)(cardContainerPanel.getLayout());
cl.show(cardContainerPanel, (String)evt.getItem());
}
private void loadSettingsComboxBoxModel(){
String HOME = "Home";
String ADMIN = "Administration";
String ADMIN_CURC = "Administration > Curriculum";
String ADMIN_DISCOUNTS = "Administration > Discounts";
String ADMIN_SECTIONS = "Administration > Sections";
String ADMIN_USERS = "Administration > Users";
String ADMIN_SCHED = "Administration > Schedule";
String ADMIN_YRLEVEL = "Administration > Year Level";
String PAYMENT = "Payment";
String REGISTRATION = "Registration";
String STUDENTS = "Students";
String FACULTY = "Faculty";
String REPORTS = "Reports";
String SECTIONS = "Sections";
String settingsComboBoxItems[] =
{HOME,ADMIN,ADMIN_CURC,ADMIN_DISCOUNTS,
ADMIN_SECTIONS,ADMIN_USERS,ADMIN_SCHED,
ADMIN_YRLEVEL,PAYMENT,REGISTRATION,STUDENTS,FACULTY,REPORTS,SECTIONS};
DefaultComboBoxModel dcm = new DefaultComboBoxModel(settingsComboBoxItems);
settingsComboBox.setModel(dcm);
}
如有任何帮助,我将不胜感激。
谢谢。
在 IDE 中硬编码卡名是无关紧要的。
相关的是当你想交换卡片时使用的卡片名称。在这种情况下,您没有理由不能使用变量来指定要交换到的卡名称。
实际上,您似乎是在使用组合框 select 要显示的卡片。所以你需要做的就是确保卡片名称与组合框中显示的值相同。
然后在您的 ItemListener 中,您只需使用以下内容来交换卡片:
cardLayout.show(cards, (String)evt.getItem());
有关工作示例,请参阅 How to Use CardLayout 上的 Swing 教程部分。
我能够使用 addLayoutComponent()
方法解决它,该方法将面板添加到 Card Layout 对象并接受字符串变量作为其第二个参数。
String HOME = "Home";
String ADMIN = "Administration";
String ADMIN_CURC = "Administration > Curriculum";
String ADMIN_DISCOUNTS = "Administration > Discounts";
String ADMIN_SECTIONS = "Administration > Sections";
String ADMIN_USERS = "Administration > Users";
String ADMIN_SCHED = "Administration > Schedule";
String ADMIN_YRLEVEL = "Administration > Year Level";
String PAYMENT = "Payment";
String REGISTRATION = "Registration";
String STUDENTS = "Students";
String FACULTY = "Faculty";
String REPORTS = "Reports";
String SECTIONS = "Sections";
String settingsComboBoxItems[] =
{HOME,ADMIN,ADMIN_CURC,ADMIN_DISCOUNTS,
ADMIN_SECTIONS,ADMIN_USERS,ADMIN_SCHED,
ADMIN_YRLEVEL,PAYMENT,REGISTRATION,STUDENTS,FACULTY,REPORTS,SECTIONS};
DefaultComboBoxModel dcm = new DefaultComboBoxModel(settingsComboBoxItems);
//constructor
public UserManagementGUI() {
initComponents();
loadUsersList();
settingsComboBox.setModel(dcm);
}
private void settingsComboBoxItemStateChanged(java.awt.event.ItemEvent evt) {
CardLayout cl = (CardLayout)(cardContainerPanel.getLayout());
cl.addLayoutComponent(homePermissionsPanel, HOME);
cl.addLayoutComponent(administrationPermissionsPanel, ADMIN);
cl.show(cardContainerPanel, (String)evt.getItem());
}
伙计们。我的项目设计结合了硬编码设计和使用 Netbeans 的 GUI Builder 进行的设计。由于我们落后于计划,我决定使用 GUI Builder 进行 RAD 和快速设计。
所以我的问题是在为面板使用卡片布局时如何为卡片面板分配名称。我知道如果不使用 GUI Builder,您可以简单地写 cardContainerPanel.add(card1Panel,"card1")
。 card1
作为 card1Panel 的 String
标识符。但我希望能够做的是传递 String variable
作为卡名。
喜欢,
String card1 = "card1";
然后
cardContainerPanel.add(card1Panel, card1)
1.) 我如何在 Netbeans GUI Builder 上使用面板的 Properties 选项执行此操作,因为它仅将您在 Card Name 上键入的内容作为字符串
我需要在 JComboBox
的 itemChange
活动期间切换到不同的卡片。
这是我到目前为止所写的内容。
private void settingsComboBoxItemStateChanged(java.awt.event.ItemEvent evt) {
CardLayout cl = (CardLayout)(cardContainerPanel.getLayout());
cl.show(cardContainerPanel, (String)evt.getItem());
}
private void loadSettingsComboxBoxModel(){
String HOME = "Home";
String ADMIN = "Administration";
String ADMIN_CURC = "Administration > Curriculum";
String ADMIN_DISCOUNTS = "Administration > Discounts";
String ADMIN_SECTIONS = "Administration > Sections";
String ADMIN_USERS = "Administration > Users";
String ADMIN_SCHED = "Administration > Schedule";
String ADMIN_YRLEVEL = "Administration > Year Level";
String PAYMENT = "Payment";
String REGISTRATION = "Registration";
String STUDENTS = "Students";
String FACULTY = "Faculty";
String REPORTS = "Reports";
String SECTIONS = "Sections";
String settingsComboBoxItems[] =
{HOME,ADMIN,ADMIN_CURC,ADMIN_DISCOUNTS,
ADMIN_SECTIONS,ADMIN_USERS,ADMIN_SCHED,
ADMIN_YRLEVEL,PAYMENT,REGISTRATION,STUDENTS,FACULTY,REPORTS,SECTIONS};
DefaultComboBoxModel dcm = new DefaultComboBoxModel(settingsComboBoxItems);
settingsComboBox.setModel(dcm);
}
如有任何帮助,我将不胜感激。
谢谢。
在 IDE 中硬编码卡名是无关紧要的。
相关的是当你想交换卡片时使用的卡片名称。在这种情况下,您没有理由不能使用变量来指定要交换到的卡名称。
实际上,您似乎是在使用组合框 select 要显示的卡片。所以你需要做的就是确保卡片名称与组合框中显示的值相同。
然后在您的 ItemListener 中,您只需使用以下内容来交换卡片:
cardLayout.show(cards, (String)evt.getItem());
有关工作示例,请参阅 How to Use CardLayout 上的 Swing 教程部分。
我能够使用 addLayoutComponent()
方法解决它,该方法将面板添加到 Card Layout 对象并接受字符串变量作为其第二个参数。
String HOME = "Home";
String ADMIN = "Administration";
String ADMIN_CURC = "Administration > Curriculum";
String ADMIN_DISCOUNTS = "Administration > Discounts";
String ADMIN_SECTIONS = "Administration > Sections";
String ADMIN_USERS = "Administration > Users";
String ADMIN_SCHED = "Administration > Schedule";
String ADMIN_YRLEVEL = "Administration > Year Level";
String PAYMENT = "Payment";
String REGISTRATION = "Registration";
String STUDENTS = "Students";
String FACULTY = "Faculty";
String REPORTS = "Reports";
String SECTIONS = "Sections";
String settingsComboBoxItems[] =
{HOME,ADMIN,ADMIN_CURC,ADMIN_DISCOUNTS,
ADMIN_SECTIONS,ADMIN_USERS,ADMIN_SCHED,
ADMIN_YRLEVEL,PAYMENT,REGISTRATION,STUDENTS,FACULTY,REPORTS,SECTIONS};
DefaultComboBoxModel dcm = new DefaultComboBoxModel(settingsComboBoxItems);
//constructor
public UserManagementGUI() {
initComponents();
loadUsersList();
settingsComboBox.setModel(dcm);
}
private void settingsComboBoxItemStateChanged(java.awt.event.ItemEvent evt) {
CardLayout cl = (CardLayout)(cardContainerPanel.getLayout());
cl.addLayoutComponent(homePermissionsPanel, HOME);
cl.addLayoutComponent(administrationPermissionsPanel, ADMIN);
cl.show(cardContainerPanel, (String)evt.getItem());
}