当前位置: 代码迷 >> Java相关 >> JFame 的add方法功能问题..
  详细解决方案

JFame 的add方法功能问题..

热度:325   发布时间:2007-07-09 15:27:08.0
JFame 的add方法功能问题..

我写了一个这如下的选定一区域的程序.
结果什么都没有显示.请大家帮忙看看..
import java.awt.*;
import java.awt.event.*;
import java.applet.Applet;
import javax.swing.*;

class CaptureScreen extends Applet implements MouseListener
\\ 选定一区域并画出矩形.
{
int x1,x2,y1,y2,width,height;
boolean flag = false;

public void init()
{
this.addMouseListener(this);
}

public void mousePressed(MouseEvent Event)
{
flag = true;
x1 = Event.getX();
y1 = Event.getY();
}

public void mouseReleased(MouseEvent Event)
{
x2 = Event.getX();
y2 = Event.getY();
}

public void paint(Graphics g)
{
if(flag)
{
if(x2<x1 && y2<y1) {x1 = x2;y1 = y2;}
if(x2<x1 && y1>y2) {x1 = x2;}
if(x2>x1 && y2<y1) {y1 = y2;}

width=Math.abs(x2-x1);
height=Math.abs(y2-y1);

g.drawRect(x1,y1,width,height);
}
}

public void mouseClicked(MouseEvent Event) {}
public void mouseEntered(MouseEvent Event) {}
public void mouseExited(MouseEvent Event) {}
}

public class Appl5_1
{ \\main 方法
public static void main(String[] args)
{
JFrame Frame = new JFrame();
Frame.setSize(1000,3000);
Frame.setTitle("");
Frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

CaptureScreen capturescreen = new CaptureScreen();
Frame.add(capturescreen);
\\这里添加组件,怎么没有显示??
Frame.setVisible(true);
}
}

搜索更多相关的解决方案: JFame  add  

----------------解决方案--------------------------------------------------------
不知道Applet能不能放到JFrame中,

不过这里,变量名取得有点问题,Frame是关键字。
JFrame Frame = new JFrame();
----------------解决方案--------------------------------------------------------

改了JComponet 也一样啊.
问问JFrame add(对象)
JFrame所执行的是 对象中的所有方法还是只有paint方法...


----------------解决方案--------------------------------------------------------
pity1115 几乎每次你都回我的贴嘛.

3Q 你辛苦了。
----------------解决方案--------------------------------------------------------

谁能帮帮忙看看这程序的一点错误.
----------------解决方案--------------------------------------------------------

[CODE]
import java.awt.*;
import java.awt.event.*;
import java.applet.Applet;
import javax.swing.*;
class CaptureScreen extends Applet implements MouseListener {
int x1;
int x2;
int y1;
int y2;
int width;
int height;
boolean flag = false;
public void init() {
this.addMouseListener(this);
}
public void mousePressed(MouseEvent Event) {
flag = true;
x1 = Event.getX();
y1 = Event.getY();
}
public void mouseReleased(MouseEvent Event) {
x2 = Event.getX();
y2 = Event.getY();
repaint();
}
public void paint(Graphics g) {
if (flag) {
if (x2 < x1 && y2 < y1) {
x1 = x2;
y1 = y2;
}
if (x2 < x1 && y1 > y2) {
x1 = x2;
}
if (x2 > x1 && y2 < y1) {
y1 = y2;
}
width = Math.abs(x2 - x1);
height = Math.abs(y2 - y1);
g.drawRect(x1, y1, width, height);
}
}
public void mouseClicked(MouseEvent Event) {
}
public void mouseEntered(MouseEvent Event) {
}
public void mouseExited(MouseEvent Event) {
}
}
public class App15_1 {
//main 方法
public static void main(String[] args) {
JFrame Frame = new JFrame();
Frame.setSize(1000, 3000);
Frame.setTitle("");
Frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
CaptureScreen capturescreen = new CaptureScreen();
capturescreen.init();
Frame.add(capturescreen, BorderLayout.CENTER);
//这里添加组件,怎么没有显示??
Frame.setVisible(true);
}
}[/CODE]


----------------解决方案--------------------------------------------------------

哈哈,想学楼上写个截屏的程序.
现在刚学JAVA只能写到以上的
给自己加油.
谢谢楼上


----------------解决方案--------------------------------------------------------
以下是引用pity1115在2007-7-9 16:39:54的发言:
不知道Applet能不能放到JFrame中,

不过这里,变量名取得有点问题,Frame是关键字。
JFrame Frame = new JFrame();

变量也用大写了?
有点问题了!


----------------解决方案--------------------------------------------------------
哈哈,
那是ultraedit自动搞的效果,我本来的打是小写的.
现在不用那个了..


----------------解决方案--------------------------------------------------------
  相关解决方案