正如标题所说,我可以重新定义Java的BorderLayout.CENTER的高度和宽度吗?我计划使用Graphics2D绘制到这个区域,但我不会总是提前知道将所有东西放入其中应该有多大。
绘制类(在我的Main类中声明):
@SuppressWarnings("serial")
private class Draw extends JComponent {
public void paint(Graphics graphics) {
Graphics2D g = (Graphics2D) graphics;
g.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
Shape drawLine = new Line2D.Float(0, 0, 500, 500);
g.setPaint(Color.BLACK);
g.draw(drawLine);
}
}
调用Draw的主构造函数:
public Main() {
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setBounds(100, 100, 640, 480);
setJMenuBar(getMenuBar_1());
contentPane = new JPanel();
contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
setContentPane(contentPane);
contentPane.setLayout(new BorderLayout(0, 0));
contentPane.add(getPanelControls(), BorderLayout.SOUTH);
contentPane.add(getPanelInfo(), BorderLayout.EAST);
contentPane.add(new Draw(), BorderLayout.CENTER);
}
我目前正在直接绘制到BorderLayout.CENTER。我应该绘制到JPanel并使用它的setPreferredSize()方法吗?
答案 0 :(得分:2)
正如标题所说,我可以重新定义Java的高度和宽度 BorderLayout.CENTER?我计划使用Graphics2D绘制到这个区域但是 我不会总是提前知道它应该有多大 一切都在。
(仅回答主题)
然后任何尺码都会产生反作用,请在JFrame.pack();
JFrame.setVisible(true);
创建JFrame
作为本地变量
修改
覆盖paintComponent()
而非paint()
,paintComponent()
内的第一个代码行应为super.paintComponent()
,否则绘制累积
Shape drawLine = new Line2D.Float(0, 0, 500, 500);
应为getHeight/Weight()
而不是500, 500
答案 1 :(得分:0)
请勿在{{1}}分配固定像素。使用Graphics
和getHeight()
方法获取动态大小。使用该大小绘制线条。
getWidth()