jpanel设置大小问题,谢谢

scljfh 2009-04-22 04:43:42
我在jframe中添加一个jpanel,每次鼠标出了jpanel的时候设置一下jFrame和jPanel 的大小,然后在绘制jPanel, 但为什么外层的jframe和jpanel大小改变了。。但绘制出来的区域(gc.fillRect(。。))的大小不变啊。。快疯了。。求帮助。。 谢谢

代码如下:
public class FrameTest extends JFrame {
private JPanel panel;
public static void main(String args[]) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
FrameTest frame = new FrameTest();
frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}

public FrameTest() {
super();
getContentPane().setLayout(null);
addMouseListener(new MouseAdapter() {
public void mouseClicked(final MouseEvent e) {
}
});
setBounds(100, 100, 500, 375);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
getContentPane().add(getPanel());
}
private void out(){
int width = this.getWidth()+20;
int height = this.getHeight()+20;
this.setSize(width,height);
this.setPreferredSize(new Dimension(width,height));
this.panel.setSize(width, height);
this.panel.setPreferredSize(new Dimension(width,height));
}
private void draw(Graphics gc){
gc.setColor(Color.RED);
gc.fillRect(0, 0, this.panel.getWidth(), this.panel.getHeight());
}

protected JPanel getPanel() {
if (panel == null) {
panel = new JPanel(){
public void paintComponent(Graphics gc) {
draw(gc);
}
};
panel.setBounds(0, 0, 492, 348);
panel.setLayout(null);
panel.addMouseListener(new MouseAdapter() {
public void mouseExited(final MouseEvent e) {
out();
}
});
final JButton button_1 = new JButton();
button_1.setBounds(137, 5, 106, 28);
button_1.setText("New JButton");
panel.add(button_1);
final JButton button = new JButton();
button.setBounds(248, 5, 106, 28);
button.setText("New JButton");
panel.add(button);
}
return panel;
}
}

...全文
1782 15 打赏 收藏 转发到动态 举报
写回复
用AI写文章
15 条回复
切换为时间正序
请发表友善的回复…
发表回复
crystal1016 2010-09-10
  • 打赏
  • 举报
回复
路过,看看
lishigui 2009-04-24
  • 打赏
  • 举报
回复
import java.awt.Color;
import java.awt.Dimension;
import java.awt.EventQueue;
import java.awt.Graphics;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
/**
* 作者:李 世贵
* JDK: 1.6
* 来源: http://blog.csdn.net/lishigui
* 欢迎转接,请保留作者和来源,谢谢!
* 2009-4-24 上午11:30:15
*/
public class FrameTest extends JFrame {
private JPanel panel;
private int width, height;

public static void main(String args[]) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
FrameTest frame = new FrameTest();
frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}

public FrameTest() {
super();
getContentPane().setLayout(null);
addMouseListener(new MouseAdapter() {
public void mouseClicked(final MouseEvent e) {
}
});
setBounds(100, 100, 500, 375);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
getContentPane().add(getPanel());
}

private void out() {
int width = this.getWidth() + 20;
int height = this.getHeight() + 20;
this.setSize(width, height);
this.setPreferredSize(new Dimension(width, height));
this.panel.setSize(width, height);
this.panel.setPreferredSize(new Dimension(width, height));
}

private void draw(Graphics gc) {
gc.setColor(Color.RED);
gc.fillRect(0, 0, width, height);
}

protected JPanel getPanel() {
if (panel == null) {
panel = new JPanel() {
public void paintComponent(Graphics gc) {
draw(gc);
}
};
panel.setBounds(0, 0, 492, 348);
panel.setLayout(null);

width = 492;
height = 348;

panel.addMouseListener(new MouseAdapter() {
public void mouseExited(final MouseEvent e) {
out();
}
});
final JButton button_1 = new JButton();
button_1.setBounds(137, 5, 106, 28);
button_1.setText("New JButton");
panel.add(button_1);
final JButton button = new JButton();
button.setBounds(248, 5, 106, 28);
button.setText("New JButton");
panel.add(button);
}
return panel;
}
}

x360995630 2009-04-24
  • 打赏
  • 举报
回复
调用Panel的 repaint()
lishigui 2009-04-24
  • 打赏
  • 举报
回复
如果你是不想让绘制jPanel跟隨下jFrame和jPanel 的大小變化而改變的話,你就把
draw()方法中的gc.fillRect(0, 0, this.panel.getWidth(), this.panel.getHeight())去掉,
因為第次jPanel變化時都會調draw()哦
shmilycharlene 2009-04-24
  • 打赏
  • 举报
回复
出来的区域(gc.fillRect(。。))

关于这一段我不了解..看不懂你的意思
lishigui 2009-04-24
  • 打赏
  • 举报
回复
大哥,我看了三遍你的问题,都不知道你问什么,小弟不才啊!!
要是你真的想问人问题的话,首先要把你的问题说清楚一些,好吗??
lishigui 2009-04-24
  • 打赏
  • 举报
回复
大哥,试过我上面的代码没???难道我的就不可以实现吗????
scljfh 2009-04-24
  • 打赏
  • 举报
回复
10楼测试可用,谢谢。
slj327 2009-04-24
  • 打赏
  • 举报
回复
在构造函数中添加下列事件就可以了,具体原因不祥:
this.addComponentListener(new ComponentAdapter() {
public void componentResized(final ComponentEvent e) {
super.componentResized(e);
}
});
junana521 2009-04-23
  • 打赏
  • 举报
回复
我也是刚刚学,加油!
scljfh 2009-04-23
  • 打赏
  • 举报
回复
我 多多了次测试,当先把外层的jFrame设置的比较大,鼠标out的时候只修改jpanel的大小,jpanel的大小绘制是正确的。为什么都修改就不可以了呢。。。郁闷中。。
wingo394005865 2009-04-22
  • 打赏
  • 举报
回复
正在学习这方面的内容。。。。
期待答案的到来。。。。
kunsnat 2009-04-22
  • 打赏
  • 举报
回复
有点怪异哈.. 代码运行出来后 会一点点的自己扩大. 但是楼主所说的JPanel 和 Frame 会随着变化的. 就是要等延迟.... =.=! 好假.

62,614

社区成员

发帖
与我相关
我的任务
社区描述
Java 2 Standard Edition
社区管理员
  • Java SE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧