java - JScrollPane 不在 MigLayout 中滚动

标签 java jscrollpane jtextarea miglayout

在此代码中,我尝试将 JScrollPane 插入到我的面板中,该面板使用 MigLayout .

import java.awt.*;
import javax.swing.*;
import net.miginfocom.swing.MigLayout;

public class Simple2
{
    JFrame simpleWindow = new JFrame("Simple MCVE");

    JPanel  simplePanel = new JPanel();

    JLabel lblTitle;
    JLabel lblSimple;
    JTextArea txtSimple;
    JScrollPane spSimple;

    GraphicsEnvironment ge = GraphicsEnvironment.getLocalGraphicsEnvironment();
    String []fontFamilies = ge.getAvailableFontFamilyNames();

    public void numberConvertGUI()
    {
        simpleWindow.setBounds(10, 10, 285, 170);

        simpleWindow.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        simpleWindow.setLayout(new GridLayout(1,1));

        createSimplePanel();

        simpleWindow.getContentPane().add(simplePanel);

        simpleWindow.setVisible(true);

        simpleWindow.setResizable(false);
    }

    public void createSimplePanel()
    {
        MigLayout layout = new MigLayout("" , "[][grow]");
        simplePanel.setLayout(layout);

        lblTitle = new JLabel();
        lblTitle.setText("This is a Title");
        simplePanel.add(lblTitle, "wrap, align center,span 2");

        lblSimple = new JLabel();
        lblSimple.setText("Next to me is a JTextArea: ");
        simplePanel.add(lblSimple);

        txtSimple = new JTextArea();
        txtSimple.setLineWrap(false);
        txtSimple.setWrapStyleWord(true);
        spSimple = new JScrollPane(txtSimple); 
        simplePanel.add(txtSimple,"width 100:100:100 , height 100:100:100");
    }

    public static void main(String[] args)
    {
        Simple2 s = new Simple2();
        s.numberConvertGUI();
    }
}

但是,当文本到达 JTextArea 的末尾并继续离开屏幕时,不会出现水平或垂直滚动​​条。我不确定我做错了什么。

使用txtSimple.setLineWrap(false);

txtSimple.setLineWrap(false);

使用txtSimple.setLineWrap(true);

txtSimple.setLineWrap(true);

编辑

我想要的结果是在滚动 Pane 上有滚动条

enter image description here

enter image description here

提供这些示例的代码是

import java.awt.*;
import javax.swing.*;
import java.lang.Object.*;
import javax.swing.event.*;
import javax.swing.text.*;
import java.awt.event.*;
import java.awt.Checkbox;

public class TextAreaSample extends JFrame implements ActionListener
{
    JFrame myMainWindow = new JFrame("This is my title");

    JTabbedPane myTabs = new JTabbedPane();

    JPanel  firstPanel = new JPanel(); //a panel for first tab

    //first panel components
    JTextArea txtSimple;
    JLabel lblSimple;
    JScrollPane myScrollTable;
    JCheckBox TextWrap;
    //end first panel

    public void runGUI()
    {
        myMainWindow.setBounds(10, 10, 800, 800); //set position, then dimensions
        myMainWindow.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        myMainWindow.setLayout(new GridLayout(1,1));

        createFirstPanel(); //call method to create each panel

        myMainWindow.getContentPane().add(firstPanel); //adds the tabbedpane to mainWindow

        myMainWindow.setVisible(true); //make the GUI appear
    }

    public void createFirstPanel()
    {
        firstPanel.setLayout(null);

        txtSimple = new JTextArea();
        txtSimple.setLineWrap(false);
        txtSimple.setWrapStyleWord(true);
        myScrollTable = new JScrollPane(txtSimple); 
        myScrollTable.setSize(700,700); 
        myScrollTable.setLocation(20,20);
        firstPanel.add(myScrollTable);
        System.out.println("Creating compare table");

        lblSimple = new JLabel();
        lblSimple.setText("Text Wrap");
        lblSimple.setSize(100,25); 
        lblSimple.setLocation(20,730);
        lblSimple.setHorizontalAlignment(JLabel.RIGHT);
        firstPanel.add(lblSimple);

        TextWrap = new JCheckBox();
        TextWrap.setLocation(125,730);
        TextWrap.setSize(25,25); 
        TextWrap.addActionListener(this);
        firstPanel.add(TextWrap);
    }

    public void actionPerformed(ActionEvent e)
    {
        if(TextWrap.isSelected())
        {
            txtSimple.setLineWrap(true);
        }

        else
        {
            txtSimple.setLineWrap(false);
        }
    }

    public static void main(String[] args)
    {
        TextAreaSample TSA = new TextAreaSample();
        TSA.runGUI();
    }
}

最佳答案

问题是这样的:

spSimple = new JScrollPane(txtSimple); 
simplePanel.add(txtSimple,"width 100:100:100 , height 100:100:100");

您没有将 JScrollPane 添加到布局中。你需要这个:

spSimple = new JScrollPane(txtSimple); 
simplePanel.add(spSimple,"width 100:100:100 , height 100:100:100");

请注意第二行中 spSimple 的使用。

关于java - JScrollPane 不在 MigLayout 中滚动,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30488310/

相关文章:

java - 使用 JSch SFTP 客户端连接到 FileZilla 服务器时出错

javascript - jScrollPane:当我调用 reinitialise() 时,不会在谷歌浏览器中创建 jspVerticalBar div

java - 在 JTextPane 中绘制水平线

java - 记事本程序中的复制按钮

java - 我无法让 JScrollPanes 实际显示滚动条

java - 为什么除非调整窗口大小,否则无法在 JTextArea 中键入内容?

java - Apache POI 的 XWPF 是否支持段落的自动连字符功能?

java - OSGi内存管理

java - 如何使用 JAXB 编码 Map<String, List<Objects>>

java - 为什么在这种特殊情况下向 JList 添加元素会使其为空?