java中jscrollpane的问题

java中jscrollpane的问题,java,swing,jscrollpane,Java,Swing,Jscrollpane,我在我的jscrollpane中添加了一个jtextarea,但我不知道为什么它不工作! 你能帮我吗 问题是我的文本向下滚动,但无法在GUI中工作 我上传了所有代码,因为运行它很容易,只需创建一个主文件并设置大小即可。您可以跟踪这些Jscrollpane的问题:jScrollPane1、jScrollPane16、jScrollPane4 import java.awt.*; import java.awt.event.*; import javax.swing.*; import com.

我在我的jscrollpane中添加了一个jtextarea,但我不知道为什么它不工作! 你能帮我吗

问题是我的文本向下滚动,但无法在GUI中工作


我上传了所有代码,因为运行它很容易,只需创建一个主文件并设置大小即可。您可以跟踪这些Jscrollpane的问题:jScrollPane1、jScrollPane16、jScrollPane4

import java.awt.*;
import java.awt.event.*;

import javax.swing.*;

import com.borland.jbcl.layout.*;

public class Frame1 extends JFrame {
        JPanel contentPane;
        BorderLayout borderLayout1 = new BorderLayout();
        JMenuBar jMenuBar1 = new JMenuBar();
        JMenu jMenuFile = new JMenu();
        JMenuItem jMenuFileExit = new JMenuItem();
        JMenu jMenuHelp = new JMenu();
        JMenuItem jMenuHelpAbout = new JMenuItem();
        JToolBar jToolBar = new JToolBar();
        JButton jButton1 = new JButton();
        JButton jButton2 = new JButton();
        JButton jButton3 = new JButton();
        ImageIcon image1 = new ImageIcon(masterproject.Frame1.class.getResource(
                "openFile.png"));
        ImageIcon image2 = new ImageIcon(masterproject.Frame1.class.getResource(
                "closeFile.png"));
        ImageIcon image3 = new ImageIcon(masterproject.Frame1.class.getResource(
                "help.png"));
        JLabel statusBar = new JLabel();
        JSplitPane jSplitPane1 = new JSplitPane();
        JPanel jPanel1 = new JPanel();
        JPanel jPanel2 = new JPanel();
        DynamicTree jtProjectList = new DynamicTree();
        BorderLayout borderLayout2 = new BorderLayout();
        JToolBar jToolBar1 = new JToolBar();
        JToolBar jToolBar2 = new JToolBar();
        JButton jbAdd = new JButton();
        JButton jbRemove = new JButton();
        int newNodeSuffix = 0;
        JToolBar jToolBar3 = new JToolBar();
        BorderLayout borderLayout3 = new BorderLayout();
        JPanel jpCore = new JPanel();
        JPanel jpExclusion = new JPanel();
        JLabel jLabel1 = new JLabel();
        JTextArea jTextArea1 = new JTextArea();
        BorderLayout borderLayout4 = new BorderLayout();
        JTable jTable1 = new JTable();
        JPanel jpScope = new JPanel();
        JLabel jLabel2 = new JLabel();
        JTextArea jTextArea2 = new JTextArea();
        JTable jTable2 = new JTable();
        BorderLayout borderLayout5 = new BorderLayout();
        VerticalFlowLayout verticalFlowLayout1 = new VerticalFlowLayout();
        JPanel jpDeliverable = new JPanel();
        JTable jTable3 = new JTable();
        JLabel jLabel3 = new JLabel();
        JTextArea jTextArea3 = new JTextArea();
        BorderLayout borderLayout6 = new BorderLayout();
        JPanel jpAcceptance = new JPanel();
        JPanel jpConstraints = new JPanel();
        JPanel jpAssumption = new JPanel();
        JPanel jpRisk = new JPanel();
        BorderLayout borderLayout7 = new BorderLayout();
        BorderLayout borderLayout8 = new BorderLayout();
        BorderLayout borderLayout9 = new BorderLayout();
        JTable jTable4 = new JTable();
        JTable jTable5 = new JTable();
        JTable jTable6 = new JTable();
        JLabel jLabel4 = new JLabel();
        JLabel jLabel5 = new JLabel();
        JLabel jLabel6 = new JLabel();
        JTextArea jTextArea4 = new JTextArea();
        JTextArea jTextArea5 = new JTextArea();
        JTextArea jTextArea6 = new JTextArea();
        JLabel jLabel7 = new JLabel();
        JTextArea jTextArea7 = new JTextArea();
        JTable jTable7 = new JTable();
        BorderLayout borderLayout10 = new BorderLayout();
        JScrollPane jScrollPane1 = new JScrollPane();
        JScrollPane jScrollPane2 = new JScrollPane();
        JScrollPane jScrollPane3 = new JScrollPane();
        JScrollPane jScrollPane4 = new JScrollPane();
        JScrollPane jScrollPane5 = new JScrollPane();
        JScrollPane jScrollPane6 = new JScrollPane();
        JScrollPane jScrollPane7 = new JScrollPane();
        JScrollPane jScrollPane8 = new JScrollPane();
        JScrollPane jScrollPane9 = new JScrollPane();
        JScrollPane jScrollPane10 = new JScrollPane();
        JScrollPane jScrollPane11 = new JScrollPane();
        JScrollPane jScrollPane12 = new JScrollPane();
        JScrollPane jScrollPane13 = new JScrollPane();
        JScrollPane jScrollPane14 = new JScrollPane();
        JScrollPane jScrollPane15 = new JScrollPane();
        JScrollPane jScrollPane16 = new JScrollPane();
        public Frame1() {
            try {
                setDefaultCloseOperation(EXIT_ON_CLOSE);
                jbInit();
            } catch (Exception exception) {
                exception.printStackTrace();
            }
        }

        /**
         * Component initialization.
         *
         * @throws java.lang.Exception
         */
        private void jbInit() throws Exception {
            contentPane = (JPanel) getContentPane();
            contentPane.setLayout(borderLayout1);
            this.setFont(new java.awt.Font("Tahoma", Font.BOLD, 14));
            setSize(new Dimension(753, 483));
            setTitle("Master Project");
            statusBar.setText(" ");
            jMenuFile.setText("File");
            jMenuFileExit.setText("Exit");
            jMenuFileExit.addActionListener(new Frame1_jMenuFileExit_ActionAdapter(this));
            jMenuHelp.setText("Help");
            jMenuHelpAbout.setText("About");
            jMenuHelpAbout.addActionListener(new
                                             Frame1_jMenuHelpAbout_ActionAdapter(this));
            jPanel1.setLayout(borderLayout2);
            jbAdd.setMaximumSize(new Dimension(51, 25));
            jbAdd.setPreferredSize(new Dimension(51, 25));
            jbAdd.setText("Add");
            jbAdd.addActionListener(new Frame1_jbAdd_actionAdapter(this));
            jbRemove.setText("Remove");
            jbRemove.addActionListener(new Frame1_jbRemove_actionAdapter(this));
            contentPane.setMinimumSize(new Dimension(400, 600));
            contentPane.setPreferredSize(new Dimension(400, 600));
            jSplitPane1.setMinimumSize(new Dimension(200, 55));
            jSplitPane1.setPreferredSize(new Dimension(200, 355));
            jSplitPane1.setLastDividerLocation(300);
            jtProjectList.setPreferredSize(new Dimension(200, 324));
            jPanel2.setLayout(borderLayout3);
            jLabel1.setText("Exclusion :");
            jTextArea1.setPreferredSize(new Dimension(280, 60));
            jTextArea1.setText("jTextArea1");
            jTextArea1.setLineWrap(true);
            jpExclusion.setLayout(borderLayout4);
            jLabel2.setText("Scope :");
            jTextArea2.setPreferredSize(new Dimension(280, 60));
            jTextArea2.setText("jTextArea1");
            jTextArea2.setLineWrap(true);
            jpScope.setLayout(borderLayout5);
            jpCore.setLayout(verticalFlowLayout1);
            jLabel3.setText("Deliverable :");
            jTextArea3.setPreferredSize(new Dimension(280, 60));
            jTextArea3.setText("jTextArea3");
            jTextArea3.setLineWrap(true);
            jpDeliverable.setLayout(borderLayout6);
            jpAssumption.setLayout(borderLayout7);
            jpConstraints.setLayout(borderLayout8);
            jpAcceptance.setLayout(borderLayout9);
            jLabel4.setText("Assumption :");
            jLabel5.setText("Constraints :");
            jLabel6.setText("Acceptance :");
            jTextArea4.setMinimumSize(new Dimension(80, 18));
            jTextArea4.setPreferredSize(new Dimension(280, 60));
            jTextArea4.setText("jTextArea4");
            jTextArea4.setLineWrap(true);
            jTextArea5.setMinimumSize(new Dimension(80, 18));
            jTextArea5.setPreferredSize(new Dimension(280, 60));
            jTextArea5.setText("jTextArea5");
            jTextArea5.setLineWrap(true);
            jTextArea6.setMinimumSize(new Dimension(80, 18));
            jTextArea6.setPreferredSize(new Dimension(280, 60));
            jTextArea6.setText("jTextArea6");
            jTextArea6.setLineWrap(true);

            jpScope.setToolTipText("");
            jLabel7.setText("Risk :");
            jTextArea7.setPreferredSize(new Dimension(280, 60));
            jTextArea7.setText("jTextArea7");
            jTextArea7.setLineWrap(true);
            jpRisk.setLayout(borderLayout10);
            jpRisk.setMinimumSize(new Dimension(280, 60));
            jScrollPane6.setPreferredSize(new Dimension(284, 44));
            jScrollPane16.setPreferredSize(new Dimension(284, 44));
            jScrollPane5.setPreferredSize(new Dimension(284, 44));
            jScrollPane7.setPreferredSize(new Dimension(284, 44));
            jScrollPane14.setPreferredSize(new Dimension(284, 44));
            jScrollPane12.setPreferredSize(new Dimension(284, 44));
            jScrollPane10.setPreferredSize(new Dimension(284, 44));
            jScrollPane3.setVerticalScrollBarPolicy(JScrollPane.
                                                    VERTICAL_SCROLLBAR_ALWAYS);
            jScrollPane9.setVerticalScrollBarPolicy(JScrollPane.
                                                    VERTICAL_SCROLLBAR_ALWAYS);
            jScrollPane11.setVerticalScrollBarPolicy(JScrollPane.
                                                     VERTICAL_SCROLLBAR_ALWAYS);
            jScrollPane13.setVerticalScrollBarPolicy(JScrollPane.
                                                     VERTICAL_SCROLLBAR_ALWAYS);
            jScrollPane8.setVerticalScrollBarPolicy(JScrollPane.
                                                    VERTICAL_SCROLLBAR_ALWAYS);
            jScrollPane4.setVerticalScrollBarPolicy(JScrollPane.
                                                    VERTICAL_SCROLLBAR_ALWAYS);
            jScrollPane15.setVerticalScrollBarPolicy(JScrollPane.
                                                     VERTICAL_SCROLLBAR_ALWAYS);
            jScrollPane1.setVerticalScrollBarPolicy(JScrollPane.
                                                    VERTICAL_SCROLLBAR_ALWAYS);
            jMenuBar1.add(jMenuFile);
            jMenuFile.add(jMenuFileExit);
            jMenuBar1.add(jMenuHelp);
            jMenuHelp.add(jMenuHelpAbout);
            setJMenuBar(jMenuBar1);
            jButton1.setIcon(image1);
            jButton1.setToolTipText("Open File");
            jButton2.setIcon(image2);
            jButton2.setToolTipText("Close File");
            jButton3.setIcon(image3);
            jButton3.setToolTipText("Help");
            jToolBar.add(jButton1);
            jToolBar.add(jButton2);
            jToolBar.add(jButton3);
            contentPane.add(statusBar, BorderLayout.SOUTH);
            contentPane.add(jSplitPane1, java.awt.BorderLayout.CENTER);
            jSplitPane1.add(jPanel1, JSplitPane.LEFT);
            jSplitPane1.add(jPanel2, JSplitPane.RIGHT);
            jToolBar1.add(jtProjectList);
            jPanel1.add(jToolBar1, java.awt.BorderLayout.CENTER);
            jPanel1.add(jToolBar2, java.awt.BorderLayout.NORTH);
            jToolBar2.add(jbAdd);
            jToolBar2.add(jbRemove);
            jPanel2.add(jToolBar3, java.awt.BorderLayout.CENTER);
            jToolBar3.add(jScrollPane3);
            jScrollPane3.getViewport().add(jpCore);
            jpScope.add(jScrollPane1, java.awt.BorderLayout.CENTER);

            jScrollPane1.getViewport().add(jTextArea2);

            jScrollPane16.getViewport().add(jTable1);
            jScrollPane14.getViewport().add(jTable5);
            jScrollPane12.getViewport().add(jTable6);
            jScrollPane10.getViewport().add(jTable7);
            jScrollPane8.getViewport().add(jTextArea4);
            jScrollPane6.getViewport().add(jTable2);
            jScrollPane5.getViewport().add(jTable3);
            jScrollPane4.getViewport().add(jTextArea3);

            contentPane.add(jToolBar, java.awt.BorderLayout.NORTH);
            jpDeliverable.add(jScrollPane4, java.awt.BorderLayout.CENTER);
            jpDeliverable.add(jScrollPane5, java.awt.BorderLayout.SOUTH);
            jpDeliverable.add(jLabel3, java.awt.BorderLayout.NORTH);
            jpScope.add(jScrollPane6, java.awt.BorderLayout.SOUTH);
            jpScope.add(jLabel2, java.awt.BorderLayout.NORTH);
            jScrollPane7.getViewport().add(jTable4);
            jpAcceptance.add(jLabel6, java.awt.BorderLayout.NORTH);
            jScrollPane9.getViewport().add(jTextArea7);
            jpRisk.add(jScrollPane10, java.awt.BorderLayout.SOUTH);
            jpRisk.add(jScrollPane9, java.awt.BorderLayout.CENTER);
            jpRisk.add(jLabel7, java.awt.BorderLayout.NORTH);
            jScrollPane11.getViewport().add(jTextArea6);
            jpAssumption.add(jLabel4, java.awt.BorderLayout.NORTH);
            jpAssumption.add(jScrollPane12, java.awt.BorderLayout.SOUTH);
            jpAssumption.add(jScrollPane11, java.awt.BorderLayout.CENTER);
            jScrollPane13.getViewport().add(jTextArea5);
            jpConstraints.add(jLabel5, java.awt.BorderLayout.NORTH);
            jpConstraints.add(jScrollPane14, java.awt.BorderLayout.SOUTH);
            jpConstraints.add(jScrollPane13, java.awt.BorderLayout.CENTER);
            jScrollPane15.getViewport().add(jTextArea1);
            jpExclusion.add(jLabel1, java.awt.BorderLayout.NORTH);
            jpExclusion.add(jScrollPane16, java.awt.BorderLayout.SOUTH);
            jpExclusion.add(jScrollPane15, java.awt.BorderLayout.CENTER);

            jpAcceptance.add(jScrollPane8, java.awt.BorderLayout.CENTER);
            jpAcceptance.add(jScrollPane7, java.awt.BorderLayout.SOUTH);


            jpCore.add(jpScope, 0);
            jpCore.add(jpExclusion, 1);
            jpCore.add(jpDeliverable, 2);
            jpCore.add(jpAcceptance, 3);
            jpCore.add(jpConstraints, 4);
            jpCore.add(jpAssumption, 5);
            jpCore.add(jpRisk, 6);
            jSplitPane1.setDividerLocation(230);
        }

        /**
         * File | Exit action performed.
         *
         * @param actionEvent ActionEvent
         */
        void jMenuFileExit_actionPerformed(ActionEvent actionEvent) {
            System.exit(0);
        }

        /**
         * Help | About action performed.
         *
         * @param actionEvent ActionEvent
         */
        void jMenuHelpAbout_actionPerformed(ActionEvent actionEvent) {
            Frame1_AboutBox dlg = new Frame1_AboutBox(this);
            Dimension dlgSize = dlg.getPreferredSize();
            Dimension frmSize = getSize();
            Point loc = getLocation();
            dlg.setLocation((frmSize.width - dlgSize.width) / 2 + loc.x,
                            (frmSize.height - dlgSize.height) / 2 + loc.y);
            dlg.setModal(true);
            dlg.pack();
            dlg.setVisible(true);
        }

        public void jbRemove_actionPerformed(ActionEvent actionEvent) {
            jtProjectList.removeCurrentNode();
        }

        public void jbAdd_actionPerformed(ActionEvent actionEvent) {
            String response = "";
            while(response.trim().equals("")){
                response = JOptionPane.showInputDialog(this,
                        "Please enter name of node?",
                        "Enter your name",
                        JOptionPane.QUESTION_MESSAGE);
                //System.out.println(response);
                if( response != null && response.trim().equals(""))
                    JOptionPane.showMessageDialog(this,"Please enter valid name",
                                                  "Error",JOptionPane.ERROR_MESSAGE);
            }
            jtProjectList.addObject(response);

        }
    }


    class Frame1_jbAdd_actionAdapter implements ActionListener {
        private Frame1 adaptee;
        Frame1_jbAdd_actionAdapter(Frame1 adaptee) {
            this.adaptee = adaptee;
        }

        public void actionPerformed(ActionEvent actionEvent) {
            adaptee.jbAdd_actionPerformed(actionEvent);
        }
    }


    class Frame1_jbRemove_actionAdapter implements ActionListener {
        private Frame1 adaptee;
        Frame1_jbRemove_actionAdapter(Frame1 adaptee) {
            this.adaptee = adaptee;
        }

        public void actionPerformed(ActionEvent actionEvent) {
            adaptee.jbRemove_actionPerformed(actionEvent);
        }
    }


    class Frame1_jMenuFileExit_ActionAdapter implements ActionListener {
        Frame1 adaptee;

        Frame1_jMenuFileExit_ActionAdapter(Frame1 adaptee) {
            this.adaptee = adaptee;
        }

        public void actionPerformed(ActionEvent actionEvent) {
            adaptee.jMenuFileExit_actionPerformed(actionEvent);
        }
    }


    class Frame1_jMenuHelpAbout_ActionAdapter implements ActionListener {
        Frame1 adaptee;

        Frame1_jMenuHelpAbout_ActionAdapter(Frame1 adaptee) {
            this.adaptee = adaptee;
        }

        public void actionPerformed(ActionEvent actionEvent) {
            adaptee.jMenuHelpAbout_actionPerformed(actionEvent);
        }
    }

我相信您的代码如此混乱的原因是因为它是由一些GUI构建器自动生成的。由于组件数量众多,界面复杂,您可能需要手动编写。这里必须有某种模式,您可以利用它来减少代码量,使其更易于理解和维护

除此之外,我认为问题可能在于组件“添加”到
JScrollPane
s的方式。你曾经

jScrollPaneX.getViewport().add(comp)

这不是将视图指定给滚动窗格的常用方法。标准方法是在构造时选择视图组件,例如调用

jScrollPanex=新的JScrollPane(comp)

或者如果以后需要设置,请致电

jScrollPaneX.setViewportView(comp)


(在内部,这将调用
getViewport().setView(view)
,这与您的操作方式类似,但不相同。)

我通过对代码进行少量检查来解决我的问题:

我只是删除这一行:

jTextArea1.setPreferredSize(new Dimension(280, 60));
别忘了,若您为Jtextarea设置了大小(任意大小),屏幕上将不会为您显示滚动窗格:)


工作完成了,享受吧……

至少有16个
JScollPane
s在里面。如果在一个(短得多)的示例上进行复制,或者至少告诉用户哪个窗格没有按预期工作,那么调试可能会更容易。有关裁剪示例的提示,请参阅。我上载了所有代码,因为运行它很容易,您可以跟踪这些Jscrollpane的问题:jScrollPane1,jScrollPane16 JSCROLLPANE4不,不是。这是一大块代码,从可见的第三方代码中导入内容(
import com.borland.jbcl.layout.*;
)。如果您提供的内容包括
main()
,并且没有依赖项(或者如果您详细说明从何处/如何获取这些dep),您可能会找到人进行调查。我们不需要“删除它们”。这取决于您是否发布易于阅读和理解的合理代码。这就是为什么你得到了发布SSCCE的链接。我们不能只看滚动窗格1、4、16,因为所有的代码都在一起工作。因此,我们需要了解整个程序以了解它们是如何协同工作的。可以通过在构造函数中使用适当的参数来设置
JTextArea
的首选大小。当有更好的方法来指定组件的大小时,随机猜测组件的大小既不是必要的,也不是可取的。我猜你不明白我的问题?!!如果为jtextArea设置大小,然后将其添加到jscrollpane,则JSCrollepane无法工作!对不起,那是错的。问题不在于设置大小,而在于JScrollPane没有正确链接到JTextArea。我的答案仍然有效。我知道我的错误很有趣,我问这个问题是因为我也收到了这个错误,所以我也很高兴……不管怎样,我的问题现在解决了