作者ArthurJack (Weston)
看板java
标题[问题] JPANEL 无法加入 JFRAME中
时间Fri Apr 17 19:24:42 2020
版上各位好,不好意思想请问各位一个问题
就是目前有个学校作业,我打算使用JPANEL来建立一个画布
目前初步程式如下
public static class canvas extends JPanel implements
MouseListener,MouseMotionListener{
int x = 0;
int y = 0;
int startx, starty, endx, endy;//起始座标与终点座标
public canvas() {
super.setBackground(Color.blue);
super.addMouseListener(this);
super.addMouseMotionListener(this);
super.setSize(800, 800);
super.setVisible(true);
}
public void mousePressed(MouseEvent e){
startx = e.getX();
starty = e.getY();
}
public void mouseReleased(MouseEvent e){
endx = e.getX();
endy = e.getY();
}
public void mouseEntered(MouseEvent e){}
public void mouseExited(MouseEvent e){}
public void mouseClicked(MouseEvent e) {}
public void mouseDragged(MouseEvent e) {}
public void mouseMoved(MouseEvent e) {}
public void paint(Graphics g) {
g.setColor(Color.black);
g.drawLine(startx, starty, endx, endy);
}
}
public static void main(String[] args) {
JFrame Editor = new JFrame("UML Editor");
Container content = Editor.getContentPane();
content.setBackground(Color.green);
///////////////////////////////////////////////////////////////////////
JPanel toolbar = new JPanel();
toolbar.setBackground(Color.black);
content.add(toolbar, BorderLayout.WEST);
JPanel menu = new JPanel();
menu.setBackground(Color.black);
content.add(menu, BorderLayout.NORTH);
///////////////////////////////////////////////////////////////////////
canvas area=new canvas();
area.setBackground(Color.black);
content.add(area , BorderLayout.CENTER);
area.setVisible(true);
Editor.setSize(1500, 800);
Editor.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
Editor.setVisible(true);
}
----------------------------------------------------------------------------
我的TOOLBAR跟MENU都能正常显现,但是唯独最後的AERA无法
如果我只加入後面那项,那连FRAME本身的背景色都无法显示,想请问
是否是我的继承项有问题?????
--
※ 发信站: 批踢踢实业坊(ptt.cc), 来自: 140.115.221.72 (台湾)
※ 文章网址: https://webptt.com/cn.aspx?n=bbs/java/M.1587122684.A.366.html
1F:推 AI3767: paint()先加上一行 super.paint(g); 试试 04/17 22:10
2F:→ ArthurJack: 可以正常显示了,十分感谢 04/17 22:27
3F:推 davidlv7621: 进ja? 05/23 17:44