java - 为什么即使添加到 JScrollPane 中,JTable 也不显示 Jtable 标题?

标签 java swing jtable transparency paintcomponent

import javax.swing.*; 
import javax.swing.border.*;
import javax.swing.table.TableColumn;

import java.awt.*;

import static java.awt.GraphicsDevice.WindowTranslucency.*;

import java.awt.Checkbox;
import java.awt.Paint;
import java.awt.Toolkit;
import java.awt.event.*;

public class Main extends JPanel {

    static Object [][] Services = {{"Google.exe","Chickeaen.exe","Crp.exe"}};
    static String [] ColNames = {"Processes:","Crolly:","Haler:"};

    static JFrame Fram = new JFrame(); 
    static JTextField CBox = new JTextField();
    static JTable Tabs = new JTable(Services,ColNames);
    JScrollPane ScrollArea = new JScrollPane();
    static JButton ExitB = new JButton();
    Dimension ScreenSize = new Dimension(Toolkit.getDefaultToolkit().getScreenSize());
    Border BlackLineB = BorderFactory.createLineBorder(new Color(50,50,50));

    public Main() {

    Fram.setTitle("Jared Console");
    Fram.setUndecorated(true);
    Fram.setVisible(true); 
    Fram.setDefaultCloseOperation(Fram.EXIT_ON_CLOSE);
    Fram.setResizable(false);
    Fram.setSize((int)Math.round(ScreenSize.getWidth()*0.45),(int)Math.round(ScreenSize.getHeight()*0.33));
    Fram.setBackground(new Color(0,0,0,150));
    Fram.add(this);

    CBox.setSize((int)Math.round(Fram.getWidth()*0.80),(int)Math.round(Fram.getHeight()*0.25));
    CBox.setBackground(new Color(255,255,255));
    CBox.setBorder(BorderFactory.createCompoundBorder(BlackLineB,BorderFactory.createEmptyBorder(0,20,0,0)));
    CBox.setLocation((int)Math.round(Fram.getWidth()*0.1),(int)Math.round(Fram.getHeight()*0.70));
    CBox.setFont(new Font("Arial",Font.BOLD,20));
    CBox.setVisible(true);

    ScrollArea.setSize((int)Math.round(Fram.getWidth()*0.80),(int)Math.round(Fram.getHeight()*0.50)); 
    ScrollArea.setLocation((int)Math.round(Fram.getWidth()*0.10),(int)Math.round(Fram.getHeight()*0.10));
    ScrollArea.setBorder(BlackLineB);
    ScrollArea.setLayout(null);
    ScrollArea.setVisible(true);

    Tabs.setSize((int)Math.round(Fram.getWidth()*0.995),(int)Math.round(Fram.getHeight()*0.995));
    Tabs.setLocation((int)Math.round(Fram.getWidth()*0.003),(int)Math.round(Fram.getHeight()*0.005));
    Tabs.setFillsViewportHeight(true);
    Tabs.setBackground(new Color(255,255,255));

    this.add(CBox);
    this.add(Tabs);
    this.add(ExitB);
    ScrollArea.add(Tabs);
    this.add(ScrollArea);
    this.setBorder(BorderFactory.createLineBorder(new Color(50,50,50),5));
    this.setLayout(null);
    this.setBackground(new Color(0,0,0));
    this.setVisible(true);

    }

    public void paintComponent(Graphics Gla) { 
    Paint Plat = new GradientPaint(0f, 0f, new Color(0, 40, 0, 0), 0.0f, Fram.getHeight(), new Color(0, 0, 0, 150), true); //Made 200 equal to Fram Background Alpha.
    Graphics2D Quo = (Graphics2D)Gla;
    Quo.setPaint(Plat);
    Quo.fillRect(0, 0, Fram.getWidth(), Fram.getHeight());
    }

    public static void main(String[] args) {
    Main CScreen = new Main(); 
    GraphicsEnvironment GE = GraphicsEnvironment.getLocalGraphicsEnvironment(); // Have to study lines 57,58 and 59
    GraphicsDevice GD = GE.getDefaultScreenDevice();
    boolean CheckTransL = GD.isWindowTranslucencySupported(PERPIXEL_TRANSLUCENT);

    if (!CheckTransL) {
        System.out.println("PERPIXEL TRANSLUCENT NOT SUPPORTED - LOL UPDATESCRUB!");
        System.exit(0);
    };  
    }

}

为什么即使添加到 JScrollPane 中,JTable 也不显示 Jtable 标题? 另外,控制台首先显示一条错误消息,然后很快消失并启动程序?所以是的,我想知道这有什么问题,你也可以告诉我这个程序中的一些坏习惯,比如它的输入方式。

最佳答案

问题

  • null 布局。避免使用 null 布局,像素完美布局是现代 UI 设计中的一种幻觉。影响组件个体尺寸的因素太多,您无法控制其中任何一个。 Swing 的设计初衷是与布局管理器一起工作,放弃这些管理器将导致无穷无尽的问题,您将花费越来越多的时间来尝试纠正这些问题。请参阅Laying Out Components Within a Container了解更多详情
  • 过度使用静态static 不是你的 friend ,你应该避免使用它。它不是跨对象通信机制,像这样过度使用会烧伤你
  • 您实际上并未将 JTable 包装在 JScrollPane
  • 通过不调用 super.paintComponent 来打破 paint 链,这将产生一系列精彩的绘画工件。请参阅Painting in AWT and SwingPerforming Custom Painting有关 Swing 中绘画工作原理的更多详细信息
  • 您可能想通读 Code Conventions for the Java TM Programming Language ,这将使人们更容易阅读您的代码,也让您更轻松地阅读其他人
  • 对 UI 的所有交互、创建和修改都应在事件调度事件的上下文中完成,以降低潜在竞争条件、死锁和渲染工件的风险。请参阅Initial Threads了解更多详情
  • Toolkit.getDefaultToolkit().getScreenSize() 是屏幕可视空间的一个不好的指示器,它没有考虑操作系统特定的元素,例如停靠栏或任务栏,这可能会导致让你的申请出现在它们下面(当这种情况发生时,我真的,真的,真的很讨厌它)。使用适当的布局管理器和 pack 将窗口打包到内容周围。然后,您可以使用 JFrame#setLocationRelativeTo 并将其传递给 null,它将使框架在屏幕中居中

JScrollPane 问题...

简单的解决方案是使用 JScrollPane 的构造函数向其传递 JTable 的引用...

static JTable Tabs = new JTable(Services,ColNames);
JScrollPane ScrollArea = new JScrollPane(Tabs);

但是,然后您稍后在代码中执行此操作...

this.add(Tabs);

这将从滚动 Pane 中删除表格以将其添加到面板中,因为组件只能有一个父组件。

另一个选项是指定滚动 Pane 的视口(viewport)的 View 组件...

this.add(CBox);
//this.add(Tabs);
this.add(ExitB);
//ScrollArea.add(Tabs);
ScrollArea.setViewportView(Tabs);
this.add(ScrollArea);

您永远不应该将组件直接添加到滚动 Pane (或其底层视口(viewport)),它们有自己的内部布局管理功能。相反,您需要将组件作为“ View ”提供给 JViewport

看看How to Use Scroll Panes了解更多详情。

关于java - 为什么即使添加到 JScrollPane 中,JTable 也不显示 Jtable 标题?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31260649/

相关文章:

java - DeltaTime 未正确更新?

java - 将 SQLite 日期整数转换为 Java 日期

java - 在Java中下载同名附件而不覆盖

java - 处理 HttpServletResponse 时运行 JerseyTest 的问题

java - 当在 JTree Location 之外发生 Focus Lost 或 Left Click 时,如何使 JTree 停止单元格编辑?

java - 在 JTable 中添加行

java - 如何获取 JScrollPane 中 JTable 模型的所有可见行

java - 更改按钮大小

swing - JTable、自定义标题渲染器和排序图标

java - 在 jtable 中创建组合框