[英]JPanel not showing when added to JFrame
我正在用Java創建GUI。 目前,我有一個空的JFrame,正在嘗試向其添加一個JPanel。 JPanel包含按鈕,文本等。但是,這些都沒有顯示。 我的代碼如下:
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class memoDisplayUI {
JFrame frame = new JFrame();
JPanel panel = new JPanel();
JTextArea jTextBox = new JTextArea();
JScrollPane scroll = new JScrollPane();
/**
* Launch the application.
*/
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
memoDisplayUI frame = new memoDisplayUI();
frame.frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
/**
* Create the application.
*/
public memoDisplayUI() {
initialize();
}
/**
* Initialize the contents of the frame.
*/
private void initialize() {
frame.getContentPane().setBackground(new Color(255, 255, 255));
frame.getContentPane().setLayout(null);
frame.setBounds(100, 100, 270, 400);
frame.setUndecorated(true); //REMOVES MENU BAR
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JLabel lblMemos = new JLabel("MEMOS");
lblMemos.setForeground(new Color(100, 149, 237));
lblMemos.setFont(new Font("Moire", Font.BOLD, 30));
lblMemos.setBounds(16, 16, 234, 37);
panel.add(lblMemos);
JButton button = new JButton("");
button.setBackground(new Color(100, 149, 237));
button.setBounds(7, 350, 40, 40);
panel.add(button);
button.setIcon(new ImageIcon("back.png"));
JButton button_1 = new JButton("");
button_1.setBackground(new Color(100, 149, 237));
button_1.setBounds(113, 350, 40, 40);
panel.add(button_1);
button_1.setIcon(new ImageIcon("Edit.png"));
JButton button_2 = new JButton("");
button_2.setBackground(new Color(100, 149, 237));
button_2.setBounds(220, 350, 40, 40);
panel.add(button_2);
button_2.setIcon(new ImageIcon("memo.png"));
JButton btnExit = new JButton("");
btnExit.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
System.exit(0);
}
});
btnExit.setBorder(null);
btnExit.setIcon(new ImageIcon("Exit.jpg"));
btnExit.setBounds(216, 19, 40, 40);
panel.add(btnExit);
jTextBox = new JTextArea();
scroll.setViewportView(jTextBox); // add scroll panel
jTextBox.setTabSize(4);
jTextBox.setLineWrap(true);
jTextBox.setBackground(new Color(192, 192, 192));
jTextBox.setBounds(8, 60, 255, 286);
panel.add(jTextBox);
frame.getContentPane().add(panel);
}
}
有人可以建議這是為什么嗎?
非常感謝 :)
從幾處調整到代碼,看來這是所需的布局(在不可調整大小的GUI中)。
我認為您使用null來獲得“將其放置在適當的位置”嗎? 然后使用FlowLayout
frame.setLayout(new FlowLayout());
那應該解決它:)
有人可以建議這是為什么嗎?
使用null
布局,而不調用pack()
。
在我注釋掉對setUndecorated(true)
的調用並將其拖動到更大一點之后,獲得了我在問題中編輯過的圖像作為GUI的屏幕截圖。 這樣做會使JRE驗證組件結構( pack()
會做什么),從而使組件出現。
正如我在評論中提到的:
..一個更好的問題是“如何布局此GUI?” (只要您提供嘗試)
這就是我的第一個評論。 (現在較長)
Java GUI可能必須在多種平台上,不同的屏幕分辨率和使用不同的PLAF上運行。 因此,它們不利於組件的精確放置。 要為健壯的GUI組織組件,請改用布局管理器或它們的組合 1 ,以及空白 2的布局填充和邊框。
所以回到:
(只要您提供嘗試)
查看這兩個示例以了解它們的工作原理,然后嘗試結合一些布局和填充以創建一個框架,然后將其打包以減小為自然尺寸。
並提示JTextArea
。 建議在列x行中結合Font
大小的大小。
只需從上面的代碼第46行刪除/注釋該行。
// frame.getContentPane().setLayout(null);
它應該工作正常。
更換
frame.getContentPane().setLayout(null);
與
frame.getContentPane().setLayout(new BorderLayout());
祝好運。
編輯:為了將來參考,要確定在您的情況下應該使用哪個LayoutManager
,您應該參考此LayoutManagers可視指南 。
1:永遠不要調用setLayout(null)。 2:嘗試使用frame.validate()使用布局來布局組件。
也許您應該替換:
frame.getContentPane().add(panel);
通過
frame.setContentPane(panel);
希望能有所幫助
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.