zoukankan      html  css  js  c++  java
  • 在java中,用window builder 绘制的图形界面中,表格如何出现在图形界面上

     1 package Test;
     2 import java.awt.EventQueue;
     3 import java.awt.event.WindowAdapter;
     4 import java.awt.event.WindowEvent;
     5 import javax.swing.JFrame;
     6 import javax.swing.JScrollPane;
     7 import javax.swing.JTable;
     8 import javax.swing.JButton;
     9 import java.awt.event.ActionListener;
    10 import java.awt.event.ActionEvent;
    11 public class Demo {
    12     private JFrame frame;
    13     private JTable table;
    14     private JScrollPane scrollPane;
    15     public static void main(String[] args) {
    16         EventQueue.invokeLater(new Runnable() {
    17             public void run() {
    18                 try {
    19                     Demo window = new Demo();
    20                     window.frame.setVisible(true);
    21                 } catch (Exception e) {
    22                     e.printStackTrace();
    23                 }
    24             }
    25         });
    26     }
    27     public Demo() {
    28         initialize();
    29     }
    30     private void initialize() {
    31         frame = new JFrame();
    32         frame.setBounds(100, 100, 582, 437);
    33         frame.getContentPane().setLayout(null);
    34         scrollPane = new JScrollPane();
    35         scrollPane.setBounds(14, 13, 536, 337);
    36         frame.getContentPane().add(scrollPane);
    37         JButton button = new JButton("u67E5u627E"); // 查找
    38         button.addActionListener(new ActionListener() {
    39             public void actionPerformed(ActionEvent e) {
    40                 Demo1 s = new Demo1();
    41                 try {
    42                     if (table != null) {// 先判断表模型是否为空,如果不是则先清除表
    43                         JFrame f = new JFrame();
    44                         f.remove(table);
    45                     }
    46                     table = s.search();// 表赋值
    47                     table.setAutoCreateRowSorter(true);
    48                     scrollPane.setViewportView(table);// 添加滚动条
    49                 } catch (Exception ex) {
    50                 }
    51             }
    52         });
    53         button.setBounds(260, 366, 63, 23);
    54         frame.getContentPane().add(button);
    55         frame.addWindowListener(new WindowAdapter() {
    56             @Override
    57             public void windowClosed(WindowEvent e) {
    58                 frame.dispose();
    59             }
    60         });
    61         frame.setVisible(true);
    62     }
    63 }

     1 package Test;
     2 
     3 import java.awt.Color;
     4 import javax.swing.*;
     5 
     6 class Demo1 {
     7     private String[] columnNames = { "Planet", "Radius", "Moons", "Gaseous", "Color" };
     8     private Object[][] cells = { { "Mercury", 2440.0, 0, false, Color.YELLOW },
     9             { "Venus", 6052.0, 0, false, Color.YELLOW }, { "Earth", 6378.0, 1, false, Color.BLUE },
    10             { "Mars", 3397.0, 2, false, Color.RED }, { "Jupiter", 71492.0, 16, true, Color.ORANGE },
    11             { "Saturn", 60268.0, 18, true, Color.ORANGE }, { "Uranus", 25559.0, 17, true, Color.BLUE },
    12             { "Neptune", 24766.0, 8, true, Color.BLUE }, { "Pluto", 1137.0, 1, false, Color.BLACK } };
    13 
    14     public JTable search() {
    15         JTable JTable = new JTable(cells, columnNames);// 创建表格
    16         return JTable;
    17     }
    18 }

        在Demo.java文件中,图形界面由window builder绘制,并且调用Demo1.java中的方法!

    在Demo1.java中借鉴了"java核心技术" 卷二 第九版 介绍表格部分的部分代码!

  • 相关阅读:
    Pytorch手写线性回归
    numpy+sklearn 手动实现逻辑回归【Python】
    如何用TensorFlow实现线性回归
    进程、线程和携程的通俗解释【刘新宇Python】
    即时通信WebSocket 和Socket.IO
    gRPC【RPC自定义http2.0协议传输】
    Django中MySQL事务的使用
    模拟电磁曲射炮_H题 方案分析【2019年电赛】【刘新宇qq522414928】
    Gitflow工作流
    雪花算法【分布式ID问题】【刘新宇】
  • 原文地址:https://www.cnblogs.com/jinjupeng/p/6241219.html
Copyright © 2011-2022 走看看