Jbutton 应该打开一个 JList,然后附加 JtextArea,但是当我按下它时什么也没有发生?

问题描述 投票:0回答:1

 public static JButton scene() {
        JButton scene = new JButton("Scene");
        ImageIcon sceneIcon = new ImageIcon("src/Images/scene.png");
        scene.setIcon(sceneIcon);

        scene.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                String[] entrances = {"EXT.", "INT."};
                JScrollPane header = new JScrollPane();
                JList<String> sceneTypeList = new JList<>(entrances);
                sceneTypeList.setSelectionMode(ListSelectionModel.SINGLE_INTERVAL_SELECTION);
                sceneTypeList.setLayoutOrientation(JList.VERTICAL);
                sceneTypeList.setVisibleRowCount(-1);
                sceneTypeList.setVisible(true);
                header.setSize(200, 200);
                header.add(sceneTypeList);
                Gui.textArea().add(header);


                if (sceneTypeList.getSelectedIndex() == 0) {
                    Gui.textDoc.append(entrances[0]);

                } else if (sceneTypeList.getSelectedIndex() == 1) {
                    Gui.textDoc.append(entrances[1]);
                }

            }
        });
public static final JTextArea textDoc = new JTextArea();
    protected static JFrame frame;

        private JFrame MyFrame() {
            frame = new JFrame();        //creates a frame
            frame.setTitle("Open Source ScriptWriter");  //sets title of frame
            frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);   //exit out of application
            frame.setResizable(true); //if set to false it'll make frame not resizable
            frame.setSize(940,820);     //sets dimensions and y-dimension of frame
            frame.setVisible(true);     //makes the frame visible
            frame.add(menuPanel(), BorderLayout.NORTH);
            frame.add(marginPanel(), BorderLayout.WEST);
            frame.add(marginPanel(), BorderLayout.EAST);
            frame.add(counterPanel(), BorderLayout.SOUTH);
            frame.add(textArea(), BorderLayout.CENTER);
            ImageIcon logo = new ImageIcon("src/Images/camera.png");
            frame.setIconImage(logo.getImage());
            return frame;
        }
public static JPanel textArea(){
            JPanel panel = new JPanel();
            panel.setLayout(new BorderLayout());
            panel.setBorder(BorderFactory.createEmptyBorder());
            panel.setBackground(Color.white);

            textDoc.setFont(new Font("Courier", Font.PLAIN, 12));
            textDoc.setMargin(new Insets(80,80,80,80));
            JScrollPane scrollPane = new JScrollPane(textDoc, JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED, JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED);
            panel.add(scrollPane);



            return panel;
        }

所以基本上,当我单击该场景按钮时,我希望在 textArea 面板中弹出一个 JList 并询问用户是否要创建 EXT。或INT。场景,然后粘贴到名为 textDoc 的文本区域中。但是,当我单击场景按钮时,它实际上没有执行任何操作。通常我可以在 textDoc 上书写,但是当我单击场景按钮时我也无法这样做。 JList 是否正在等待它未收到的操作?请指教。

java swing jbutton desktop-application jlist
1个回答
0
投票

你的程序结构已经关闭。当您调用

Gui.textArea()
时,您创建了一个 JPanel,然后在这里
.add(header);
您添加了一个带有组件的 JScrollPane,但您对
.textArea()
方法调用中创建的 JPanel 不执行任何操作。您不将其添加到任何顶级容器,例如 JFrame 或 JPanel 或其中包含的任何其他组件,因此最终的代码不执行任何操作。

更好的办法是以任何方式废弃所有这些代码并完全重写内容,以便在程序启动时创建组件,并且所有组件都已连接并准备就绪。如果您需要隐藏组件然后显示它,请使用 CardLayout。

© www.soinside.com 2019 - 2024. All rights reserved.