我正在开发java swing应用程序 . 我尝试在我的主JFrame上添加2个JPanel .
我的主要JPanel类:
public class DrawingPanel extends JPanel {
//mouse variables here
//Point mPt = new Point(0,0);
public DrawingPanel() {
setBackground(COLOURBACK);
MyMouseListener ml = new MyMouseListener();
addMouseListener(ml);
}
public Dimension getPreferredSize() {
return new Dimension(width, height);
}
....
....
....
}
当我添加为主框架上的唯一组件时工作正常 . 但是当我尝试在框架上添加另一个JPanel时,我的DrawingPanel显示为小白框 .
private void createAndShowGUI()
{
DrawingPanel panel = new DrawingPanel();
JFrame frame = new JFrame("Hex Testing 4");
frame.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );
Container content = frame.getContentPane();
JPanel aggregationFrame = new JPanel(new GridBagLayout());
aggregationFrame.add(panel);
aggregationFrame.add(new JLabel("Enter username:"));
content.add(aggregationFrame);
//this.add(panel); -- cannot be done in a static context
//for hexes in the FLAT orientation, the height of a 10x10 grid is 1.1764 * the width. (from h / (s+t))
frame.setSize( (int)(SCRSIZE/1.23), SCRSIZE);
frame.setResizable(false);
frame.setLocationRelativeTo( null );
frame.setVisible(true);
}
看起来像这样
我尝试将getPreferredSize()方法添加到DrawingPanel中,如JPanel appears as a small white box问题中所建议的那样,但它不能正常工作 .
你能帮我解决这个问题吗?
UPDATE:
我已经改变了布局,现在我看到了一半的JPanel . 我认为问题与JPanels和JFrame的固定大小有关 . 将调查它 .