JAVA朋友们,帮我看看这个小程序问题出现在哪里,应该怎么解决?最小化后再打开两条直线不见了
importjava.awt.*;importjava.awt.event.*;importjavax.swing.*;classTestextendsJFrame{pu...
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
class Test extends JFrame
{
public void paint(Graphics g)
{
g.drawLine(20, 105, 690, 105);
g.drawLine(611, 40, 611, 550);
}
Test(String str)
{
super(str);
setSize(700, 600);
Container ct = getContentPane();
ct.setLayout(null);
JButton bt1 = new JButton();
bt1.setBounds(481, 45, 60, 25);
ct.add(bt1);
setDefaultCloseOperation(EXIT_ON_CLOSE);
setResizable(false);
addWindowListener(new myfirst());
setVisible(true);
}
public static void main(String[] args)
{
Test t = new Test("gg");
}
}
class myfirst extends JFrame implements WindowListener
{
public void windowOpened(WindowEvent e)
{
repaint();
}
public void windowClosing(WindowEvent e)
{
}
public void windowClosed(WindowEvent e)
{
}
public void windowIconified(WindowEvent e)
{
}
public void windowDeiconified(WindowEvent e)
{
}
public void windowActivated(WindowEvent e)
{
}
public void windowDeactivated(WindowEvent e)
{
}
}
麻烦在上面改改,我要完整的代码,谢谢,我不知道怎么使用repaint()方法 展开
import java.awt.event.*;
import javax.swing.*;
class Test extends JFrame
{
public void paint(Graphics g)
{
g.drawLine(20, 105, 690, 105);
g.drawLine(611, 40, 611, 550);
}
Test(String str)
{
super(str);
setSize(700, 600);
Container ct = getContentPane();
ct.setLayout(null);
JButton bt1 = new JButton();
bt1.setBounds(481, 45, 60, 25);
ct.add(bt1);
setDefaultCloseOperation(EXIT_ON_CLOSE);
setResizable(false);
addWindowListener(new myfirst());
setVisible(true);
}
public static void main(String[] args)
{
Test t = new Test("gg");
}
}
class myfirst extends JFrame implements WindowListener
{
public void windowOpened(WindowEvent e)
{
repaint();
}
public void windowClosing(WindowEvent e)
{
}
public void windowClosed(WindowEvent e)
{
}
public void windowIconified(WindowEvent e)
{
}
public void windowDeiconified(WindowEvent e)
{
}
public void windowActivated(WindowEvent e)
{
}
public void windowDeactivated(WindowEvent e)
{
}
}
麻烦在上面改改,我要完整的代码,谢谢,我不知道怎么使用repaint()方法 展开
5个回答
展开全部
不是repaint的问题,是你在画线的时候没有擦除背景,改成这样就可以了:
public void paint(Graphics g)
{
g.setColor(0);
g.fillRect(0,0,getWidth(),getHeight());
g.setColor(0xff0000);
g.drawLine(20, 105, 690, 105);
g.drawLine(611, 40, 611, 550);
}
注意:在绘图中,先擦除背景然后再绘图室基本常识。否则就可能出现画面支离破碎甚至不显示的情况。
public void paint(Graphics g)
{
g.setColor(0);
g.fillRect(0,0,getWidth(),getHeight());
g.setColor(0xff0000);
g.drawLine(20, 105, 690, 105);
g.drawLine(611, 40, 611, 550);
}
注意:在绘图中,先擦除背景然后再绘图室基本常识。否则就可能出现画面支离破碎甚至不显示的情况。
更多追问追答
追问
我把你的程序添加进去要报错,setColor()中的参数是一个对象,你的怎么是一个整型0啊,麻烦你直接在我的程序上面改改,望运行有正确的结果
追答
哦,不好意思,我正在看J2ME代码,把SE和ME搞混了。改成这样:
public void paint(Graphics g)
{
g.setColor(Color.BLACK);
g.fillRect(0,0,getWidth(),getHeight());
g.setColor(Color.RED);
g.drawLine(20, 105, 690, 105);
g.drawLine(611, 40, 611, 550);
}
我帮你看了下,你的代码未作任何修改,在我的机器上并没有出现你说的情况,表现完全正常(我使用的是JBuilder)。所以,我怀疑问题出在你自己的IDE上,可能有什么地方构建的不对,你可以重新构建一个。
另外,你的代码里,myfirst 这个类也继承了JFrame,这完全没有必要,可以直接去掉,因为如果这样的话,你在这个类里调用repaint,将只会更新myfirst 本身,跟Test类没有半点关系,改成这样就好了:
class myfirst implements WindowListener。想要添加更新,则需要在myfirst 中传入一个JFrame的句柄,用这个句柄更新,修改后代码如下(因为字数所限,而且Test类并未做任何修改,所以不贴了):
class myfirst implements WindowListener {
JFrame parent;
public myfirst(JFrame frame) {
this.parent = frame;
}
public void windowOpened(WindowEvent e) {
parent.repaint();
}
public void windowClosing(WindowEvent e) {
}
public void windowClosed(WindowEvent e) {
}
public void windowIconified(WindowEvent e) {
}
public void windowDeiconified(WindowEvent e) {
}
public void windowActivated(WindowEvent e) {
parent.repaint();
}
public void windowDeactivated(WindowEvent e) {
parent.repaint();
}
}
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
JFrame 结构太复杂,如果想编写自己的组件,就写一个JComponent的组件,在paintComponent()方法里,写你想要实现的内容,或者paint()方法 这两个方法都可以,切忌直接使用JFrame框架,这玩意太复杂。
import java.awt.Graphics;
import javax.swing.JButton;
import javax.swing.JComponent;
import javax.swing.JFrame;
class Test extends JComponent {
public void paintComponent(Graphics g) {
g.drawLine(20, 105, 690, 105);
g.drawLine(611, 40, 611, 550);
}
Test() {
JButton bt1 = new JButton();
bt1.setBounds(481, 45, 60, 25);
add(bt1);
}
public static void main(String[] args) {
Test t = new Test();
JFrame f = new JFrame("gg");
f.setSize(700, 600);
f.add(t);
// f.pack();
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.setVisible(true);
}
}
import java.awt.Graphics;
import javax.swing.JButton;
import javax.swing.JComponent;
import javax.swing.JFrame;
class Test extends JComponent {
public void paintComponent(Graphics g) {
g.drawLine(20, 105, 690, 105);
g.drawLine(611, 40, 611, 550);
}
Test() {
JButton bt1 = new JButton();
bt1.setBounds(481, 45, 60, 25);
add(bt1);
}
public static void main(String[] args) {
Test t = new Test();
JFrame f = new JFrame("gg");
f.setSize(700, 600);
f.add(t);
// f.pack();
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.setVisible(true);
}
}
追问
朋友,你好,你的代码我运行了,就是我想要的效果,但是我看到书上都是继承自JFrame,真不明白
追答
看core java 一书!书中的列子 就没有一个使用JFrame 这种重量级的组件的
JFrame 唯一的作用 就是 作为最顶层的容器来显示,而且编写swing是,千万别把框架当成普通轻量级的组件或容器来用
本回答被提问者采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
class Test extends JFrame
{
public void paint(Graphics g)
{
g.drawLine(20, 105, 690, 105);
g.drawLine(611, 40, 611, 550);
}
Test(String str)
{
super(str);
setSize(700, 600);
Container ct = getContentPane();
ct.setLayout(null);
JButton bt1 = new JButton();
bt1.setBounds(481, 45, 60, 25);
ct.add(bt1);
setDefaultCloseOperation(EXIT_ON_CLOSE);
setResizable(false);
addWindowListener(new myfirst());
setVisible(true);
}
public static void main(String[] args)
{
Test t = new Test("gg");
}
}
class myfirst extends JFrame implements WindowListener
{
public void windowOpened(WindowEvent e)
{
repaint();
}
public void windowClosing(WindowEvent e)
{
}
public void windowClosed(WindowEvent e)
{
}
public void windowIconified(WindowEvent e)
{
}
public void windowDeiconified(WindowEvent e)
{
}
public void windowActivated(WindowEvent e)
{
repaint();
}
public void windowDeactivated(WindowEvent e)
{
}
}
repaint();就是刷新
import java.awt.event.*;
import javax.swing.*;
class Test extends JFrame
{
public void paint(Graphics g)
{
g.drawLine(20, 105, 690, 105);
g.drawLine(611, 40, 611, 550);
}
Test(String str)
{
super(str);
setSize(700, 600);
Container ct = getContentPane();
ct.setLayout(null);
JButton bt1 = new JButton();
bt1.setBounds(481, 45, 60, 25);
ct.add(bt1);
setDefaultCloseOperation(EXIT_ON_CLOSE);
setResizable(false);
addWindowListener(new myfirst());
setVisible(true);
}
public static void main(String[] args)
{
Test t = new Test("gg");
}
}
class myfirst extends JFrame implements WindowListener
{
public void windowOpened(WindowEvent e)
{
repaint();
}
public void windowClosing(WindowEvent e)
{
}
public void windowClosed(WindowEvent e)
{
}
public void windowIconified(WindowEvent e)
{
}
public void windowDeiconified(WindowEvent e)
{
}
public void windowActivated(WindowEvent e)
{
repaint();
}
public void windowDeactivated(WindowEvent e)
{
}
}
repaint();就是刷新
更多追问追答
追问
你在电脑上面测试过没有?我运行起结果还是那样,问题没有解决,麻烦再看看
追答
我试过了。。。
你在每个WindowListener接口的函数里都加上repaint();试试
参考资料: repaint();
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
我可以给你介绍一套视频,一直看着感觉还不错,在动力节点的官方网站就有,你可以去看看。
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
表示路过
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询