2013-07-06 44 views
0

这里是我的代码:无法使JTextArea可滚动..!

final JTextArea textArea = new JTextArea(); 
textArea.setFont(new Font("MS UI Gothic", Font.PLAIN, 13)); 
textArea.setLineWrap(true); 
textArea.setBounds(77, 310, 474, 136); 
//contentPane.add(textArea); (edited...still the same problem persists..) 


JScrollPane sbrText = new JScrollPane(textArea); 
sbrText.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS); 
contentPane.add(sbrText); 

当过我尝试这一点,文本区域是不可见的。(我使用Eclipse的窗口Builder插件和布局为“绝对布局”)..

+2

您不需要将'textArea'添加到'contentPane'中,因为它已经包含在'sbrText'中。有关更多信息,请参阅[本教程](http://docs.oracle.com/javase/tutorial/uiswing/components/scrollpane.html)。 –

+0

@MichaelLang:是的......试过这个太友...没有运气.. !! –

+0

为了更好地提供帮助,请发布[SSCCE](http://sscce.org/)。 –

回答

2

在这里,我都做了,同样的啄使用Nested Layout,看看代码示例:

import java.awt.*; 
import javax.swing.*; 

public class WelcomeExample 
{ 
    private JPanel headerPanel; 
    private JButton logoutButton; 

    private JPanel leavePanel; 
    private JRadioButton casualRButton; 
    private JRadioButton specialRButton; 
    private JRadioButton sickRButton; 
    private JRadioButton privilegeRButton; 
    private ButtonGroup radioButtonGroup; 

    private JTextField leaveDaysField; 
    private JButton checkLeaveButton; 

    private JTextArea notesArea; 
    private JScrollPane notesScroller; 

    private JButton applyLeaveButton; 

    private String headerText = "<html><body><h1><font " + 
      "color=\"red\">Welcome : </font><font color" + 
      "=\"blue\">Code Zero</font></h1></body></html>"; 

    private void displayGUI() 
    { 
     JFrame frame = new JFrame("Welcome"); 
     frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE); 

     JPanel contentPane = new JPanel(); 
     contentPane.setLayout(new BorderLayout(5, 5)); 
     contentPane.setBorder(
      BorderFactory.createEmptyBorder(5, 5, 5, 5)); 

     headerPanel = getHeaderPanel(); 
     leavePanel = getLeavePanel(); 

     contentPane.add(headerPanel, BorderLayout.PAGE_START); 
     contentPane.add(leavePanel, BorderLayout.CENTER); 
     contentPane.add(getApplyPanel(), BorderLayout.PAGE_END); 

     frame.setContentPane(contentPane); 
     frame.pack(); 
     frame.setLocationByPlatform(true); 
     frame.setVisible(true); 
    } 

    private JPanel getHeaderPanel() 
    { 
     JPanel panel = new JPanel(); 
     panel.setLayout(new BorderLayout(5, 5)); 
     panel.setBorder(BorderFactory.createEmptyBorder(5, 5, 5, 5)); 
     JLabel headerLabel = new JLabel(headerText, JLabel.CENTER); 
     JPanel buttonPanel = new JPanel(); 
     logoutButton = new JButton("Logout"); 
     buttonPanel.add(logoutButton); 
     panel.add(headerLabel, BorderLayout.CENTER); 
     panel.add(buttonPanel, BorderLayout.LINE_END); 
     panel.add(new JSeparator(
      SwingConstants.HORIZONTAL), BorderLayout.PAGE_END); 

     return panel; 
    } 

    private JPanel getLeavePanel() 
    { 
     JPanel panel = new JPanel(); 
     panel.setLayout(new BorderLayout(5, 5)); 
     panel.setBorder(BorderFactory.createEmptyBorder(5, 5, 5, 5)); 

     JPanel leaveHeaderPanel = new JPanel(); 
     leaveHeaderPanel.setLayout(new GridLayout(0, 1, 5, 5)); 
     leaveHeaderPanel.setBorder(
      BorderFactory.createTitledBorder("Choose a leave type : ")); 
     JPanel leaveTypePanel = new JPanel(); 
     leaveTypePanel.setLayout(new FlowLayout(
           FlowLayout.LEFT, 5, 5)); 
     casualRButton = new JRadioButton("Casual Leave"); 
     specialRButton = new JRadioButton("Special Leave"); 
     sickRButton = new JRadioButton("Sick Leave"); 
     privilegeRButton = new JRadioButton("Privilege Leave"); 

     radioButtonGroup = new ButtonGroup(); 
     radioButtonGroup.add(casualRButton); 
     radioButtonGroup.add(specialRButton); 
     radioButtonGroup.add(sickRButton); 
     radioButtonGroup.add(privilegeRButton); 

     leaveTypePanel.add(casualRButton); 
     leaveTypePanel.add(specialRButton); 
     leaveTypePanel.add(sickRButton); 
     leaveTypePanel.add(privilegeRButton); 

     JPanel applyLeavePanel = new JPanel(); 
     applyLeavePanel.setLayout(new FlowLayout(
           FlowLayout.LEFT, 5, 5)); 
     JLabel applyLeaveLabel = new JLabel(
       "Apply for (No. of days) : ", JLabel.CENTER); 
     leaveDaysField = new JTextField(5); 
     checkLeaveButton = new JButton("Check Leave Availability"); 

     applyLeavePanel.add(applyLeaveLabel); 
     applyLeavePanel.add(leaveDaysField); 
     applyLeavePanel.add(checkLeaveButton); 

     leaveHeaderPanel.add(leaveTypePanel); 
     leaveHeaderPanel.add(applyLeavePanel); 

     notesArea = new JTextArea(10, 10); 
     notesScroller = new JScrollPane(); 
     notesScroller.setBorder(
      BorderFactory.createTitledBorder(
       "Leave Note (Max. 200 Characters) : ")); 
     notesScroller.setViewportView(notesArea); 

     panel.add(leaveHeaderPanel, BorderLayout.PAGE_START); 
     panel.add(notesScroller, BorderLayout.CENTER); 

     return panel; 
    } 

    private JPanel getApplyPanel() 
    { 
     JPanel panel = new JPanel(); 
     applyLeaveButton = new JButton("Apply"); 
     panel.add(applyLeaveButton); 

     return panel; 
    } 

    public static void main(String[] args) 
    { 
     Runnable runnable = new Runnable() 
     { 
      @Override 
      public void run() 
      { 
       new WelcomeExample().displayGUI(); 
      } 
     }; 
     EventQueue.invokeLater(runnable); 
    } 
} 

输出:

WELCOME EXAMPLE

+0

啊......只是我在找.. ..! 谢谢maitey .. !! :D(Y) –

+0

与您的代码进行比较,我非常确定,它不包含大惊小怪,我们正在讨论与使用布局管理器相关的问题:-),其余的部分您最喜欢并保持微笑: - ) –

+0

肯定的事情..! :) –

1

我认为你不需要做contentPane.add(textArea);。正是这条线导致了这个问题。注释掉这个,你的代码应该可以正常工作。

请参阅this答案,它可能会帮助你。

下面的代码运行在我的地方罚款:

final JTextArea textArea = new JTextArea(); 
     textArea.setFont(new Font("MS UI Gothic", Font.PLAIN, 13)); 
     textArea.setLineWrap(true); 
     textArea.setBounds(77, 310, 474, 136); 

     JScrollPane sbrText = new JScrollPane(textArea); 
     sbrText.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS); 
     frame.getContentPane().add(sbrText);//contentPane.add(sbrText); 
     frame.setVisible(true); 

如果你的代码不罚款运行,那么你必须有可能与您的contentpane.

+0

根据之前的评论/建议,我也尝试过......但是问题依然如此。 :( - –

+0

此外,我也尝试在此代码片段:http://stackoverflow.com/questions/8849063/adding-a-scrollable-jtextarea-java ... 仍然没有运气.. !! :-( –

+0

@CodeZero:你有没有尝试像这样'JTextArea tArea = new JTextArea(20,20);'提供'rows/columns'(不使用无参数的构造函数)来初始化'JTextArea'。给一些大小(不要使用setXxX()方法,只是这个东西)以这种方式到'JTextArea',以便'JScrollPane'可以依靠它。答案是好的,毫无疑问,在这:-),已经upvoted这一个 –

1

好了一些其他错误...我终于得到了它的工作......我在滚动窗格而不是文本区域中指定了相同的集合边界。这里是代码。^_^

 final JTextArea textArea = new JTextArea(); 
     textArea.setFont(new Font("MS UI Gothic", Font.PLAIN, 13)); 
     textArea.setLineWrap(true); 
     //textArea.setBounds(77, 310, 474, 136); 

     JScrollPane scroll = new JScrollPane (textArea, JScrollPane.VERTICAL_SCROLLBAR_ALWAYS, JScrollPane.HORIZONTAL_SCROLLBAR_NEVER);   
     scroll.setBounds(77, 310, 474, 136); 
     contentPane.add(scroll); 

以下是截图:

上一页:http://oi40.tinypic.com/11jyum0.jpg
现在:http://oi44.tinypic.com/2s9vdvt.jpg

+1

这就是你做错了什么,使用'绝对定位'__(所有问题的祸害)__。相反,你应该使用嵌套布局来实现这个'View' :-) –

+0

哈哈..好吧..但由于我是JAVA编程的新手,绝对定位我似乎得到了我所需要的,没有太多大惊小怪。 :P 感谢您的所有提示,但.. ..! :) –

+0

当我刚刚接触Java Swing时,我现在用同样的错误做,但后来我意识到Layouts是Swing的一部分是多么重要:-) –