我希望组合框中的每个选项都在我的应用程序上显示不同的内容。窗口:不同的按钮、表格、文本字段等。
我认为最好的方法是做一个“开关”,但我真的不知道在开关的测试区域放什么。
package tema3;
import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
public class Interface {
public static void main(String[] args) {
new Interface();
}
public Interface() {
JFrame guiFrame = new JFrame();
guiFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
guiFrame.setTitle("Tema3");
guiFrame.setSize(1200, 600);
guiFrame.setLocationRelativeTo(null); // seteaza centrat
String[] tableOptions = { "OPDept", "Order", "OrderItems", "Product",
"Warehouse", "Customer" };
// pentru combo box
final JPanel comboPanel = new JPanel();
JLabel comboLbl = new JLabel("Tabele:");
JComboBox tables = new JComboBox(tableOptions);
comboPanel.add(comboLbl);
comboPanel.add(tables);
tables.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent event) {
comboPanel.setVisible(comboPanel.isVisible());
switch (???what to insert here???) {
case "Order": {
System.out.println("11111");
break;
}
case "Product": {
System.out.println("2222");
break;
}
default: {
System.out.print("sd");
break;
}
}
}
});
guiFrame.add(comboPanel, BorderLayout.NORTH);
guiFrame.add(tables, BorderLayout.SOUTH);
guiFrame.setVisible(true);
}
}
谢谢, 德拉戈斯