通过JComboBox选择打开另一个JFrame可以通过以下步骤实现:
以下是一个示例代码:
import javax.swing.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
public class MainFrame extends JFrame {
private JComboBox<String> comboBox;
public MainFrame() {
// 设置主窗口的属性
setTitle("主窗口");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setSize(300, 200);
setLocationRelativeTo(null);
// 创建一个JComboBox对象,并添加选项
comboBox = new JComboBox<>();
comboBox.addItem("选项1");
comboBox.addItem("选项2");
comboBox.addItem("选项3");
// 添加选择事件的监听器
comboBox.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
// 获取选择的选项
String selectedOption = (String) comboBox.getSelectedItem();
// 根据选择的选项创建新的JFrame对象
if (selectedOption.equals("选项1")) {
JFrame newFrame = new JFrame("新窗口1");
newFrame.setSize(200, 150);
newFrame.setLocationRelativeTo(null);
newFrame.setVisible(true);
} else if (selectedOption.equals("选项2")) {
JFrame newFrame = new JFrame("新窗口2");
newFrame.setSize(200, 150);
newFrame.setLocationRelativeTo(null);
newFrame.setVisible(true);
} else if (selectedOption.equals("选项3")) {
JFrame newFrame = new JFrame("新窗口3");
newFrame.setSize(200, 150);
newFrame.setLocationRelativeTo(null);
newFrame.setVisible(true);
}
}
});
// 将JComboBox添加到主窗口中
getContentPane().add(comboBox);
}
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
@Override
public void run() {
MainFrame mainFrame = new MainFrame();
mainFrame.setVisible(true);
}
});
}
}
这个示例代码演示了如何通过JComboBox选择打开不同的JFrame窗口。根据选择的选项,创建一个新的JFrame对象,并设置其可见性为true,即可打开新窗口。
企业创新在线学堂
企业创新在线学堂
云+社区沙龙online [技术应变力]
云原生正发声
腾讯技术创作特训营第二季第4期
618音视频通信直播系列
“中小企业”在线学堂
2019腾讯云华北区互联网高峰论坛
技术创作101训练营
云+社区开发者大会(苏州站)
领取专属 10元无门槛券
手把手带您无忧上云