【问题标题】:Adding a ScrollPane is making my JTextArea disappear添加 ScrollPane 使我的 JTextArea 消失
【发布时间】:2013-03-31 09:12:44
【问题描述】:

如果我有滚动条,JTextArea 就会消失。没有它,一切都很好。有人可以向我解释一下吗?以及如何解决它!

另外,顺便说一句,是否可以在滚动窗格上更改颜色或设置边框?

以下是相关代码:

        //Text Container
    JPanel textCon = new JPanel();
    textCon.setOpaque(false);
    textCon.setLayout(new GridLayout(1,3));
    add(textCon);

    //Left Filler
    JPanel left = new JPanel();
    left.setOpaque(false);
    textCon.add(left);

    //Text area
    mainText = new JTextArea("SAMPLE");
    mainText.setOpaque(true);
    mainText.setSize(50,30);
    mainText.setLineWrap(true);
    mainText.setWrapStyleWord(true);
    textCon.add(mainText);

    //Set textAre fonts, colors, border/padding
    mainText.setBorder(BorderFactory.createCompoundBorder(BorderFactory.createLineBorder(Color.GREEN), BorderFactory.createEmptyBorder(10, 10, 10, 10)));
    mainText.setFont(new Font("sansserif", Font.BOLD, 10));
    mainText.setForeground(Color.GREEN);
    mainText.setBackground(Color.BLACK);

    //Scroll Bar
    scroller = new JScrollPane(mainText);
    scroller.setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS);
    mainText.add(scroller);

这是其余的,以防万一!

import java.awt.*;
import java.awt.event.*;
import java.awt.Toolkit.*;
import javax.swing.*;
import java.io.*;
import javax.swing.border.*;
import java.util.*;

class ProjectMain extends JFrame
{
//Declare String/Array for mainText
String output = "";
String [] hero;
int page = 0;
JTextArea mainText;
JScrollPane scroller;

public ProjectMain()
{
    //Set Background
    setTitle("JLA Viewer");
    setSize(1920,1080);
    setLocationRelativeTo(null);
    setDefaultCloseOperation(EXIT_ON_CLOSE);
    setVisible(true);
    setContentPane(new JLabel(new ImageIcon("bg.png")));
    setLayout(new GridLayout(3,1));

    //Refresh Background
    setSize(1919,1079);
    setSize(1920,1080);

    //Label Container
    JPanel labelCon = new JPanel();
    labelCon.setOpaque(false);
    labelCon.setLayout(new BorderLayout());
    add(labelCon);

    //Top Label
    JLabel topLabel = new JLabel("JLA Profile Viewer");
    topLabel.setHorizontalAlignment(SwingConstants.CENTER);
    topLabel.setForeground(Color.GREEN);
    topLabel.setOpaque(false);
    topLabel.setFont(new Font("sansserif", Font.BOLD, 30));
    labelCon.add(BorderLayout.NORTH,topLabel);

    //Logo
    ImageIcon logo = new ImageIcon("logo.jpg");
    JLabel logoLabel = new JLabel();
    logoLabel.setHorizontalAlignment(SwingConstants.CENTER);
    logoLabel.setIcon(logo);
    labelCon.add(BorderLayout.SOUTH,logoLabel);

    //Text Container
    JPanel textCon = new JPanel();
    textCon.setOpaque(false);
    textCon.setLayout(new GridLayout(1,3));
    add(textCon);

    //Left Filler
    JPanel left = new JPanel();
    left.setOpaque(false);
    textCon.add(left);

    //Text area
    mainText = new JTextArea("SAMPLE");
    mainText.setOpaque(true);
    mainText.setSize(50,30);
    mainText.setLineWrap(true);
    mainText.setWrapStyleWord(true);
    textCon.add(mainText);

    //Set textAre fonts, colors, border/padding
    mainText.setBorder(BorderFactory.createCompoundBorder(BorderFactory.createLineBorder(Color.GREEN), BorderFactory.createEmptyBorder(10, 10, 10, 10)));
    mainText.setFont(new Font("sansserif", Font.BOLD, 10));
    mainText.setForeground(Color.GREEN);
    mainText.setBackground(Color.BLACK);

    //Scroll Bar
    scroller = new JScrollPane(mainText);
    scroller.setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS);
    mainText.add(scroller);

    //Right Filler
    JPanel right = new JPanel();
    right.setOpaque(false);
    textCon.add(right);

    //Button Container
    JPanel buttonContainer = new JPanel();
    buttonContainer.setOpaque(false);
    add(BorderLayout.SOUTH,buttonContainer);

    //PREV Button
    JButton prev = new JButton("PREV");
    prev.setOpaque(false);
    ActionListener PrevList = new PrevButton(); //Call Button Listener
    prev.addActionListener(PrevList);
    buttonContainer.add(prev);

    //EXIT Button
    JButton exit = new JButton("EXIT");
    exit.setOpaque(false);
    ActionListener ExitList = new ExitButton(); //Call Button Listener
    exit.addActionListener(ExitList);
    buttonContainer.add(exit);

    //NEXT Button
    JButton next = new JButton("NEXT");
    next.setOpaque(false);
    ActionListener NextList = new NextButton(); //Call Button Listener
    next.addActionListener(NextList);
    buttonContainer.add(next);

    //File Handling
    try
    {
        File inputFile = new File ("ProjectInputFile.txt");
        Scanner scanner = new Scanner(inputFile);

        while(scanner.hasNextLine())
        {
            output = output + (scanner.nextLine() + "\n");
        }
    }
    catch(IOException ioe)
    {

    }

    //Add split strings to array
    hero = output.split("@");
}

//Prev button event listener
public class PrevButton implements ActionListener
{
    public void actionPerformed(ActionEvent e)
    {
        page = page - 1;
        if(page < 0)
        {
            page = hero.length;
        }
        mainText.setText(hero[page]);
    }
}

//Exit button event listener
public class ExitButton implements ActionListener
{
    public void actionPerformed(ActionEvent e)
    {
        setVisible(false);
        dispose();
        System.exit(0);
    }
}

//Next button event listener
public class NextButton implements ActionListener
{
    public void actionPerformed(ActionEvent e)
    {
        page++;
        if(page > (hero.length))
        {
            page = 0;
        }
        mainText.setText(hero[page]);
    }
}

public static void main(String args[])
{
    new ProjectMain();
}

}

【问题讨论】:

    标签: java swing jscrollpane jtextarea


    【解决方案1】:

    这一行

    mainText.add(scroller);

    尝试将JScrollPane 添加到JTextArea。您已经设置了JScrollPane 的ViewPortView,因此不需要此行。

    您仍然需要添加JSrolllPane:

    textCon.add(scroller);
    

    还要确保在添加所有组件之后调用JFrame#setVisible。

    setVisible(true);
    

    是否可以在滚动窗格上更改颜色或设置边框?

    当然。不要在JTextArea mainText 上设置边框,而是在JScrollPane 上设置它。

    旁注:

    • 不要在不显示异常内容的情况下静默捕捉IOException
    • JFrames 通常直接使用而不是扩展

    【讨论】:

    • 谢谢,但这并没有真正帮助。如果不添加滚动窗格,我就没有滚动窗格。添加滚动窗格会使 JTextArea 消失。我扩展了 JFrame,因为它是针对大学里的一个项目,而这正是讲师出于某种原因想要它的方式。
    • 将JScrollPane 添加到textCon JPanel 而不是`JTextArea。查看更新
    • 再次感谢您抽出宝贵时间提供帮助,但如果我将滚动窗格添加到 textCon,那么布局就会变得混乱。我认为这是因为它在一个网格上并且所有三个插槽都被占用了,所以我去掉了一个填充面板并在其中添加了滚动窗格,但后来我得到一个大的灰色块占用了网格,我是否将 opaque 设置为 false 并且仍然没有滚动窗格。这只是一个大学项目,我有一个解决方法(更少的文本 = 不需要滚动;D)所以它不是那么重要,只是让我很烦我无法让它工作。
    【解决方案2】:

    您只需要将setBounds(x,y,z,w) 发送到您的JScrollPane 而不是您的JTextArea。然后将您的JSrcollPane 添加到您的JFrame 或您的JPanel。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多