2016-11-06 132 views
-1

我试图将JScrollPane添加到我的大JTextArea中,但是无论何时包含JScrollPane代码,我的整个JTextArea都会消失。将JScrollPane添加到JPanel中的JTextArea

public myGUI() { 
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
    setBounds(100, 100, 1174, 656); 
    contentPane = new JPanel(); 
    contentPane.setBorder(new EmptyBorder(5, 5, 5, 5)); 
    setContentPane(contentPane); 
    contentPane.setLayout(null); 
    contentPane.setVisible(true); 

    JTextArea textArea_1 = new JTextArea(); 
    textArea_1.setBounds(203, 5, 869, 440); 
    textArea_1.setEditable(true); 
    textArea_1.setVisible(true); 
    contentPane.add(textArea_1); 

    JScrollPane scroll = new JScrollPane (textArea_1); 
    scroll.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS); 
    scroll.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS); 
    contentPane.add(scroll); 

}

回答

6

的几个问题与您的代码:

  • 你试图添加一个组件,在这里你的JTextArea叫textArea_1,多个集装箱,这里的contentPane 的JScrollPane中。 Swing中不能这样做,因为组件只能添加到一个组件中。将它仅添加到JScrollPane而不添加到contentPane,然后将滚动窗格添加到GUI。
  • 您通过setBounds约束了您的JTextArea的大小,这几乎可以保证JScrollPane不会工作,因为这样做会阻止JTextArea在保存比显示的文本更多的文本时展开。相反,请设置JScrollPane的行和列属性,而不是其边界。这将是它应该在滚动窗格中显示的行和列的数量
  • 您正在使用空布局但未指定JScrollPane的大小,因此它默认大小为[0,0] - 这就是为什么你的JTextArea消失。空布局需要完整规定所有组件的大小和位置。
  • 您正在使用空白布局来设置您的GUI。虽然Swing的新手可能看起来像是创建复杂GUI的最简单也是最好的方式,但更多Swing GUI的创建使用它们时会遇到更严重的困难。它们不会在GUI大小调整时调整组件的大小,它们是增强或维护的皇室女巫,当它们放在滚动窗格中时它们会完全失败,在所有平台或屏幕分辨率与原始视图不同时,它们看起来会非常糟糕。

例如:

import java.awt.BorderLayout; 
import java.awt.Font; 
import java.awt.GridLayout; 
import javax.swing.*; 

@SuppressWarnings("serial") 
public class MyGuiPanel extends JPanel { 
    // some row and column values for our JTextArea 
    private static final int TXT_AREA_ROWS = 25; 
    private static final int TXT_AREA_COLS = 80; 

    // create the JTextArea, passing in the rows and columns values 
    private JTextArea textArea = new JTextArea(TXT_AREA_ROWS, TXT_AREA_COLS); 

    public MyGuiPanel() { 
     // create the JScrollPane, adding our JTextArea 
     JScrollPane scroll = new JScrollPane(textArea); 
     scroll.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS); 
     scroll.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS); 

     // lets add some buttons to the bottom of the GUI just for fun 
     JPanel buttonPanel = new JPanel(new GridLayout(1, 0, 5, 0)); 
     buttonPanel.add(new JButton("Save")); 
     buttonPanel.add(new JButton("Open")); 
     buttonPanel.add(new JButton("Delete")); 
     buttonPanel.add(new JButton("Exit")); 

     // let's add a title to the top: 
     JLabel title = new JLabel("This is my Applications's Title", SwingConstants.CENTER); 
     title.setFont(title.getFont().deriveFont(Font.BOLD, 24)); // and make 
                    // the text 
                    // *BIG* 

     // use a BorderLayout for our GUI 
     setLayout(new BorderLayout(5, 5)); 
     setBorder(BorderFactory.createEmptyBorder(5, 5, 5, 5)); 
     add(scroll, BorderLayout.CENTER); // add the scrollpane to the center 
     add(buttonPanel, BorderLayout.PAGE_END); // the button panel to the 
               // bottom 
     add(title, BorderLayout.PAGE_START); // and the title JLabel to the top 
    } 

    private static void createAndShowGui() { 
     // create our GUI JPanel 
     MyGuiPanel mainPanel = new MyGuiPanel(); 

     // create a JFrame to add it to 
     JFrame frame = new JFrame("My GUI"); 
     frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE); 
     frame.getContentPane().add(mainPanel); // add the GUI to the JFrame 
     frame.pack(); // tell the layout managers to do their work 
     frame.setLocationByPlatform(true); 
     frame.setVisible(true); // display the GUI 
    } 

    public static void main(String[] args) { 
     SwingUtilities.invokeLater(() -> createAndShowGui()); 
    } 
} 

另外,如果你的程序扩展的JFrame,明白你是画自己在一个角落里做这个,迫使你创建和显示JFrames时往往更多灵活性呼吁。事实上,我敢打赌,我已经创建了大多数Swing GUI代码,而且我已经看到了而不是扩展了JFrame,事实上很少有人会想要这样做。更常见的是,您的GUI类将专门用于创建JPanel,然后可将其放置到JFrame或JDialogs或JTabbedPanes中,或在需要时通过CardLayouts进行交换。这将大大增加您的GUI编码的灵活性。

2

您不需要为您的JTextArea设置setBounds。由于您使用的是空布局,并且JScrollPane没有边界,因此不会显示任何内容。你的JTextArea也被添加到两个会导致一些问题的地方。我会建议任何波动layout managers。作为使用BorderLayout的一个例子,它是最简单的管理者之一:

public mygui() { 
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
    setVisible(true); 
    setSize(700, 500); 
    setLayout(new BorderLayout()); 

    JTextArea textArea_1 = new JTextArea(); 
    textArea_1.setEditable(true); 

    JScrollPane scroll = new JScrollPane(textArea_1); 
    scroll.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS); 
    scroll.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS); 

    add(scroll, BorderLayout.CENTER); 
}