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

📄 displaymessageapp.java

📁 此源码为机械工业出版社出版的《Java语言程序设计》第三版所配套的书中所有源代码。
💻 JAVA
字号:
// DisplayMessageApp.java:
// The program can run as an applet or application
import javax.swing.*;
import java.awt.BorderLayout;
import java.awt.Font;

public class DisplayMessageApp extends JApplet
{
  private String message = "A default message"; // Message to display
  private int x = 20; // Default x coordinate
  private int y = 20; // Default y coordinate

  // Determine if it is application
  private boolean isStandalone = false;

  // Initialize the applet
  public void init()
  {
    if (!isStandalone)
    {
      // Get parameter values from the HTML file
      message = getParameter("MESSAGE");
      x = Integer.parseInt(getParameter("X"));
      y = Integer.parseInt(getParameter("Y"));
    }

    // Create a message panel
    MessagePanel messagePanel = new MessagePanel(message);
    messagePanel.setFont(new Font("SansSerif", Font.BOLD, 20));
    messagePanel.setXCoordinate(x);
    messagePanel.setYCoordinate(y);

    // Add the message panel to the applet
    getContentPane().add(messagePanel);
  }

  // Main method with three arguments:
  // args[0]: x coordinate
  // args[1]: y coordinate
  // args[2]: message
  public static void main(String[] args)
  {
    // Create a frame
    JFrame frame = new JFrame("DisplayMessageApp");

    // Create an instance of the applet
    DisplayMessageApp applet = new DisplayMessageApp();

    // It runs as an application
    applet.isStandalone = true;

    // Get parameters from the command line
    applet.getCommandLineParameters(args);

    // Add the applet instance to the frame
    frame.getContentPane().add(applet, BorderLayout.CENTER);

    // Invoke init() and start()
    applet.init();
    applet.start();

    // Display the frame
    frame.setSize(300, 300);
    // frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.setVisible(true);
  }

  // Get command line parameters
  public void getCommandLineParameters(String[] args)
  {
    // Check usage and get x, y and message
    if (args.length != 3)
    {
      System.out.println(
        "Usage: java DisplayMessageApp x y message");
      System.exit(0);
    }
    else
    {
      x = Integer.parseInt(args[0]);
      y = Integer.parseInt(args[1]);
      message = args[2];
    }
  }
}

⌨️ 快捷键说明

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