我目前正在為我的學校專案制作一個程式,它是一個快餐訂單計算器,我的問題是我必須能夠在 Main 的 JFrame 中顯示表格,如果我添加“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);
}
}
uj5u.com熱心網友回復:
- 添加
frame.add(JScrollPane(table)); - 更改
frame.add(label);為frame.add(label, BorderLayout.NORTH); - 移動
frame.setVisible(true);到main方法的末尾 - 洗掉
frame.setSize(1000, 800);和frame.setResizable(false); frame.pack()之前添加frame.setVisible(true);

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情境,這只會讓生活更輕松
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/482338.html
