[英]Why does the JTable not show the Jtable heading even when added to JScrollPane?
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);
};
}
}
为什么 JTable 即使添加到 JScrollPane 也不显示 Jtable 标题? 控制台也首先显示错误消息然后迅速消失并启动程序? 所以是的,我想知道这有什么问题,你能否指出我在这个程序中的一些坏习惯,比如它的输入方式。
null
布局。 避免使用null
布局,像素完美布局是现代 ui 设计中的一种错觉。 影响组件大小的因素太多,您无法控制。 Swing 旨在与核心布局管理器一起工作,丢弃这些将导致无休止的问题和问题,您将花费越来越多的时间试图纠正这些问题。 有关更多详细信息,请参阅在容器内布置组件static
。 static
不是你的朋友,你应该避免使用它。 它不是跨对象通信机制,像这样过度使用会烧伤你JTable
包装在JScrollPane
super.paintComponent
打破paint
链,这将产生一系列美妙的油漆工件。 有关在 Swing 中绘画如何工作的更多详细信息,请参阅在 AWT 和 Swing 中绘画和执行自定义绘画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);
这将从滚动窗格中删除表格以将其添加到您的面板中,因为组件只能有一个父级。
另一种选择是指定滚动窗格的视口的视图组件...
this.add(CBox);
//this.add(Tabs);
this.add(ExitB);
//ScrollArea.add(Tabs);
ScrollArea.setViewportView(Tabs);
this.add(ScrollArea);
永远不要将组件直接添加到滚动窗格(或它的底层视口),它们有自己的内部布局管理功能。 相反,您需要将组件作为“视图”提供给JViewport
有关更多详细信息,请查看如何使用滚动窗格。
首先,您应该将JTable
添加到JScrollPane
的ViewPort
,以便JTableHeader
可见。
之后,你应该不是你添加JTable
到两个JScrollPane
,也是潜在的容器。 你应该:
JTable
添加到JScrollPane
的ViewPort
。JScrollPane
添加到底层容器。 并删除将JTable
显式添加到容器的行。
祝你好运。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.