Home > Software Tutorial > Office Software > Create a program using the Java programming language to display a table

Create a program using the Java programming language to display a table

WBOY
Release: 2024-01-04 19:18:26
forward
834 people have browsed it

In order to give everyone a better understanding, we will use Java language to write a program to display a simple table. The following are the specific steps: 1. Import the necessary Java classes: ```java import javax.swing.*; import java.awt.*; ``` 2. Create a class inherited from JFrame to display the table: ```java public class TableFrame extends JFrame { public TableFrame() { //Set window title setTitle("Table representation

It is a very common requirement to write a program in Java to display a table, and in the Swing library, you can use the JTable component to achieve this function. The following is a simple sample code : ```java import javax.swing.*; import java.awt.*; public class TableExample extends JFrame { public TableExample() { setTitle("Table example"); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); String[] columnNames = {"Name", "Age", "Gender"}; Object[][] data = { {"Zhang San", 25, "Male"}, {"李思", 30, "Female"}, {"王五", 28, "male"} };

import javax.swing.*;
import java.awt.*;

public class TableExample {
    public static void main(String[] args) {
        // 创建 JFrame 实例
        JFrame frame = new JFrame("表格示例");

        // 创建表格的数据和列名
        Object[][] data = {
                {"John", 28, "Male"},
                {"Alice", 22, "Female"},
                {"Bob", 35, "Male"}
        };

        String[] columnNames = {"Name", "Age", "Gender"};

        // 创建表格
        JTable table = new JTable(data, columnNames);

        // 创建滚动窗格,并将表格添加到窗格中
        JScrollPane scrollPane = new JScrollPane(table);

        // 将滚动窗格添加到 JFrame 中
        frame.getContentPane().add(scrollPane, BorderLayout.CENTER);

        // 设置 JFrame 的大小、可见性和默认关闭操作
        frame.setSize(400, 300);
        frame.setVisible(true);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    }
}
Copy after login

This example creates a simple Swing application containing a table. Data and column names can be modified as needed.

2. How to implement the printing function in JAVA

To implement the printing function in Java, you can use the Java printing API. The following is a simple example:

import java.awt.print.*;

public class PrintExample implements Printable {

    public int print(Graphics g, PageFormat pf, int page) throws PrinterException {
        if (page > 0) {
            return NO_SUCH_PAGE;
        }

        // 在这里绘制要打印的内容
        Graphics2D g2d = (Graphics2D) g;
        g2d.drawString("Hello, this is a test!", 100, 100);

        return PAGE_EXISTS;
    }

    public static void main(String[] args) {
        PrinterJob job = PrinterJob.getPrinterJob();
        job.setPrintable(new PrintExample());

        // 弹出打印对话框
        if (job.printDialog()) {
            try {
                job.print();
            } catch (PrinterException e) {
                e.printStackTrace();
            }
        }
    }
}
Copy after login

In the above example, the PrintExample class implements the Printable interface, in which the print method is used Draw what you want to print. In the main method, a PrinterJob instance is created, and the print dialog box pops up through the printDialog method, and the user can select a printer and other settings. Then, call the print method to perform the printing operation.

Summary:

  1. 1. By using the JTable component in the Swing library, you can write a simple program to display a table in Java.
  2. 2. To implement the printing function, you can use the Java printing API, create a class that implements the Printable interface, and use PrinterJob to perform the printing operation.

The above is the detailed content of Create a program using the Java programming language to display a table. For more information, please follow other related articles on the PHP Chinese website!

source:docexcel.net
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template