JTextArea 不显示

JTextArea not showing

为什么 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

    }

}

createAndShowGUI 方法中,您将再次创建一个 AddMovie 对象..

因此,如果您按以下方式修改尾声,它将起作用...

示例:

    public class AddMovie extends JTextField {

    private static final long serialVersionUID = 6180900736631578119L;
    private JFrame frame;
    private JLabel description;
    private JTextArea movieDescription;

    public JPanel createContentPane() {

    JPanel totalGUI = new JPanel();
    
    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(scrollPane);

    return totalGUI;
    }

    public void createAndShowGUI() {
    JFrame.setDefaultLookAndFeelDecorated(true);
    frame = new JFrame("New Movie");
    frame.setContentPane(createContentPane());
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.setSize(515, 520);
    frame.setLocation(480, 120);
    frame.setVisible(true);
    }

    public static void main(String[] args) {
    AddMovie am = new AddMovie();
    am.createAndShowGUI();
    }

}

按照建议 and , a null layout invites trouble. Because your display centers on the description, use the JTextArea constructor that lets you specify a size in rows and columns. When you pack() the enclosing Window, it will be resized to fit the text area. I've added some ad hoc text to illustrate the effect. I've also updated 调整框架大小时允许文本区域增大的代码。

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 
 */
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);
    }
}
totalGUI.setLayout(null);

您应该解决第一个问题:JavaGUI 必须在不同的 OS'、屏幕尺寸、屏幕分辨率等上工作,在不同的区域设置中使用不同的 PLAF。因此,它们不利于像素完美布局。而是使用布局管理器,或 combinations of them along with layout padding and borders for white space.

除了许多其他问题,它通常会破坏滚动窗格完成其设计目的的能力。

一般提示:

  1. 没有名为 cancel 的属性。
  2. 不要覆盖 setVisible(boolean) 这样的方法,然后将它们留空!这只不过是破坏了正常工作的功能而已!
  3. movieDescription 已添加到 totalGUI 和滚动窗格。它应该只添加到滚动窗格。
  4. 添加到白色 BG 的白色文本区域除了脱字符外是不可见的。
  5. Tahoma 字体仅在某些 OS'
  6. 中可用
  7. 除了使用 JLabel 描述之外,您还可以将文本区域添加到面板,并将描述文本设置为 TitledBorder,用于放置文本区域的面板英寸
  8. 声明 throws IOException 的两个方法中没有任何一个实际上会导致一个。