JScrollPane不在MigLayout

时间:2015-05-27 16:34:33

标签: 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);

修改

我想要的结果是滚动窗格上有滚动条

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();
    }
}

1 个答案:

答案 0 :(得分:1)

问题在于:

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