【问题标题】:JTextArea is not being displayedJTextArea 未显示
【发布时间】:2015-03-04 18:08:31
【问题描述】:

我创建了一个 JFrame,在其中创建了 JTextArea。我已将此 JTextArea 作为构造函数传递给另一个类。 JFrame如下:

JFrame frame = new JFrame("Find");
            frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            frame.setLayout(new BorderLayout());

            JTextArea textArea = new JTextArea(20,15);
            frame.add(textArea,BorderLayout.NORTH);

            textArea.setWrapStyleWord(true);
            textArea.setLineWrap(true);

            Texts text = new Texts(textArea);

           frame.add(text.pane(),BorderLayout.CENTER);

            JScrollPane pane = new JScrollPane(textArea);

           pane.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS);
            pane.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);

            frame.setSize(400, 400);
            frame.setLocationRelativeTo(null);
            frame.setVisible(true);

            frame.addWindowListener(new WindowAdapter() {
                public void windowClosing(WindowEvent ev) {

                    JOptionPane.showMessageDialog(null, "Thank you for using finder");    
                            System.exit(0); //Close program

                        }     

            });
            }

我已将 JTextArea 传递给其他类,但它并未显示在 JFrame 中,仅显示按钮。:

公开课文{

public JTextArea tx;
JTextField  findField = new JTextField( 10);

int pos = 0;

public Texts(JTextArea textArea) {
    // TODO Auto-generated constructor stub

tx=textArea;
tx.setVisible(true);

}

public Component pane() {
    // TODO Auto-generated method stub

    JButton findButton = new JButton("Find");
    JButton clearButton = new JButton("Clear");

        JPanel header = new JPanel(new GridBagLayout());

            GridBagConstraints gbc = new GridBagConstraints();
            gbc.gridx = 0;
            gbc.gridy = 0;
            gbc.anchor = GridBagConstraints.WEST;
            header.add(findField, gbc);
            gbc.gridx++;
            header.add(findButton, gbc);
            tx.add(header, BorderLayout.SOUTH);
            header.add(clearButton);

            findButton.addActionListener(new ActionListener() {

                @Override

                public void actionPerformed(ActionEvent e) {

                    // Get the text to find...convert it to lower case for easier comparison
                    String find = findField.getText();

                    // Focus the text area, otherwise the highlighting won't show up

                    tx.requestFocusInWindow();

                    // Make sure we have a valid search term
                    if (find != null && find.length() > 0) {
                        Document document = tx.getDocument(); 


                      ..........

【问题讨论】:

  • 缩进所有代码,以便突出显示所有代码。您可以通过选择文本并按下 {} 按钮来完成此操作

标签: java jframe jpanel textarea


【解决方案1】:

首先你这样做......

frame.add(textArea,BorderLayout.NORTH);

然后你这样做......

JScrollPane pane = new JScrollPane(textArea);

这有效地从框架中删除了textArea,这是因为组件只能有一个父级。

相反,尝试类似...

JScrollPane pane = new JScrollPane(textArea);
frame.add(pane,BorderLayout.NORTH);

【讨论】:

    猜你喜欢
    • 2017-01-18
    • 1970-01-01
    • 1970-01-01
    • 2015-06-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多