java - 无法同时显示我的 JFrame 和表格
问题描述
因此,我在 intellij 上创建了一个 gui 格式,当我取出与表格有关的代码时,它完全符合我的要求,但是当我将代码放入时,我得到的只是表格本身。我在使用 gui 表单本身添加列时遇到问题,并找到了一个关于如何使用代码添加列等的教程。我如何能够在表格中添加行和/或在表格中添加表格
public class App extends JFrame{
private JButton createRecordButton;
private JPanel panel1;
private JPanel east;
private JPanel west;
private JPanel south;
private JButton deleteRecordButton;
private JButton viewStatisticsButton;
private JButton downloadDataButton;
public JTable TestDB;
public App() {
}
public static void main(String[] args) {
JFrame frame=new JFrame("App");
frame.setContentPane(new App().panel1);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.pack();
frame.setVisible(true);
table gui=new table();
gui.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
gui.setSize(600,200);
gui.setVisible(true);
gui.setTitle("The DataBase Table");
}
}
解决方案
您正在在线生成异常: frame.setContentPane(new App().panel1); 请注意,您创建了 App 对象,但 panel1 对象仍然为空。使用此代码,您将看到两个 JFrame 都会出现:
public class App extends JFrame{
private JButton createRecordButton;
private JPanel panel1;
private JPanel east;
private JPanel west;
private JPanel south;
private JButton deleteRecordButton;
private JButton viewStatisticsButton;
private JButton downloadDataButton;
public JTable TestDB;
public App() {
panel1 = new JPanel();
}
public static void main(String[] args) {
JFrame frame=new JFrame("App");
frame.setContentPane(new App().panel1);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.pack();
frame.setVisible(true);
JFrame gui=new JFrame();
gui.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
gui.setSize(600,200);
gui.setVisible(true);
gui.setTitle("The DataBase Table");
}
}
推荐阅读
- python - 使用 Python、NLTK 分析德语文本
- amazon-web-services - Schema graphql 错误给出“不能为不可为空的字段 Organisation.id 返回 null”为什么?
- sql-server - SQL Server:在没有 BULK 加载语句的情况下加载 CSV
- c# - 使用循环在 Array 中搜索对象
- javascript - 如何使用jspdf生成具有正确值的表
- vue.js - ECharts - 如何从道具中注入价值?
- ios - 在 resigned ipa 中注入 ObjectiveC-Swift 钩子,主要在 Swift 中而不是在 Objective C 中编写钩子
- html - 根据滚动位置触发不同的 CSS 关键帧动画(我应该做一个动画类吗?)
- html - 如何使 HTML 字段集更紧密?
- python - 当主窗口最小化时,为什么顶层窗口会被破坏?