[英]JPanel is not displaying in my JFrame
所以我在玩 JPanels 和 JFrames,我注意到當我將它添加到 Jframe 對象時,我創建的 JPanel 沒有顯示。 請注意,當我在我的 Jframe 構造函數中創建一個 JPanel 時,在添加到 Jframe 之前給出 jpanel 參數,它起作用了。 但是現在我正在使用我創建的 JPanel 對象,它不再工作了。 這就是我所做的。
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.GridLayout;
import java.awt.Point;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import javax.swing.JFrame;
import javax.swing.JPanel;
public class MyGui extends JFrame {
MyMouseListener listen = new MyMouseListener();
public MyGui() {
setSize(500, 500);
//setLayout(new BoxLayout(this, BoxLayout.X_AXIS));
getContentPane().setBackground(Color.WHITE);
addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {
System.exit(0);
}
});
Panel panel = new Panel();
add(panel, BorderLayout.WEST);
//setVisible(true);
show();
}
public static void main(String[] a) {
MyGui gui = new MyGui();
}
}
class Panel extends JPanel {
MyMouseListener listen = new MyMouseListener();
public Panel() {
setPreferredSize(new Dimension(300, 300));
addMouseListener(listen);
setBackground(Color.YELLOW);
setLayout(new GridLayout(3, 1));
}
public void paint(Graphics g) {
super.paintComponents(g);
g.drawOval((int) Math.round(listen.p.getX()),
(int) Math.round(listen.p.getX()), 1, 1);
}
}
class MyMouseListener implements MouseListener {
Point p = new Point();
@Override
public void mouseClicked(MouseEvent e) {
System.out.println("Mouse was clicked");
}
@Override
public void mousePressed(MouseEvent e) {
p = e.getPoint();
System.out.println(p);
}
@Override
public void mouseReleased(MouseEvent e) {
}
@Override
public void mouseEntered(MouseEvent e) {
}
@Override
public void mouseExited(MouseEvent e) {
}
}
編輯:其實我想我已經找到了錯誤。 JPanel 有它的繪制方法,刪除時允許 Jframe 顯示面板。 但是我需要能夠在 JPanel 上繪制東西。
它的
super.paintComponent(g);
建議:
1)你讓事情變得不必要地復雜。 例如關閉你應該使用的窗口
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
而不是調用System.exit(0);
並使用窗口偵聽器
2)正如@mKorbel 所說,您應該使用SwingUtilities.invokeLater
來啟動您的 gui,因為 Java GUI 應該在 EDT(事件調度線程)上運行而不應該在主線程上運行。
1) super.paintComponents(g)
; 內部paint()
可能是
public void paintComponent(Graphics g) {
super.paintComponent(g);
....
}
2) 不要設置任何大小setSize(500,500)
; 或setPreferredSize(new Dimension(300, 300));
, 對 JFrame 使用pack()
然后(取消注釋) setVisible(true)
並為JPanel
覆蓋getPreferredSize()
3) MyGui gui=new MyGui();
在public static void main(String []a){
,應該被包裝到invokeLater
,更多見Oracle tutorial Initial Thread
您是否嘗試設置布局管理器並將面板添加到 contentPane 而不是 JFrame 本身?
getContentPane().setLayout(new BorderLayout());
getContentPane().add(panel, BorderLayout.WEST);
框架的默認布局管理器是FlowLayout
而不是BorderLayout
。 嘗試在MyGui
構造MyGui
setLayout(new BorderLayout())
。
您沒有為內容窗格設置布局。 嘗試類似getContentPane.setLayout(new Layout())
有關布局管理器的詳細信息,請查看 oracle 文檔: http : //docs.oracle.com/javase/tutorial/uiswing/layout/layoutlist.html
希望這可以幫助
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.