如何使 table 显示在 Main 的 JFrame 中?
How do I make it so that the table is displayed inside the JFrame in Main?
我目前正在为我的学校项目制作一个程序,它是一个快餐订单计算器,我的问题是我必须能够在 Main 的 JFrame 中显示 table,如果我添加“frame.add(table);”在 Main 内部,什么也没有发生。任何帮助将不胜感激,谢谢!
JTable table;
JTable FastFoodCalculatorProgramming;
public FastFoodCalculatorProgramming() {
setLayout(new FlowLayout());
String[] columnNames = {"Product", "Description", "Item Price"};
Object[][] data = {
{"Product1", "Description1", "Price1"},
{"Product2", "Description2", "Price2"},
{"Product3", "Description3", "Price3"},
{"Product4", "Description4", "Price4"},
{"Product5", "Description5", "Price5"},
{"Product6", "Description6", "Price6"},
{"Product7", "Description7", "Price7"},
{"Product8", "Description8", "Price8"},};
table = new JTable(data, columnNames) {
public boolean isCellEditable(int data, int columnNames) {
return false;
}
};
table.setPreferredScrollableViewportSize(new Dimension(500, 250));
table.setFillsViewportHeight(true);
JScrollPane scrollPane = new JScrollPane(table);
add(scrollPane);
}
public static void main(String[] args) {
FastFoodCalculatorProgramming table = new FastFoodCalculatorProgramming();
table.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
table.setSize(600, 200);
table.setVisible(true);
table.setTitle("Fast Food Order Calculator");
JLabel label = new JLabel();
label.setText("Welcome to Fast Food Calculator!");
label.setFont(new Font("Century Gothic", Font.BOLD, 30));
label.setVerticalAlignment(JLabel.TOP);
label.setHorizontalAlignment(JLabel.CENTER);
label.setBorder(new EmptyBorder(20, 0, 0, 0));
JFrame frame = new JFrame();
frame.setVisible(true);
frame.setSize(1000, 800);
frame.setResizable(false);
frame.setTitle("Fast Food Calculator");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.getContentPane().setBackground(Color.gray);
frame.add(label);
}
}
- 添加
frame.add(JScrollPane(table));
- 将
frame.add(label);
更改为frame.add(label, BorderLayout.NORTH);
- 将
frame.setVisible(true);
移动到main
方法的末尾
- 删除
frame.setSize(1000, 800);
和 frame.setResizable(false);
- 在
frame.setVisible(true);
前加frame.pack()
import java.awt.BorderLayout;
import java.awt.EventQueue;
import java.awt.Font;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.border.EmptyBorder;
import javax.swing.table.DefaultTableModel;
import javax.swing.table.TableModel;
public class Main {
public static void main(String[] args) {
new Main();
}
private JTable table;
private TableModel tableModel;
private JLabel titleLabel;
public Main() {
EventQueue.invokeLater(new Runnable() {
@Override
public void run() {
JFrame frame = new JFrame();
frame.add(getTitleLabel(), BorderLayout.NORTH);
frame.add(new JScrollPane(getTable()));
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
});
}
protected JLabel getTitleLabel() {
if (titleLabel != null) {
return titleLabel;
}
titleLabel = new JLabel();
titleLabel.setText("Welcome to Fast Food Calculator!");
titleLabel.setFont(new Font("Century Gothic", Font.BOLD, 30));
titleLabel.setVerticalAlignment(JLabel.TOP);
titleLabel.setHorizontalAlignment(JLabel.CENTER);
titleLabel.setBorder(new EmptyBorder(20, 0, 0, 0));
return titleLabel;
}
protected JTable getTable() {
if (table != null) {
return table;
}
table = new JTable(getTableModel());
return table;
}
protected TableModel getTableModel() {
if (tableModel != null) {
return tableModel;
}
String[] columnNames = {"Product", "Description", "Item Price"};
Object[][] data = {
{"Product1", "Description1", "Price1"},
{"Product2", "Description2", "Price2"},
{"Product3", "Description3", "Price3"},
{"Product4", "Description4", "Price4"},
{"Product5", "Description5", "Price5"},
{"Product6", "Description6", "Price6"},
{"Product7", "Description7", "Price7"},
{"Product8", "Description8", "Price8"}
};
tableModel = new DefaultTableModel(data, columnNames);
return tableModel;
}
}
就个人而言,我总是尽快摆脱 static
环境,这只会让生活更轻松
我目前正在为我的学校项目制作一个程序,它是一个快餐订单计算器,我的问题是我必须能够在 Main 的 JFrame 中显示 table,如果我添加“frame.add(table);”在 Main 内部,什么也没有发生。任何帮助将不胜感激,谢谢!
JTable table;
JTable FastFoodCalculatorProgramming;
public FastFoodCalculatorProgramming() {
setLayout(new FlowLayout());
String[] columnNames = {"Product", "Description", "Item Price"};
Object[][] data = {
{"Product1", "Description1", "Price1"},
{"Product2", "Description2", "Price2"},
{"Product3", "Description3", "Price3"},
{"Product4", "Description4", "Price4"},
{"Product5", "Description5", "Price5"},
{"Product6", "Description6", "Price6"},
{"Product7", "Description7", "Price7"},
{"Product8", "Description8", "Price8"},};
table = new JTable(data, columnNames) {
public boolean isCellEditable(int data, int columnNames) {
return false;
}
};
table.setPreferredScrollableViewportSize(new Dimension(500, 250));
table.setFillsViewportHeight(true);
JScrollPane scrollPane = new JScrollPane(table);
add(scrollPane);
}
public static void main(String[] args) {
FastFoodCalculatorProgramming table = new FastFoodCalculatorProgramming();
table.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
table.setSize(600, 200);
table.setVisible(true);
table.setTitle("Fast Food Order Calculator");
JLabel label = new JLabel();
label.setText("Welcome to Fast Food Calculator!");
label.setFont(new Font("Century Gothic", Font.BOLD, 30));
label.setVerticalAlignment(JLabel.TOP);
label.setHorizontalAlignment(JLabel.CENTER);
label.setBorder(new EmptyBorder(20, 0, 0, 0));
JFrame frame = new JFrame();
frame.setVisible(true);
frame.setSize(1000, 800);
frame.setResizable(false);
frame.setTitle("Fast Food Calculator");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.getContentPane().setBackground(Color.gray);
frame.add(label);
}
}
- 添加
frame.add(JScrollPane(table));
- 将
frame.add(label);
更改为frame.add(label, BorderLayout.NORTH);
- 将
frame.setVisible(true);
移动到main
方法的末尾 - 删除
frame.setSize(1000, 800);
和frame.setResizable(false);
- 在
frame.setVisible(true);
前加
frame.pack()
import java.awt.BorderLayout;
import java.awt.EventQueue;
import java.awt.Font;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.border.EmptyBorder;
import javax.swing.table.DefaultTableModel;
import javax.swing.table.TableModel;
public class Main {
public static void main(String[] args) {
new Main();
}
private JTable table;
private TableModel tableModel;
private JLabel titleLabel;
public Main() {
EventQueue.invokeLater(new Runnable() {
@Override
public void run() {
JFrame frame = new JFrame();
frame.add(getTitleLabel(), BorderLayout.NORTH);
frame.add(new JScrollPane(getTable()));
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
});
}
protected JLabel getTitleLabel() {
if (titleLabel != null) {
return titleLabel;
}
titleLabel = new JLabel();
titleLabel.setText("Welcome to Fast Food Calculator!");
titleLabel.setFont(new Font("Century Gothic", Font.BOLD, 30));
titleLabel.setVerticalAlignment(JLabel.TOP);
titleLabel.setHorizontalAlignment(JLabel.CENTER);
titleLabel.setBorder(new EmptyBorder(20, 0, 0, 0));
return titleLabel;
}
protected JTable getTable() {
if (table != null) {
return table;
}
table = new JTable(getTableModel());
return table;
}
protected TableModel getTableModel() {
if (tableModel != null) {
return tableModel;
}
String[] columnNames = {"Product", "Description", "Item Price"};
Object[][] data = {
{"Product1", "Description1", "Price1"},
{"Product2", "Description2", "Price2"},
{"Product3", "Description3", "Price3"},
{"Product4", "Description4", "Price4"},
{"Product5", "Description5", "Price5"},
{"Product6", "Description6", "Price6"},
{"Product7", "Description7", "Price7"},
{"Product8", "Description8", "Price8"}
};
tableModel = new DefaultTableModel(data, columnNames);
return tableModel;
}
}
就个人而言,我总是尽快摆脱 static
环境,这只会让生活更轻松