當我運行Java程序時,我的文本區域和按鈕不會添加到要顯示的面板。我運行程序時看到的所有內容都是沒有任何文本區域和按鈕的面板。我如何讓文字區域和按鈕顯示出來?JTextArea和JButton不會添加到JPanel
NumberFormat nf = NumberFormat.getCurrencyInstance(Locale.US);
JFrame frame = new JFrame("Bookstore");
frame.setSize(500, 300);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JPanel panel = new JPanel();
panel.setLayout(new GridBagLayout());
frame.setVisible(true);
JTextArea textArea = new JTextArea();
textArea.append("\n" + bk[0]);
panel.add(textArea);
JButton btnFirst = new JButton("First");
GridBagConstraints g = new GridBagConstraints();
g.insets = new Insets(5, 5, 5, 5);
g.gridx = 0;
g.gridy = 2;
panel.add(btnFirst, g);
JButton btnLast = new JButton("Last");
g.gridx = 1;
g.gridy = 2;
panel.add(btnLast, g);
JButton btnNext = new JButton("Next");
g.gridx = 3;
g.gridy = 2;
panel.add(btnNext, g);
JButton btnPrevious = new JButton("Previous");
g.gridx = 0;
g.gridy = 3;
panel.add(btnPrevious, g);
JTextArea totalInventory = new JTextArea("Total Inventory\t\t\t");
totalInventory.append(nf.format(total));
g.gridx = 0;
g.gridy = 6;
panel.add(totalInventory, g);
考慮提供一個[可運行後,示例說明你的問題](https://stackoverflow.com/help/mcve),這將涉及更少的猜測工作併產生更好的響應 – MadProgrammer
雖然這裏沒有猜測, MadProgrammer的回答指出了這兩個非常明顯的問題。 –