java中jscrollpane的问题

时间:2011-03-12 10:52:11

标签: java swing jscrollpane

我在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);
        }
    }

2 个答案:

答案 0 :(得分:4)

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

除此之外,我认为问题可能归结为组件被“添加”到JScrollPane的方式。你用过

jScrollPaneX.getViewport().add(comp);

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

jScrollPanex = new JScrollPane(comp)

或者如果您需要稍后再设置,请致电

jScrollPaneX.setViewportView(comp)

(在内部,这会调用getViewport().setView(view),它与您的方式类似,但不一样。)

答案 1 :(得分:-2)

我通过对我的代码进行小规模审核解决了我的问题:

我只是删除了这一行:

jTextArea1.setPreferredSize(new Dimension(280, 60));

不要忘记,如果您为Jtextarea设置尺寸(任何尺寸),滚动窗格将不会出现在屏幕上:)

完成工作,享受它......