简体   繁体   English

JTextArea 不显示

[英]JTextArea not showing

Why JTextArea not showing in GUI ?为什么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

    }

}

As suggested here and here , a null layout invites trouble.正如这里这里所建议的, null布局会带来麻烦。 Because your display centers on the description, use the JTextArea constructor that lets you specify a size in rows and columns.因为您的显示以描述为中心,所以使用JTextArea构造函数,它允许您指定行和列的大小。 When you pack() the enclosing Window , it will be resized to fit the text area.当您pack()封闭的Window时,它将调整大小以适合文本区域。 I've added some ad hoc text to illustrate the effect.我添加了一些临时文本来说明效果。 I've also updated the code to allow the text area to grow when resizing the frame.我还更新了代码以允许文本区域在调整框架大小时增长。

图片

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

There's the 1st problem you should solve: Java GUIs have to work on different OS', screen size, screen resolution etc. using different PLAFs in different locales.您应该解决第一个问题:Java GUI 必须在不同的操作系统、屏幕尺寸、屏幕分辨率等上使用不同的语言环境中的不同 PLAF。 As such, they are not conducive to pixel perfect layout.因此,它们不利于像素完美布局。 Instead use layout managers, or combinations of them along with layout padding and borders for white space .而是使用布局管理器,或它们的组合以及布局填充和空白边框。

Besides many other problems, it typically destroys the ability of scroll-panes to do the job they were designed for.除了许多其他问题之外,它通常会破坏滚动窗格完成其设计工作的能力。

General tips:一般提示:

  1. There is no attribute named cancel .没有名为cancel的属性。
  2. Don't override methods like setVisible(boolean) then leave them empty!不要覆盖setVisible(boolean)之类的方法,然后将它们留空! That is achieving nothing more than breaking functionality that works just fine as it is!这只不过是破坏了可以正常工作的功能!
  3. The movieDescription is added to both the totalGUI & the scroll pane. movieDescription被添加到totalGUI和滚动窗格中。 It should only be added to the scroll pane.它应该只添加到滚动窗格中。
  4. A white text area added to a white BG will be invisible except for the caret.添加到白色 BG 的白色文本区域将不可见,除了插入符号。
  5. The Tahoma font will only be available in some OS' Tahoma字体仅在某些操作系统中可用
  6. Rather than use a JLabel for the description, you might also add the text area to a panel and set the Description text to a TitledBorder used on a panel that the text area is placed in.除了使用JLabel进行描述外,您还可以将文本区域添加到面板并将描述文本设置为在放置文本区域的面板上使用的TitledBorder
  7. Nothing in the two wethods that declare throws IOException could actually cause one.声明throws IOException的两个方法中没有任何东西实际上会导致一个。

In the createAndShowGUI method you are creating again a AddMovie object..createAndShowGUI方法中,您将再次创建一个AddMovie对象。

so if you modifiy the coda as following, it will work...因此,如果您按以下方式修改尾声,它将起作用...

Example:例子:

    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();
    }

}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM