⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 9.17-textareademo.java

📁 介绍有关java的资料 课件 相当一本书籍 里面都是很基础的知识
💻 JAVA
字号:
// TextAreaDemo.java: Display an image in a label, the title for
// the image in a label, and the discription of the image in a
// text area
import java.awt.*;
import javax.swing.*;

public class TextAreaDemo extends JFrame
{
  // Declare and create a description panel
  private DescriptionPanel descriptionPanel = new DescriptionPanel();

  // Main method
  public static void main(String[] args)
  {
    TextAreaDemo frame = new TextAreaDemo();
    frame.pack();
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.setTitle("Text Area Demo");
    frame.setVisible(true);
  }

  // Constructor
  public TextAreaDemo()
  {
    // Set title, text and image in the description panel
    descriptionPanel.setTitle("中国");
    String description = "国旗 \n\n" +
      " 中华人民共和国建立于1949年\n " +  "人民言论自由\n" + "人民富裕 " ;
      
    descriptionPanel.setTextDescription(description);
    descriptionPanel.setImageIcon(new ImageIcon("images/ca.gif"));

    // Add the description panel to the frame
    getContentPane().setLayout(new BorderLayout());
    getContentPane().add(descriptionPanel, BorderLayout.CENTER);
  }
}

// Define a panel for displaying image and text
class DescriptionPanel extends JPanel
{
  // Label for displaying an image icon
  private JLabel jlblImage = new JLabel();
  // Label for displaying a title
  private JLabel jlblTitle = new JLabel();
  // Text area for displaying text
  private JTextArea jtaTextDescription;

  // Default constructor
  public DescriptionPanel()
  {
    // Group image label and title label in a panel
    JPanel panel = new JPanel();
    panel.setLayout(new BorderLayout());
    panel.add(jlblImage, BorderLayout.CENTER);
    panel.add(jlblTitle, BorderLayout.SOUTH);

    // Create a scroll pane to hold text area
    JScrollPane scrollPane = new JScrollPane
      (jtaTextDescription = new JTextArea());

    // Center the title on the label
    jlblTitle.setHorizontalAlignment(JLabel.CENTER);

    // Set the font for the title and text
    jlblTitle.setFont(new Font("SansSerif", Font.BOLD, 16));
    jtaTextDescription.setFont(new Font("Serif", Font.PLAIN, 14));

    // Set lineWrap and wrapStyleWord true for text area
    jtaTextDescription.setLineWrap(true);
    jtaTextDescription.setWrapStyleWord(true);

    // Set preferred size for the  class class cscroll pane
    scrollPane.setPreferredSize(new Dimension(200, 100));

    // Set BorderLayout for the whole panel, add panel and scrollpane
    setLayout(new BorderLayout());
    add(scrollPane, BorderLayout.CENTER);
    add(panel, BorderLayout.WEST);
  }

  // Set the title
  public void setTitle(String title)
  {
    jlblTitle.setText(title);
  }

  // Set the image icon
  public void setImageIcon(ImageIcon icon)
  {
    jlblImage.setIcon(icon);
    Dimension dimension = new Dimension(icon.getIconWidth(),
      icon.getIconHeight());
    jlblImage.setPreferredSize(dimension);
  }

  // Set the text description
  public void setTextDescription(String text)
  {
    jtaTextDescription.setText(text);
  }
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -