java - JTextArea 不显示

标签 java swing layout-manager jtextarea null-layout-manager

为什么 JTextArea 没有显示在 GUI 中?

public class AddMovie extends JTextField {

    static JFrame frame;
    private JLabel description;
    JTextArea movieDescription;

    public JPanel createContentPane() throws IOException
    {

        JPanel totalGUI = new JPanel();
        totalGUI.setLayout(null);
        totalGUI.setBackground(Color.WHITE);

        description = new JLabel("Description  ");
            description.setLocation(15,285);
            description.setSize(120, 25);
            description.setFont(new java.awt.Font("Tahoma", 0, 12));
            movieDescription=new JTextArea();
          movieDescription.setLocation(15,320);
          movieDescription.setSize(420, 110);

        JScrollPane scrollPane = new JScrollPane(movieDescription);    
        totalGUI.add(description);
        totalGUI.add(movieDescription);
        totalGUI.add(cancel);
        totalGUI.add(scrollPane);                   
        return totalGUI;
    }


    static void createAndShowGUI() throws IOException
    {

        JFrame.setDefaultLookAndFeelDecorated(true);
        frame = new JFrame("New Movie");
        //Create and set up the content pane.
        AddMovie demo = new AddMovie();
        frame.setContentPane(demo.createContentPane());
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setSize(515, 520);
        frame.setLocation(480,120);
        frame.setVisible(true);
    }

    public void setVisible(boolean b) {
        // TODO Auto-generated method stub

    }

}

最佳答案

按照建议herehere , null 布局会带来麻烦。因为您的显示以描述为中心,所以使用 JTextArea允许您指定行和列大小的构造函数。当您 pack() 封闭的 Window 时,它将调整大小以适应文本区域。我添加了一些临时 文本来说明效果。我也updated调整框架大小时允许文本区域增长的代码。

image

import java.awt.EventQueue;
import java.awt.GridLayout;
import javax.swing.BorderFactory;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;

/**
 * @see https://stackoverflow.com/a/38282886/230513
 */
public class Test {

    private JPanel createPanel() {
        JPanel panel = new JPanel(new GridLayout());
        //panel.setBorder(BorderFactory.createTitledBorder("Description"));
        JTextArea movieDescription = new JTextArea(10, 20);
        panel.add(new JScrollPane(movieDescription));
        movieDescription.setLineWrap(true);
        movieDescription.setWrapStyleWord(true);
        movieDescription.setText(movieDescription.toString());
        return panel;
    }

    private void display() {
        JFrame f = new JFrame("Test");
        f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        f.setContentPane(createPanel());
        f.pack();
        f.setLocationRelativeTo(null);
        f.setVisible(true);
    }

    public static void main(String[] args) {
        EventQueue.invokeLater(new Test()::display);
    }
}

关于java - JTextArea 不显示,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38279321/

相关文章:

Java JAR 文件找不到类

java - GridLayout 没有填满整个窗口

java - 没有LayoutManager就无法使用JTree

java - 将 xbasej 与 NTX 索引结合使用

java - (菜鸟)输出语句的快速帮助

java - 如何将 SSL CA 获取到 Java 服务器

java - 使用 swingworker 动画递归三角剖分算法

java - JXTreeTable 模型不允许使用 insertNodeInto()

java - 桌面 UI 设计工具,最好是 Java 或 Java 包装器

java - JTextField 在 Horizo​​ntalBox 中变得巨大