0
嗨,大家好,我遇到了问题,看起来应该更加简单。我只是不能让我简单的自定义JComponent出现!我选择使用绝对定位,我不确定这是否会导致一些问题。任何意见和/或>解决方案<非常感谢。 Thankss!显示自定义JComponent的麻烦
(继承人我的代码)
public class XtremePaintballNetwork {
private static JFrame _xpbnWindow;
private static JTextField _chatTextField;
//private static Map _map;
private static Map _map;
public static void main(String[] args) {
// Set up main window
_xpbnWindow = new JFrame("Xtreme Paintball Network");
_xpbnWindow.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
_xpbnWindow.setSize(400, 300);;
//_xpbnWindow.setBackground(Color.white);
//_xpbnWindow.getContentPane().setBackground(Color.white);
//_xpbnWindow.pack();
_xpbnWindow.setVisible(true);
addComponentsToPane();
_xpbnWindow.addComponentListener(new ComponentListener() {
public void componentHidden(ComponentEvent e) {}
public void componentMoved(ComponentEvent e){}
public void componentResized(ComponentEvent e) {
adjustBounds();
}
public void componentShown(ComponentEvent e) {}
});
_xpbnWindow.repaint();
}
private static void addComponentsToPane() {
Container pane = _xpbnWindow.getContentPane();
// Use Absolute Positioning
pane.setLayout(null);
// Create GUI components
_map = new Map();
_chatTextField = new JTextField();
// Add components to pane
pane.add(_map);
pane.add(_chatTextField);
// Calculate and set Bounds
adjustBounds();
}
private static void adjustBounds() {
Container pane = _xpbnWindow.getContentPane();
// Use 'null' layout -> Absolute Positioning
Insets insets = pane.getInsets();
Dimension _windowDimension = pane.getSize();
Dimension _chatDimension = _chatTextField.getPreferredSize();
/*_map.setBounds(0, insets.top, _windowDimension.width - insets.left - insets.right,
_windowDimension.height - insets.top - insets.bottom);*/
_map.setBounds(10, 10, 100, 100);
_chatTextField.setBounds(0, _windowDimension.height - _chatDimension.height - insets.top - insets.bottom,
_windowDimension.width - insets.left - insets.right, _chatDimension.height);
}
}
和这里的简单JComponent类
public class Map extends JComponent{
//@Override
protected void PaintComponent(Graphics g){
super.paintComponent(g);
g.drawLine(0, 0, 70, 70);
g.drawString("string",20,20);
}
}
基本上,我的问题是,没有任何显示了我的JFrame内...:/帮助!
Geeze,谢谢!它的5点在这里,我想重新编程我丢失的东西,因为我没有支持它。 Java命名约定和我自己的知识目前只是拍摄。谢谢一吨哈哈。有人删除这篇文章,其推迟:P – sduffy89 2011-05-16 10:25:14
@ sduffy89 - 我注意到你评论了@覆盖标记。我认为它给了你一些警告,告诉你这种方法不会覆盖任何方法。不要忽视这些警告,这就是这个符号的意思...... – MByD 2011-05-16 10:30:26
+1匿名upVoter – mKorbel 2011-05-16 10:38:10