【问题标题】:GroupLayout: JScrollPane in TextArea is not workingGroupLayout:TextArea 中的 JScrollPane 不起作用
【发布时间】:2012-07-01 18:27:16
【问题描述】:

我是 Java 新手,最近开始开发一个简单的应用程序。目前我对 JScrollPanne 有疑问,当 textarea 中的文本超过区域大小时,它无法向下(或向上)滚动。我已经查看了一些解决方案,但它们都适用于 FlowLayot(GridLayout 和 BoxLayout),但不适用于 GroupLayout。代码如下:

JPanel conent_p = new JPanel();
    conent_p.setBorder(new TitledBorder(null, "", TitledBorder.LEADING, TitledBorder.TOP, null, null));

    JLabel lblItemName = new JLabel("Item name:");
    itemField = new JTextField();
    itemField.setColumns(10);

    JLabel lblMxPrice = new JLabel("Max price:");   
    mpriceField = new JTextField();
    mpriceField.setColumns(10);

    JLabel lblQuantity = new JLabel("Quantity:");
    quanField = new JTextField();
    quanField.setColumns(10);

    JLabel lblDelivery = new JLabel("Delivery:");
    delivField = new JTextField();
    delivField.setColumns(10);

    JLabel lblLogcat = new JLabel("LogCat:");
    final JTextArea txtConsole = new JTextArea();
    txtConsole.setEditable(false);
    txtConsole.setLineWrap(true);
    txtConsole.setWrapStyleWord(true);

    sbrText = new JScrollPane(txtConsole);
    sbrText.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);

    // Now create a new TextAreaOutputStream to write to our JTextArea control and wrap a
    // PrintStream around it to support the println/printf methods.
    PrintStream out = new PrintStream(new TextAreaOutputStream(txtConsole));
    // redirect standard output stream to the TextAreaOutputStream
    System.setOut(out);
    // redirect standard error stream to the TextAreaOutputStream
    System.setErr(out);

    GroupLayout gl_conent_p = new GroupLayout(conent_p);
    gl_conent_p.setHorizontalGroup(
        gl_conent_p.createParallelGroup(Alignment.LEADING)
            .addGroup(gl_conent_p.createSequentialGroup()
                .addContainerGap()
                .addGroup(gl_conent_p.createParallelGroup(Alignment.LEADING)
                    .addComponent(lblMxPrice, Alignment.TRAILING)
                    .addComponent(lblItemName, Alignment.TRAILING)
                    .addComponent(lblLogcat, Alignment.TRAILING))
                .addGap(18)
                .addGroup(gl_conent_p.createParallelGroup(Alignment.LEADING)
                    .addGroup(gl_conent_p.createSequentialGroup()
                        .addGroup(gl_conent_p.createParallelGroup(Alignment.LEADING, false)
                            .addComponent(itemField, GroupLayout.PREFERRED_SIZE, 365, GroupLayout.PREFERRED_SIZE)
                            .addGroup(gl_conent_p.createSequentialGroup()
                                .addComponent(mpriceField, GroupLayout.PREFERRED_SIZE, 80, GroupLayout.PREFERRED_SIZE)
                                .addGap(18)
                                .addComponent(lblQuantity)
                                .addPreferredGap(ComponentPlacement.RELATED)
                                .addComponent(quanField, 0, 0, Short.MAX_VALUE)
                                .addGap(18)
                                .addComponent(lblDelivery)
                                .addPreferredGap(ComponentPlacement.RELATED)
                                .addComponent(delivField, GroupLayout.PREFERRED_SIZE, 80, GroupLayout.PREFERRED_SIZE)
                                .addPreferredGap(ComponentPlacement.RELATED)))
                        .addGap(100))
                    .addGroup(gl_conent_p.createSequentialGroup()
                        .addComponent(txtConsole, GroupLayout.PREFERRED_SIZE, 345, GroupLayout.PREFERRED_SIZE)
                        .addComponent(sbrText)
                        .addContainerGap())))
    );
    gl_conent_p.setVerticalGroup(
        gl_conent_p.createParallelGroup(Alignment.LEADING)
            .addGroup(gl_conent_p.createSequentialGroup()
                .addContainerGap()
                .addGroup(gl_conent_p.createParallelGroup(Alignment.BASELINE)
                    .addComponent(lblItemName)
                    .addComponent(itemField, GroupLayout.PREFERRED_SIZE, 20, GroupLayout.PREFERRED_SIZE))
                .addGap(20)
                .addGroup(gl_conent_p.createParallelGroup(Alignment.LEADING)
                    .addGroup(gl_conent_p.createParallelGroup(Alignment.BASELINE)
                        .addComponent(lblDelivery)
                        .addComponent(delivField, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE))
                    .addGroup(gl_conent_p.createParallelGroup(Alignment.BASELINE)
                        .addComponent(lblMxPrice)
                        .addComponent(mpriceField, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
                        .addComponent(lblQuantity)
                        .addComponent(quanField, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)))
                .addGap(55)
                .addGroup(gl_conent_p.createParallelGroup(Alignment.BASELINE)
                    .addComponent(lblLogcat)
                    .addComponent(txtConsole, GroupLayout.PREFERRED_SIZE, 200, GroupLayout.PREFERRED_SIZE)
                    .addComponent(sbrText))
                .addContainerGap())
    );
    conent_p.setLayout(gl_conent_p);

    getContentPane().add(conent_p, BorderLayout.NORTH);

    JButton btnBuy = new JButton("Buy");
    btnBuy.addActionListener( new ActionListener() {
        public void actionPerformed(ActionEvent ev) {
            try {
                String title = itemField.getText().trim();
                String mprice = mpriceField.getText().trim();
                String quantity = quanField.getText().trim();
                String deliver = delivField.getText().trim();

                Item_CONCEPT item = new Item_CONCEPT();

                item.setName(title);
                item.setDelivery(Integer.parseInt(deliver));
                item.setStartPrice(0);
                item.setMaxPrice(Integer.parseInt(mprice));

                myAgent.existsSeller(item);

                Date date = new Date();
                DateFormat df = new SimpleDateFormat("dd.MM.yy HH:mm");
                System.out.println(df.format(date)+": Buyer orders an item: "+item.getName());

                //Clearing all fields
                itemField.setText("");
                quanField.setText("");
                delivField.setText("");
                //txtConsole.setText("");
                mpriceField.setText("");
            }
            catch (Exception e) {
                JOptionPane.showMessageDialog(BuyerGUI.this, "A field is filled incorrectly. "+e.getMessage()+" is invalid.", "Error", JOptionPane.ERROR_MESSAGE); 
            }
        }
    } );![enter image description here][1]

【问题讨论】:

  • 代码 sn-p 中实际引用 JTextAreaTextArea 的部分在哪里?我的“查找”没有找到任何一个字符串..

标签: java swing jscrollpane jtextarea layout-manager


【解决方案1】:

您观察到的问题来自两个事实:

  • 您正在将内容面板添加到BorderLayout.NORTH
  • 您将文本控制台和滚动窗格添加为单独的组件

对于第一个:替换

getContentPane().add(conent_p, BorderLayout.NORTH);

getContentPane().add(conent_p, BorderLayout.CENTER);

第二个:不要单独添加txtConsole,即

横向组内替换

.addGroup(gl_conent_p.createSequentialGroup()
  .addComponent(txtConsole, GroupLayout.PREFERRED_SIZE, 345, GroupLayout.PREFERRED_SIZE)
  .addComponent(sbrText).addContainerGap());

.addComponent(sbrText);

在垂直组内

.addGroup(gl_conent_p.createParallelGroup(Alignment.BASELINE)
  .addComponent(lblLogcat)
  .addComponent(txtConsole, GroupLayout.PREFERRED_SIZE, 200, GroupLayout.PREFERRED_SIZE)
  .addComponent(sbrText)).addContainerGap()));

.addGroup(gl_conent_p.createParallelGroup(Alignment.BASELINE)
  .addComponent(lblLogcat)
  .addComponent(sbrText)).addContainerGap()));

【讨论】:

    【解决方案2】:

    不确定这是否是问题所在(因为您没有提供 SSCCE,只是一段代码),但您同时添加了 txtConsolesbrText

    .addComponent(txtConsole, GroupLayout.PREFERRED_SIZE, 200, GroupLayout.PREFERRED_SIZE)
    .addComponent(sbrText)
    

    当你把文本区域放在一个滚动窗格中时,添加滚动窗格就足够了。

    此外,我建议设置滚动窗格的首选大小,如scroll pane tutorial所示

    【讨论】:

      猜你喜欢
      • 2013-11-15
      • 1970-01-01
      • 2016-04-17
      • 1970-01-01
      • 2013-09-27
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多