2013-03-31 31 views
0

如果我有滾動條,JTextArea就會消失。沒有它,一切都很好。有人可以向我解釋這個嗎?以及如何解決它!添加ScrollPane讓我的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(); 
} 

}

回答

2

此行

mainText.add(卷軸);

嘗試將JScrollPane添加到JTextArea。您已經設置了JScrollPaneViewPortView,因此該行是不必要的。

您仍然需要添加JSrolllPane

textCon.add(scroller); 

同時請確保調用JFrame#setVisible後,所有組件都被添加。

setVisible(true); 

是有可能改變顏色或設置邊框上滾動面板?

當然。請將其設置在JScrollPane上,而不是在JTextAreamainText上設置邊框。

旁註:

  • 不要默默追趕IOException沒有表現出異常的內容通常用於
  • JFrames直接而不是擴展
+0

謝謝,但這並沒有真正的幫助。不添加滾動窗格,我沒有滾動窗格。添加滾動窗格使JTextArea消失。我擴展了JFrame,因爲它適用於大學的一個項目,講師因爲某種原因需要它。 – Aezur

+0

將'JScrollPane'添加到'textCon'' JPanel'而不是'JTextArea。請參閱更新 – Reimeus

+0

再次感謝您花時間提供幫助,但如果將滾動窗格添加到textCon,則佈局會變得混亂。我認爲這是因爲它在一個網格上,並且所有三個插槽都被佔用,所以我擺脫了其中一個填充面板,並在其中添加了滾動窗格,但是接下來我只是得到一個大的灰色塊來佔據網格,我是否將不透明設置爲false或不是滾動窗格。這只是一個大學項目,我有一個解決方法(少文本=不需要滾動; D)所以它不是那麼重要,它只是令人討厭,我不能讓它工作。 – Aezur

-1

你只需要setBounds(x,y,z,w)JScrollPane而不是您的JTextArea。然後將您的jSrcollPane添加到您的jPaneljFrame