cp.add(operation,BorderLayout.NORTH);
cp.add(canvas,BorderLayout.CENTER);
可是,位于CENTERR的Canvas对象总是把上面拉下来的菜单遮住。
我曾经试过这样的方法,把Canvas先放到一个JPanel中,再把JPanel放到JApplet中,可事实是这样也不行。
我用得编译器是JDK1.4,程序由一个图形抽象基类AllShape和三个派生图形类ShapeLine,ShapeCircle,ShapeRect以及一个JApplet类组成。编译环境Win98。
9 个解决方案
#1
/**Paint.java
*@author Pilgrim
*@version 1.0
*@date 03-10-27 8:01
*/
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.util.*;
import com.bruceeckel.swing.*;
import java.io.*;
public class Paint extends JApplet
{
//Make sure that there is only one Applet
private static int flag;//1 for line, 2 for rect, 3 for circle
public static int getFlag(){return flag;}//function used to encapsulate the flag;
JMenuBar mainMenu = new JMenuBar();
JMenu file = new JMenu("File");
JMenu edit = new JMenu("Edit");
JMenu draw = new JMenu("Draw");
String[] control = {"Open","Save","Exit","Copy","Paste","Delete"};
JMenuItem[] fileMenu =
{
new JMenuItem(control[0], KeyEvent.VK_O),
new JMenuItem(control[1], KeyEvent.VK_S),
new JMenuItem(control[2], KeyEvent.VK_E)
};
JMenuItem[] editMenu =
{
new JMenuItem(control[3], KeyEvent.VK_C),
new JMenuItem(control[4], KeyEvent.VK_P),
new JMenuItem(control[5], KeyEvent.VK_D)
};
JMenuItem[] drawMenu =
{
new JMenuItem("Line", KeyEvent.VK_L),
new JMenuItem("Rect", KeyEvent.VK_R),
new JMenuItem("Circle", KeyEvent.VK_C)
};
JToolBar operation = new JToolBar();
//An array to contain the drawn shapes
private static ShapeList saveShapes = new ShapeList();
public static ShapeList getShapeList(){return saveShapes;}
//Canvas to be drawn on;
MyCanvas canvas = new MyCanvas();//Canvas.paint();function need to be overrided
public void init()
{
//initializing the mainMenu
file.setMnemonic(KeyEvent.VK_F);
edit.setMnemonic(KeyEvent.VK_E);
draw.setMnemonic(KeyEvent.VK_D);
mainMenu.add(file);
mainMenu.add(edit);
mainMenu.add(draw);
MenuListener mListener = new MenuListener();
for(int i = 0; i < fileMenu.length; i++)
{
fileMenu[i].addActionListener(mListener);
file.add(fileMenu[i]);
}
for(int i = 0; i < editMenu.length; i++)
{
editMenu[i].addActionListener(mListener);
edit.add(editMenu[i]);
}
for(int i = 0; i < drawMenu.length; i++)
{
drawMenu[i].addActionListener(mListener);
draw.add(drawMenu[i]);
}
for(int i = 0; i < control.length; i++)
{
operation.add(new JButton(control[i]));
if((i+1) % 3 == 0)
operation.addSeparator();
}
setJMenuBar(mainMenu);
//Set layout of the pane
Container cp = getContentPane();
//cp.setLayout(new BorderLayout());
cp.add(operation,BorderLayout.NORTH);
cp.add(canvas,BorderLayout.CENTER);
}//init()
public void report(String shownText)
{
JFrame noFrame = null;
new MyDialog(noFrame,shownText).show();
}//report
public class MenuListener implements ActionListener
{
public void actionPerformed(ActionEvent e)
{
JMenuItem target = (JMenuItem)e.getSource();
String actionCommand = target.getActionCommand();
//Waitint to be implemented
//Perform action to draw on the
if(actionCommand.equals("Line"))
flag = 1;
else if(actionCommand.equals("Rect"))
flag = 2;
else if(actionCommand.equals("Circle"))
flag = 3;
}
};//semicolon needed//inner class MenuListener
public static void main(String[] args)
{
Console.run(new Paint(), 600, 450);
}
}
class ShapeList extends ArrayList
{
public AllShape getElem(int index)
{
return (AllShape)get(index);
}
public AllShape removeElem(int index)
{
return (AllShape)remove(index);
}
}
class MyCanvas extends Canvas
{
ShapeList saveShapes;
int flag;
Graphics graphics;
public void paint(Graphics g)
{
graphics = g;
g.drawRect(0, 0, getWidth() - 1, getHeight() - 1);
int size = saveShapes.size();
for(int i = 0; i < size; i++)
saveShapes.getElem(i).draw(g);
}//Method paint()
public MyCanvas()
{
//report(graphics.toString());//!!!!!!!!!!!!
flag = Paint.getFlag();
setBackground(Color.white);
saveShapes = Paint.getShapeList();
//report(saveShapes.toString());//I failed to get any referance;
AllMouseListener allListener = new AllMouseListener();
addMouseListener(allListener);
addMouseMotionListener(allListener);
}//Constructor
class AllMouseListener implements MouseListener, MouseMotionListener
{
Point startPoint;
AllShape current;
public void mousePressed(MouseEvent e)
{
startPoint = e.getPoint();
flag = Paint.getFlag();
switch(flag)
{
case 1:
graphics.drawLine(100,200,300,400);
current = new ShapeLine(startPoint.x, startPoint.y);
break;
case 2:
current = new ShapeRect(startPoint.x, startPoint.y);
break;
case 3:
current = new ShapeCircle(startPoint.x, startPoint.y);
break;
default://Change the status bar to notify the user;
}
}//Press
public void mouseDragged(MouseEvent e)
{
//Xpos & Ypos in the status bar changes
// drawing a temporary shape
if(flag != 0)//about to draw
{
current.setEndPoint(e.getX(), e.getY());
current.draw(graphics);
}
}//Drag
public void mouseReleased(MouseEvent e)
{
if(flag != 0)//about to draw
{
current.setEndPoint(e.getX(), e.getY());
//update(graphics);
current.draw(graphics);
saveShapes.add(current);
}
}//Release
//Next methos is of no use
public void mouseClicked(MouseEvent e) {}
public void mouseEntered(MouseEvent e) {}
public void mouseExited(MouseEvent e) {}
public void mouseMoved(MouseEvent e)
{
//Xpos & Ypos in the status bar changes
}
} //inner class Listener
public void report(String shownText)
{
JFrame noFrame = null;
new MyDialog(noFrame,shownText).show();
}//report
}//class MyCanvas
class MyDialog extends JDialog
{
public MyDialog(JFrame parent,String shownText)
{
super(parent, "Report", false);
Container cp = getContentPane();
cp.setLayout(new BorderLayout());
cp.add(new JLabel(shownText, JLabel.CENTER), BorderLayout.NORTH);
JButton ok = new JButton("OK");
ok.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
dispose(); // Closes the dialog
}
});
cp.add(ok, BorderLayout.SOUTH);
setSize(200,150);
Dimension d = Toolkit.getDefaultToolkit().getScreenSize();
setLocation((d.width - getSize().width) / 2, (d.height - getSize().height) / 2);
}
}
*@author Pilgrim
*@version 1.0
*@date 03-10-27 8:01
*/
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.util.*;
import com.bruceeckel.swing.*;
import java.io.*;
public class Paint extends JApplet
{
//Make sure that there is only one Applet
private static int flag;//1 for line, 2 for rect, 3 for circle
public static int getFlag(){return flag;}//function used to encapsulate the flag;
JMenuBar mainMenu = new JMenuBar();
JMenu file = new JMenu("File");
JMenu edit = new JMenu("Edit");
JMenu draw = new JMenu("Draw");
String[] control = {"Open","Save","Exit","Copy","Paste","Delete"};
JMenuItem[] fileMenu =
{
new JMenuItem(control[0], KeyEvent.VK_O),
new JMenuItem(control[1], KeyEvent.VK_S),
new JMenuItem(control[2], KeyEvent.VK_E)
};
JMenuItem[] editMenu =
{
new JMenuItem(control[3], KeyEvent.VK_C),
new JMenuItem(control[4], KeyEvent.VK_P),
new JMenuItem(control[5], KeyEvent.VK_D)
};
JMenuItem[] drawMenu =
{
new JMenuItem("Line", KeyEvent.VK_L),
new JMenuItem("Rect", KeyEvent.VK_R),
new JMenuItem("Circle", KeyEvent.VK_C)
};
JToolBar operation = new JToolBar();
//An array to contain the drawn shapes
private static ShapeList saveShapes = new ShapeList();
public static ShapeList getShapeList(){return saveShapes;}
//Canvas to be drawn on;
MyCanvas canvas = new MyCanvas();//Canvas.paint();function need to be overrided
public void init()
{
//initializing the mainMenu
file.setMnemonic(KeyEvent.VK_F);
edit.setMnemonic(KeyEvent.VK_E);
draw.setMnemonic(KeyEvent.VK_D);
mainMenu.add(file);
mainMenu.add(edit);
mainMenu.add(draw);
MenuListener mListener = new MenuListener();
for(int i = 0; i < fileMenu.length; i++)
{
fileMenu[i].addActionListener(mListener);
file.add(fileMenu[i]);
}
for(int i = 0; i < editMenu.length; i++)
{
editMenu[i].addActionListener(mListener);
edit.add(editMenu[i]);
}
for(int i = 0; i < drawMenu.length; i++)
{
drawMenu[i].addActionListener(mListener);
draw.add(drawMenu[i]);
}
for(int i = 0; i < control.length; i++)
{
operation.add(new JButton(control[i]));
if((i+1) % 3 == 0)
operation.addSeparator();
}
setJMenuBar(mainMenu);
//Set layout of the pane
Container cp = getContentPane();
//cp.setLayout(new BorderLayout());
cp.add(operation,BorderLayout.NORTH);
cp.add(canvas,BorderLayout.CENTER);
}//init()
public void report(String shownText)
{
JFrame noFrame = null;
new MyDialog(noFrame,shownText).show();
}//report
public class MenuListener implements ActionListener
{
public void actionPerformed(ActionEvent e)
{
JMenuItem target = (JMenuItem)e.getSource();
String actionCommand = target.getActionCommand();
//Waitint to be implemented
//Perform action to draw on the
if(actionCommand.equals("Line"))
flag = 1;
else if(actionCommand.equals("Rect"))
flag = 2;
else if(actionCommand.equals("Circle"))
flag = 3;
}
};//semicolon needed//inner class MenuListener
public static void main(String[] args)
{
Console.run(new Paint(), 600, 450);
}
}
class ShapeList extends ArrayList
{
public AllShape getElem(int index)
{
return (AllShape)get(index);
}
public AllShape removeElem(int index)
{
return (AllShape)remove(index);
}
}
class MyCanvas extends Canvas
{
ShapeList saveShapes;
int flag;
Graphics graphics;
public void paint(Graphics g)
{
graphics = g;
g.drawRect(0, 0, getWidth() - 1, getHeight() - 1);
int size = saveShapes.size();
for(int i = 0; i < size; i++)
saveShapes.getElem(i).draw(g);
}//Method paint()
public MyCanvas()
{
//report(graphics.toString());//!!!!!!!!!!!!
flag = Paint.getFlag();
setBackground(Color.white);
saveShapes = Paint.getShapeList();
//report(saveShapes.toString());//I failed to get any referance;
AllMouseListener allListener = new AllMouseListener();
addMouseListener(allListener);
addMouseMotionListener(allListener);
}//Constructor
class AllMouseListener implements MouseListener, MouseMotionListener
{
Point startPoint;
AllShape current;
public void mousePressed(MouseEvent e)
{
startPoint = e.getPoint();
flag = Paint.getFlag();
switch(flag)
{
case 1:
graphics.drawLine(100,200,300,400);
current = new ShapeLine(startPoint.x, startPoint.y);
break;
case 2:
current = new ShapeRect(startPoint.x, startPoint.y);
break;
case 3:
current = new ShapeCircle(startPoint.x, startPoint.y);
break;
default://Change the status bar to notify the user;
}
}//Press
public void mouseDragged(MouseEvent e)
{
//Xpos & Ypos in the status bar changes
// drawing a temporary shape
if(flag != 0)//about to draw
{
current.setEndPoint(e.getX(), e.getY());
current.draw(graphics);
}
}//Drag
public void mouseReleased(MouseEvent e)
{
if(flag != 0)//about to draw
{
current.setEndPoint(e.getX(), e.getY());
//update(graphics);
current.draw(graphics);
saveShapes.add(current);
}
}//Release
//Next methos is of no use
public void mouseClicked(MouseEvent e) {}
public void mouseEntered(MouseEvent e) {}
public void mouseExited(MouseEvent e) {}
public void mouseMoved(MouseEvent e)
{
//Xpos & Ypos in the status bar changes
}
} //inner class Listener
public void report(String shownText)
{
JFrame noFrame = null;
new MyDialog(noFrame,shownText).show();
}//report
}//class MyCanvas
class MyDialog extends JDialog
{
public MyDialog(JFrame parent,String shownText)
{
super(parent, "Report", false);
Container cp = getContentPane();
cp.setLayout(new BorderLayout());
cp.add(new JLabel(shownText, JLabel.CENTER), BorderLayout.NORTH);
JButton ok = new JButton("OK");
ok.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
dispose(); // Closes the dialog
}
});
cp.add(ok, BorderLayout.SOUTH);
setSize(200,150);
Dimension d = Toolkit.getDefaultToolkit().getScreenSize();
setLocation((d.width - getSize().width) / 2, (d.height - getSize().height) / 2);
}
}
#2
//AllShape.java
import java.io.Serializable.*;//needed or not?
import java.io.*;
import java.awt.*;
abstract public class AllShape implements Serializable
{
abstract public void draw(Graphics g);
abstract public void setBorderColor(Color borderColor);
abstract public void setFillColor(Color fillColor);
//Check whether the shape has been selected
abstract public boolean selected();
//Resize the selected shapes,
//Succeed with true returned ,while false when go out of bound;
abstract public boolean resize();
abstract public void setEndPoint(int x2, int y2);
}
//ShapeCircle.java
class ShapeCircle extends AllShape
{
//xcoordinate, ycoordinate, width, height, start angle, end angle;
private int x, y, width, height, start, end;
private Color borderColor, fillColor;
public ShapeCircle(int x, int y)
{
this.x = x;
this.y = y;
width = 0;
height = 0;
this.start = 0;//From 0 degree to 360 degree ,This is a circle
this.end = 360;
borderColor = Color.black;
fillColor = Color.white;
}
public ShapeCircle(int x, int y, int width, int height, int start, int end)
{
this.x=x;
this.y=y;
this.width =width;
this.height =height;
this.start = 0;//From 0 degree to 360 degree ,This is a circle
this.end = 360;
borderColor = Color.black;
fillColor = Color.white;
}
public void setEndPoint(int x2, int y2)
{
width = x2 - this.x;//Math.abs()?
height = y2 - this.y;
}
public void setBorderColor(Color borderColor) {this.borderColor = borderColor;}
public void setFillColor(Color fillColor) {this.fillColor = fillColor;}
//abstract public void draw(Graphics g);
public void draw(Graphics g)
{
g.setColor(borderColor);
g.drawArc(x, y, width, height, start, end);
g.setColor(fillColor);
//g.fillArc(x, y, width, height, start, end);//DUPLICATED?
}
//public boolean selected();
public boolean selected()
{
return true;
}
//public boolean resize();
public boolean resize()
{
return true;
}
}
//ShapeLine
class ShapeLine extends AllShape
{
private int x1, y1, x2, y2;//startx, start y, end x , end y
private Color borderColor;
public ShapeLine(int x1, int y1)
{
this.x1 = x1;
this.y1 = y1;
x2 = 0;
y2 = 0;
borderColor = Color.pink;
}
public ShapeLine(int x1, int y1, int x2, int y2)
{
this.x1 = x1;
this.y1 = y1;
this.x2 = x2;
this.y2 = y2;
borderColor = Color.black;
}
public void setEndPoint(int x2, int y2)
{
this.x2 = x2;
this.y2 = y2;
}
public void setBorderColor(Color borderColor){this.borderColor = borderColor;}
public void setFillColor(Color fillColor){}
//abstract public void draw(Graphics g);
public void draw(Graphics g)
{
g.setColor(borderColor);
g.drawLine(x1, y1, x2, y2);
}
//public boolean selected();
public boolean selected()
{
return true;
}
//public boolean resize();
public boolean resize()
{
return true;
}
public void report(String shownText)
{
JFrame noFrame = null;
new MyDialog(noFrame,shownText).show();
}//report
}
//ShapeRect
class ShapeRect extends AllShape
{
private int x1, y1, width, height;//start x, start y, width ,height
private Color borderColor, fillColor;
public ShapeRect(int x1, int y1)
{
this.x1 = x1;
this.y1 = y1;
width = 0;
height = 0;
borderColor = Color.black;
fillColor = Color.white;
}
public ShapeRect(int x1, int y1, int width, int height)
{
this.x1 = x1;
this.y1 = y1;
this.width = width; //abs???
this.height = height; //abs???
borderColor = Color.black;
fillColor = Color.white;
}
public void setEndPoint(int x2, int y2)
{
width = x2 - this.x1;//Math.abs()?
height = y2 - this.y1;
}
//abstract public void draw(Graphics g);
public void setBorderColor(Color borderColor) {this.borderColor = borderColor;}
public void setFillColor(Color fillColor) {this.fillColor = fillColor;}
public void draw(Graphics g)
{
g.setColor(borderColor);
g.drawRect(x1, y1, width, height);
g.setColor(fillColor);
//g.fileRect(x1, y1, width, height);//Whether it is duplicated?
}
//public boolean selected();
public boolean selected()
{
return true;
}
//public boolean resize();
public boolean resize()
{
return true;
}
}
import java.io.Serializable.*;//needed or not?
import java.io.*;
import java.awt.*;
abstract public class AllShape implements Serializable
{
abstract public void draw(Graphics g);
abstract public void setBorderColor(Color borderColor);
abstract public void setFillColor(Color fillColor);
//Check whether the shape has been selected
abstract public boolean selected();
//Resize the selected shapes,
//Succeed with true returned ,while false when go out of bound;
abstract public boolean resize();
abstract public void setEndPoint(int x2, int y2);
}
//ShapeCircle.java
class ShapeCircle extends AllShape
{
//xcoordinate, ycoordinate, width, height, start angle, end angle;
private int x, y, width, height, start, end;
private Color borderColor, fillColor;
public ShapeCircle(int x, int y)
{
this.x = x;
this.y = y;
width = 0;
height = 0;
this.start = 0;//From 0 degree to 360 degree ,This is a circle
this.end = 360;
borderColor = Color.black;
fillColor = Color.white;
}
public ShapeCircle(int x, int y, int width, int height, int start, int end)
{
this.x=x;
this.y=y;
this.width =width;
this.height =height;
this.start = 0;//From 0 degree to 360 degree ,This is a circle
this.end = 360;
borderColor = Color.black;
fillColor = Color.white;
}
public void setEndPoint(int x2, int y2)
{
width = x2 - this.x;//Math.abs()?
height = y2 - this.y;
}
public void setBorderColor(Color borderColor) {this.borderColor = borderColor;}
public void setFillColor(Color fillColor) {this.fillColor = fillColor;}
//abstract public void draw(Graphics g);
public void draw(Graphics g)
{
g.setColor(borderColor);
g.drawArc(x, y, width, height, start, end);
g.setColor(fillColor);
//g.fillArc(x, y, width, height, start, end);//DUPLICATED?
}
//public boolean selected();
public boolean selected()
{
return true;
}
//public boolean resize();
public boolean resize()
{
return true;
}
}
//ShapeLine
class ShapeLine extends AllShape
{
private int x1, y1, x2, y2;//startx, start y, end x , end y
private Color borderColor;
public ShapeLine(int x1, int y1)
{
this.x1 = x1;
this.y1 = y1;
x2 = 0;
y2 = 0;
borderColor = Color.pink;
}
public ShapeLine(int x1, int y1, int x2, int y2)
{
this.x1 = x1;
this.y1 = y1;
this.x2 = x2;
this.y2 = y2;
borderColor = Color.black;
}
public void setEndPoint(int x2, int y2)
{
this.x2 = x2;
this.y2 = y2;
}
public void setBorderColor(Color borderColor){this.borderColor = borderColor;}
public void setFillColor(Color fillColor){}
//abstract public void draw(Graphics g);
public void draw(Graphics g)
{
g.setColor(borderColor);
g.drawLine(x1, y1, x2, y2);
}
//public boolean selected();
public boolean selected()
{
return true;
}
//public boolean resize();
public boolean resize()
{
return true;
}
public void report(String shownText)
{
JFrame noFrame = null;
new MyDialog(noFrame,shownText).show();
}//report
}
//ShapeRect
class ShapeRect extends AllShape
{
private int x1, y1, width, height;//start x, start y, width ,height
private Color borderColor, fillColor;
public ShapeRect(int x1, int y1)
{
this.x1 = x1;
this.y1 = y1;
width = 0;
height = 0;
borderColor = Color.black;
fillColor = Color.white;
}
public ShapeRect(int x1, int y1, int width, int height)
{
this.x1 = x1;
this.y1 = y1;
this.width = width; //abs???
this.height = height; //abs???
borderColor = Color.black;
fillColor = Color.white;
}
public void setEndPoint(int x2, int y2)
{
width = x2 - this.x1;//Math.abs()?
height = y2 - this.y1;
}
//abstract public void draw(Graphics g);
public void setBorderColor(Color borderColor) {this.borderColor = borderColor;}
public void setFillColor(Color fillColor) {this.fillColor = fillColor;}
public void draw(Graphics g)
{
g.setColor(borderColor);
g.drawRect(x1, y1, width, height);
g.setColor(fillColor);
//g.fileRect(x1, y1, width, height);//Whether it is duplicated?
}
//public boolean selected();
public boolean selected()
{
return true;
}
//public boolean resize();
public boolean resize()
{
return true;
}
}
#3
//: com:bruceeckel:swing:Console.java这里就是那个com.bruceeckel.swing.*;的类库的来源
// Tool for running Swing demos from the
// console, both applets and JFrames.
// From 'Thinking in Java, 3rd ed.' (c) Bruce Eckel 2002
// www.BruceEckel.com. See copyright notice in CopyRight.txt.
package com.bruceeckel.swing;
import javax.swing.*;
import java.awt.event.*;
public class Console {
// Create a title string from the class name:
public static String title(Object o) {
String t = o.getClass().toString();
// Remove the word "class":
if(t.indexOf("class") != -1)
t = t.substring(6);
return t;
}
public static void
run(JFrame frame, int width, int height) {
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(width, height);
frame.setVisible(true);
}
public static void
run(JApplet applet, int width, int height) {
JFrame frame = new JFrame(title(applet));
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.getContentPane().add(applet);
frame.setSize(width, height);
applet.init();
applet.start();
frame.setVisible(true);
}
public static void
run(JPanel panel, int width, int height) {
JFrame frame = new JFrame(title(panel));
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.getContentPane().add(panel);
frame.setSize(width, height);
frame.setVisible(true);
}
} ///:~
// Tool for running Swing demos from the
// console, both applets and JFrames.
// From 'Thinking in Java, 3rd ed.' (c) Bruce Eckel 2002
// www.BruceEckel.com. See copyright notice in CopyRight.txt.
package com.bruceeckel.swing;
import javax.swing.*;
import java.awt.event.*;
public class Console {
// Create a title string from the class name:
public static String title(Object o) {
String t = o.getClass().toString();
// Remove the word "class":
if(t.indexOf("class") != -1)
t = t.substring(6);
return t;
}
public static void
run(JFrame frame, int width, int height) {
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(width, height);
frame.setVisible(true);
}
public static void
run(JApplet applet, int width, int height) {
JFrame frame = new JFrame(title(applet));
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.getContentPane().add(applet);
frame.setSize(width, height);
applet.init();
applet.start();
frame.setVisible(true);
}
public static void
run(JPanel panel, int width, int height) {
JFrame frame = new JFrame(title(panel));
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.getContentPane().add(panel);
frame.setSize(width, height);
frame.setVisible(true);
}
} ///:~
#4
我一般用JPanel,不会被Menu档住
#5
up
#6
把复杂的事变简单了,是一种贡献。
#7
JPanel 的确不会被挡住,但是就没有更好的解决办法了吗?
#8
重量组件的特点就是窗口不透明,不能生成透明的区域,也只能是矩形外观
轻量组件可以合并透明的区域
Canvas是重量组件,而JMenuBar却是轻量组件
两者相互混合使用,很容易出现楼主说的覆盖的问题
这是没有办法解决的
唯一的建议就是不要把轻量组件和重量组件混合使用
轻量组件可以合并透明的区域
Canvas是重量组件,而JMenuBar却是轻量组件
两者相互混合使用,很容易出现楼主说的覆盖的问题
这是没有办法解决的
唯一的建议就是不要把轻量组件和重量组件混合使用
#9
谢谢大家的回答,请下面的两位高手到
http://expert.csdn.net/Expert/topic/2410/2410008.xml?temp=.6915399
我来结帐,谢谢!
不过,在我把Canvas改成了JPanel以后,却又出现了这样一种情况:
我在自己的类
class MyCanvas extends JPanel
{
public void paint(Graphics g)
{
setBackground(Color.white);
}
public MyCanvas()//构造函数
{
setBackground(Color.white);
}
}
这两句设置背景色的函数好象没有效果,不知道这是为什么?再次对大家表示感谢!
kypfos(我不是深圳人,只爱我家)
LoveRose(旺旺) ( )
http://expert.csdn.net/Expert/topic/2410/2410008.xml?temp=.6915399
我来结帐,谢谢!
不过,在我把Canvas改成了JPanel以后,却又出现了这样一种情况:
我在自己的类
class MyCanvas extends JPanel
{
public void paint(Graphics g)
{
setBackground(Color.white);
}
public MyCanvas()//构造函数
{
setBackground(Color.white);
}
}
这两句设置背景色的函数好象没有效果,不知道这是为什么?再次对大家表示感谢!
kypfos(我不是深圳人,只爱我家)
LoveRose(旺旺) ( )
#1
/**Paint.java
*@author Pilgrim
*@version 1.0
*@date 03-10-27 8:01
*/
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.util.*;
import com.bruceeckel.swing.*;
import java.io.*;
public class Paint extends JApplet
{
//Make sure that there is only one Applet
private static int flag;//1 for line, 2 for rect, 3 for circle
public static int getFlag(){return flag;}//function used to encapsulate the flag;
JMenuBar mainMenu = new JMenuBar();
JMenu file = new JMenu("File");
JMenu edit = new JMenu("Edit");
JMenu draw = new JMenu("Draw");
String[] control = {"Open","Save","Exit","Copy","Paste","Delete"};
JMenuItem[] fileMenu =
{
new JMenuItem(control[0], KeyEvent.VK_O),
new JMenuItem(control[1], KeyEvent.VK_S),
new JMenuItem(control[2], KeyEvent.VK_E)
};
JMenuItem[] editMenu =
{
new JMenuItem(control[3], KeyEvent.VK_C),
new JMenuItem(control[4], KeyEvent.VK_P),
new JMenuItem(control[5], KeyEvent.VK_D)
};
JMenuItem[] drawMenu =
{
new JMenuItem("Line", KeyEvent.VK_L),
new JMenuItem("Rect", KeyEvent.VK_R),
new JMenuItem("Circle", KeyEvent.VK_C)
};
JToolBar operation = new JToolBar();
//An array to contain the drawn shapes
private static ShapeList saveShapes = new ShapeList();
public static ShapeList getShapeList(){return saveShapes;}
//Canvas to be drawn on;
MyCanvas canvas = new MyCanvas();//Canvas.paint();function need to be overrided
public void init()
{
//initializing the mainMenu
file.setMnemonic(KeyEvent.VK_F);
edit.setMnemonic(KeyEvent.VK_E);
draw.setMnemonic(KeyEvent.VK_D);
mainMenu.add(file);
mainMenu.add(edit);
mainMenu.add(draw);
MenuListener mListener = new MenuListener();
for(int i = 0; i < fileMenu.length; i++)
{
fileMenu[i].addActionListener(mListener);
file.add(fileMenu[i]);
}
for(int i = 0; i < editMenu.length; i++)
{
editMenu[i].addActionListener(mListener);
edit.add(editMenu[i]);
}
for(int i = 0; i < drawMenu.length; i++)
{
drawMenu[i].addActionListener(mListener);
draw.add(drawMenu[i]);
}
for(int i = 0; i < control.length; i++)
{
operation.add(new JButton(control[i]));
if((i+1) % 3 == 0)
operation.addSeparator();
}
setJMenuBar(mainMenu);
//Set layout of the pane
Container cp = getContentPane();
//cp.setLayout(new BorderLayout());
cp.add(operation,BorderLayout.NORTH);
cp.add(canvas,BorderLayout.CENTER);
}//init()
public void report(String shownText)
{
JFrame noFrame = null;
new MyDialog(noFrame,shownText).show();
}//report
public class MenuListener implements ActionListener
{
public void actionPerformed(ActionEvent e)
{
JMenuItem target = (JMenuItem)e.getSource();
String actionCommand = target.getActionCommand();
//Waitint to be implemented
//Perform action to draw on the
if(actionCommand.equals("Line"))
flag = 1;
else if(actionCommand.equals("Rect"))
flag = 2;
else if(actionCommand.equals("Circle"))
flag = 3;
}
};//semicolon needed//inner class MenuListener
public static void main(String[] args)
{
Console.run(new Paint(), 600, 450);
}
}
class ShapeList extends ArrayList
{
public AllShape getElem(int index)
{
return (AllShape)get(index);
}
public AllShape removeElem(int index)
{
return (AllShape)remove(index);
}
}
class MyCanvas extends Canvas
{
ShapeList saveShapes;
int flag;
Graphics graphics;
public void paint(Graphics g)
{
graphics = g;
g.drawRect(0, 0, getWidth() - 1, getHeight() - 1);
int size = saveShapes.size();
for(int i = 0; i < size; i++)
saveShapes.getElem(i).draw(g);
}//Method paint()
public MyCanvas()
{
//report(graphics.toString());//!!!!!!!!!!!!
flag = Paint.getFlag();
setBackground(Color.white);
saveShapes = Paint.getShapeList();
//report(saveShapes.toString());//I failed to get any referance;
AllMouseListener allListener = new AllMouseListener();
addMouseListener(allListener);
addMouseMotionListener(allListener);
}//Constructor
class AllMouseListener implements MouseListener, MouseMotionListener
{
Point startPoint;
AllShape current;
public void mousePressed(MouseEvent e)
{
startPoint = e.getPoint();
flag = Paint.getFlag();
switch(flag)
{
case 1:
graphics.drawLine(100,200,300,400);
current = new ShapeLine(startPoint.x, startPoint.y);
break;
case 2:
current = new ShapeRect(startPoint.x, startPoint.y);
break;
case 3:
current = new ShapeCircle(startPoint.x, startPoint.y);
break;
default://Change the status bar to notify the user;
}
}//Press
public void mouseDragged(MouseEvent e)
{
//Xpos & Ypos in the status bar changes
// drawing a temporary shape
if(flag != 0)//about to draw
{
current.setEndPoint(e.getX(), e.getY());
current.draw(graphics);
}
}//Drag
public void mouseReleased(MouseEvent e)
{
if(flag != 0)//about to draw
{
current.setEndPoint(e.getX(), e.getY());
//update(graphics);
current.draw(graphics);
saveShapes.add(current);
}
}//Release
//Next methos is of no use
public void mouseClicked(MouseEvent e) {}
public void mouseEntered(MouseEvent e) {}
public void mouseExited(MouseEvent e) {}
public void mouseMoved(MouseEvent e)
{
//Xpos & Ypos in the status bar changes
}
} //inner class Listener
public void report(String shownText)
{
JFrame noFrame = null;
new MyDialog(noFrame,shownText).show();
}//report
}//class MyCanvas
class MyDialog extends JDialog
{
public MyDialog(JFrame parent,String shownText)
{
super(parent, "Report", false);
Container cp = getContentPane();
cp.setLayout(new BorderLayout());
cp.add(new JLabel(shownText, JLabel.CENTER), BorderLayout.NORTH);
JButton ok = new JButton("OK");
ok.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
dispose(); // Closes the dialog
}
});
cp.add(ok, BorderLayout.SOUTH);
setSize(200,150);
Dimension d = Toolkit.getDefaultToolkit().getScreenSize();
setLocation((d.width - getSize().width) / 2, (d.height - getSize().height) / 2);
}
}
*@author Pilgrim
*@version 1.0
*@date 03-10-27 8:01
*/
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.util.*;
import com.bruceeckel.swing.*;
import java.io.*;
public class Paint extends JApplet
{
//Make sure that there is only one Applet
private static int flag;//1 for line, 2 for rect, 3 for circle
public static int getFlag(){return flag;}//function used to encapsulate the flag;
JMenuBar mainMenu = new JMenuBar();
JMenu file = new JMenu("File");
JMenu edit = new JMenu("Edit");
JMenu draw = new JMenu("Draw");
String[] control = {"Open","Save","Exit","Copy","Paste","Delete"};
JMenuItem[] fileMenu =
{
new JMenuItem(control[0], KeyEvent.VK_O),
new JMenuItem(control[1], KeyEvent.VK_S),
new JMenuItem(control[2], KeyEvent.VK_E)
};
JMenuItem[] editMenu =
{
new JMenuItem(control[3], KeyEvent.VK_C),
new JMenuItem(control[4], KeyEvent.VK_P),
new JMenuItem(control[5], KeyEvent.VK_D)
};
JMenuItem[] drawMenu =
{
new JMenuItem("Line", KeyEvent.VK_L),
new JMenuItem("Rect", KeyEvent.VK_R),
new JMenuItem("Circle", KeyEvent.VK_C)
};
JToolBar operation = new JToolBar();
//An array to contain the drawn shapes
private static ShapeList saveShapes = new ShapeList();
public static ShapeList getShapeList(){return saveShapes;}
//Canvas to be drawn on;
MyCanvas canvas = new MyCanvas();//Canvas.paint();function need to be overrided
public void init()
{
//initializing the mainMenu
file.setMnemonic(KeyEvent.VK_F);
edit.setMnemonic(KeyEvent.VK_E);
draw.setMnemonic(KeyEvent.VK_D);
mainMenu.add(file);
mainMenu.add(edit);
mainMenu.add(draw);
MenuListener mListener = new MenuListener();
for(int i = 0; i < fileMenu.length; i++)
{
fileMenu[i].addActionListener(mListener);
file.add(fileMenu[i]);
}
for(int i = 0; i < editMenu.length; i++)
{
editMenu[i].addActionListener(mListener);
edit.add(editMenu[i]);
}
for(int i = 0; i < drawMenu.length; i++)
{
drawMenu[i].addActionListener(mListener);
draw.add(drawMenu[i]);
}
for(int i = 0; i < control.length; i++)
{
operation.add(new JButton(control[i]));
if((i+1) % 3 == 0)
operation.addSeparator();
}
setJMenuBar(mainMenu);
//Set layout of the pane
Container cp = getContentPane();
//cp.setLayout(new BorderLayout());
cp.add(operation,BorderLayout.NORTH);
cp.add(canvas,BorderLayout.CENTER);
}//init()
public void report(String shownText)
{
JFrame noFrame = null;
new MyDialog(noFrame,shownText).show();
}//report
public class MenuListener implements ActionListener
{
public void actionPerformed(ActionEvent e)
{
JMenuItem target = (JMenuItem)e.getSource();
String actionCommand = target.getActionCommand();
//Waitint to be implemented
//Perform action to draw on the
if(actionCommand.equals("Line"))
flag = 1;
else if(actionCommand.equals("Rect"))
flag = 2;
else if(actionCommand.equals("Circle"))
flag = 3;
}
};//semicolon needed//inner class MenuListener
public static void main(String[] args)
{
Console.run(new Paint(), 600, 450);
}
}
class ShapeList extends ArrayList
{
public AllShape getElem(int index)
{
return (AllShape)get(index);
}
public AllShape removeElem(int index)
{
return (AllShape)remove(index);
}
}
class MyCanvas extends Canvas
{
ShapeList saveShapes;
int flag;
Graphics graphics;
public void paint(Graphics g)
{
graphics = g;
g.drawRect(0, 0, getWidth() - 1, getHeight() - 1);
int size = saveShapes.size();
for(int i = 0; i < size; i++)
saveShapes.getElem(i).draw(g);
}//Method paint()
public MyCanvas()
{
//report(graphics.toString());//!!!!!!!!!!!!
flag = Paint.getFlag();
setBackground(Color.white);
saveShapes = Paint.getShapeList();
//report(saveShapes.toString());//I failed to get any referance;
AllMouseListener allListener = new AllMouseListener();
addMouseListener(allListener);
addMouseMotionListener(allListener);
}//Constructor
class AllMouseListener implements MouseListener, MouseMotionListener
{
Point startPoint;
AllShape current;
public void mousePressed(MouseEvent e)
{
startPoint = e.getPoint();
flag = Paint.getFlag();
switch(flag)
{
case 1:
graphics.drawLine(100,200,300,400);
current = new ShapeLine(startPoint.x, startPoint.y);
break;
case 2:
current = new ShapeRect(startPoint.x, startPoint.y);
break;
case 3:
current = new ShapeCircle(startPoint.x, startPoint.y);
break;
default://Change the status bar to notify the user;
}
}//Press
public void mouseDragged(MouseEvent e)
{
//Xpos & Ypos in the status bar changes
// drawing a temporary shape
if(flag != 0)//about to draw
{
current.setEndPoint(e.getX(), e.getY());
current.draw(graphics);
}
}//Drag
public void mouseReleased(MouseEvent e)
{
if(flag != 0)//about to draw
{
current.setEndPoint(e.getX(), e.getY());
//update(graphics);
current.draw(graphics);
saveShapes.add(current);
}
}//Release
//Next methos is of no use
public void mouseClicked(MouseEvent e) {}
public void mouseEntered(MouseEvent e) {}
public void mouseExited(MouseEvent e) {}
public void mouseMoved(MouseEvent e)
{
//Xpos & Ypos in the status bar changes
}
} //inner class Listener
public void report(String shownText)
{
JFrame noFrame = null;
new MyDialog(noFrame,shownText).show();
}//report
}//class MyCanvas
class MyDialog extends JDialog
{
public MyDialog(JFrame parent,String shownText)
{
super(parent, "Report", false);
Container cp = getContentPane();
cp.setLayout(new BorderLayout());
cp.add(new JLabel(shownText, JLabel.CENTER), BorderLayout.NORTH);
JButton ok = new JButton("OK");
ok.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
dispose(); // Closes the dialog
}
});
cp.add(ok, BorderLayout.SOUTH);
setSize(200,150);
Dimension d = Toolkit.getDefaultToolkit().getScreenSize();
setLocation((d.width - getSize().width) / 2, (d.height - getSize().height) / 2);
}
}
#2
//AllShape.java
import java.io.Serializable.*;//needed or not?
import java.io.*;
import java.awt.*;
abstract public class AllShape implements Serializable
{
abstract public void draw(Graphics g);
abstract public void setBorderColor(Color borderColor);
abstract public void setFillColor(Color fillColor);
//Check whether the shape has been selected
abstract public boolean selected();
//Resize the selected shapes,
//Succeed with true returned ,while false when go out of bound;
abstract public boolean resize();
abstract public void setEndPoint(int x2, int y2);
}
//ShapeCircle.java
class ShapeCircle extends AllShape
{
//xcoordinate, ycoordinate, width, height, start angle, end angle;
private int x, y, width, height, start, end;
private Color borderColor, fillColor;
public ShapeCircle(int x, int y)
{
this.x = x;
this.y = y;
width = 0;
height = 0;
this.start = 0;//From 0 degree to 360 degree ,This is a circle
this.end = 360;
borderColor = Color.black;
fillColor = Color.white;
}
public ShapeCircle(int x, int y, int width, int height, int start, int end)
{
this.x=x;
this.y=y;
this.width =width;
this.height =height;
this.start = 0;//From 0 degree to 360 degree ,This is a circle
this.end = 360;
borderColor = Color.black;
fillColor = Color.white;
}
public void setEndPoint(int x2, int y2)
{
width = x2 - this.x;//Math.abs()?
height = y2 - this.y;
}
public void setBorderColor(Color borderColor) {this.borderColor = borderColor;}
public void setFillColor(Color fillColor) {this.fillColor = fillColor;}
//abstract public void draw(Graphics g);
public void draw(Graphics g)
{
g.setColor(borderColor);
g.drawArc(x, y, width, height, start, end);
g.setColor(fillColor);
//g.fillArc(x, y, width, height, start, end);//DUPLICATED?
}
//public boolean selected();
public boolean selected()
{
return true;
}
//public boolean resize();
public boolean resize()
{
return true;
}
}
//ShapeLine
class ShapeLine extends AllShape
{
private int x1, y1, x2, y2;//startx, start y, end x , end y
private Color borderColor;
public ShapeLine(int x1, int y1)
{
this.x1 = x1;
this.y1 = y1;
x2 = 0;
y2 = 0;
borderColor = Color.pink;
}
public ShapeLine(int x1, int y1, int x2, int y2)
{
this.x1 = x1;
this.y1 = y1;
this.x2 = x2;
this.y2 = y2;
borderColor = Color.black;
}
public void setEndPoint(int x2, int y2)
{
this.x2 = x2;
this.y2 = y2;
}
public void setBorderColor(Color borderColor){this.borderColor = borderColor;}
public void setFillColor(Color fillColor){}
//abstract public void draw(Graphics g);
public void draw(Graphics g)
{
g.setColor(borderColor);
g.drawLine(x1, y1, x2, y2);
}
//public boolean selected();
public boolean selected()
{
return true;
}
//public boolean resize();
public boolean resize()
{
return true;
}
public void report(String shownText)
{
JFrame noFrame = null;
new MyDialog(noFrame,shownText).show();
}//report
}
//ShapeRect
class ShapeRect extends AllShape
{
private int x1, y1, width, height;//start x, start y, width ,height
private Color borderColor, fillColor;
public ShapeRect(int x1, int y1)
{
this.x1 = x1;
this.y1 = y1;
width = 0;
height = 0;
borderColor = Color.black;
fillColor = Color.white;
}
public ShapeRect(int x1, int y1, int width, int height)
{
this.x1 = x1;
this.y1 = y1;
this.width = width; //abs???
this.height = height; //abs???
borderColor = Color.black;
fillColor = Color.white;
}
public void setEndPoint(int x2, int y2)
{
width = x2 - this.x1;//Math.abs()?
height = y2 - this.y1;
}
//abstract public void draw(Graphics g);
public void setBorderColor(Color borderColor) {this.borderColor = borderColor;}
public void setFillColor(Color fillColor) {this.fillColor = fillColor;}
public void draw(Graphics g)
{
g.setColor(borderColor);
g.drawRect(x1, y1, width, height);
g.setColor(fillColor);
//g.fileRect(x1, y1, width, height);//Whether it is duplicated?
}
//public boolean selected();
public boolean selected()
{
return true;
}
//public boolean resize();
public boolean resize()
{
return true;
}
}
import java.io.Serializable.*;//needed or not?
import java.io.*;
import java.awt.*;
abstract public class AllShape implements Serializable
{
abstract public void draw(Graphics g);
abstract public void setBorderColor(Color borderColor);
abstract public void setFillColor(Color fillColor);
//Check whether the shape has been selected
abstract public boolean selected();
//Resize the selected shapes,
//Succeed with true returned ,while false when go out of bound;
abstract public boolean resize();
abstract public void setEndPoint(int x2, int y2);
}
//ShapeCircle.java
class ShapeCircle extends AllShape
{
//xcoordinate, ycoordinate, width, height, start angle, end angle;
private int x, y, width, height, start, end;
private Color borderColor, fillColor;
public ShapeCircle(int x, int y)
{
this.x = x;
this.y = y;
width = 0;
height = 0;
this.start = 0;//From 0 degree to 360 degree ,This is a circle
this.end = 360;
borderColor = Color.black;
fillColor = Color.white;
}
public ShapeCircle(int x, int y, int width, int height, int start, int end)
{
this.x=x;
this.y=y;
this.width =width;
this.height =height;
this.start = 0;//From 0 degree to 360 degree ,This is a circle
this.end = 360;
borderColor = Color.black;
fillColor = Color.white;
}
public void setEndPoint(int x2, int y2)
{
width = x2 - this.x;//Math.abs()?
height = y2 - this.y;
}
public void setBorderColor(Color borderColor) {this.borderColor = borderColor;}
public void setFillColor(Color fillColor) {this.fillColor = fillColor;}
//abstract public void draw(Graphics g);
public void draw(Graphics g)
{
g.setColor(borderColor);
g.drawArc(x, y, width, height, start, end);
g.setColor(fillColor);
//g.fillArc(x, y, width, height, start, end);//DUPLICATED?
}
//public boolean selected();
public boolean selected()
{
return true;
}
//public boolean resize();
public boolean resize()
{
return true;
}
}
//ShapeLine
class ShapeLine extends AllShape
{
private int x1, y1, x2, y2;//startx, start y, end x , end y
private Color borderColor;
public ShapeLine(int x1, int y1)
{
this.x1 = x1;
this.y1 = y1;
x2 = 0;
y2 = 0;
borderColor = Color.pink;
}
public ShapeLine(int x1, int y1, int x2, int y2)
{
this.x1 = x1;
this.y1 = y1;
this.x2 = x2;
this.y2 = y2;
borderColor = Color.black;
}
public void setEndPoint(int x2, int y2)
{
this.x2 = x2;
this.y2 = y2;
}
public void setBorderColor(Color borderColor){this.borderColor = borderColor;}
public void setFillColor(Color fillColor){}
//abstract public void draw(Graphics g);
public void draw(Graphics g)
{
g.setColor(borderColor);
g.drawLine(x1, y1, x2, y2);
}
//public boolean selected();
public boolean selected()
{
return true;
}
//public boolean resize();
public boolean resize()
{
return true;
}
public void report(String shownText)
{
JFrame noFrame = null;
new MyDialog(noFrame,shownText).show();
}//report
}
//ShapeRect
class ShapeRect extends AllShape
{
private int x1, y1, width, height;//start x, start y, width ,height
private Color borderColor, fillColor;
public ShapeRect(int x1, int y1)
{
this.x1 = x1;
this.y1 = y1;
width = 0;
height = 0;
borderColor = Color.black;
fillColor = Color.white;
}
public ShapeRect(int x1, int y1, int width, int height)
{
this.x1 = x1;
this.y1 = y1;
this.width = width; //abs???
this.height = height; //abs???
borderColor = Color.black;
fillColor = Color.white;
}
public void setEndPoint(int x2, int y2)
{
width = x2 - this.x1;//Math.abs()?
height = y2 - this.y1;
}
//abstract public void draw(Graphics g);
public void setBorderColor(Color borderColor) {this.borderColor = borderColor;}
public void setFillColor(Color fillColor) {this.fillColor = fillColor;}
public void draw(Graphics g)
{
g.setColor(borderColor);
g.drawRect(x1, y1, width, height);
g.setColor(fillColor);
//g.fileRect(x1, y1, width, height);//Whether it is duplicated?
}
//public boolean selected();
public boolean selected()
{
return true;
}
//public boolean resize();
public boolean resize()
{
return true;
}
}
#3
//: com:bruceeckel:swing:Console.java这里就是那个com.bruceeckel.swing.*;的类库的来源
// Tool for running Swing demos from the
// console, both applets and JFrames.
// From 'Thinking in Java, 3rd ed.' (c) Bruce Eckel 2002
// www.BruceEckel.com. See copyright notice in CopyRight.txt.
package com.bruceeckel.swing;
import javax.swing.*;
import java.awt.event.*;
public class Console {
// Create a title string from the class name:
public static String title(Object o) {
String t = o.getClass().toString();
// Remove the word "class":
if(t.indexOf("class") != -1)
t = t.substring(6);
return t;
}
public static void
run(JFrame frame, int width, int height) {
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(width, height);
frame.setVisible(true);
}
public static void
run(JApplet applet, int width, int height) {
JFrame frame = new JFrame(title(applet));
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.getContentPane().add(applet);
frame.setSize(width, height);
applet.init();
applet.start();
frame.setVisible(true);
}
public static void
run(JPanel panel, int width, int height) {
JFrame frame = new JFrame(title(panel));
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.getContentPane().add(panel);
frame.setSize(width, height);
frame.setVisible(true);
}
} ///:~
// Tool for running Swing demos from the
// console, both applets and JFrames.
// From 'Thinking in Java, 3rd ed.' (c) Bruce Eckel 2002
// www.BruceEckel.com. See copyright notice in CopyRight.txt.
package com.bruceeckel.swing;
import javax.swing.*;
import java.awt.event.*;
public class Console {
// Create a title string from the class name:
public static String title(Object o) {
String t = o.getClass().toString();
// Remove the word "class":
if(t.indexOf("class") != -1)
t = t.substring(6);
return t;
}
public static void
run(JFrame frame, int width, int height) {
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(width, height);
frame.setVisible(true);
}
public static void
run(JApplet applet, int width, int height) {
JFrame frame = new JFrame(title(applet));
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.getContentPane().add(applet);
frame.setSize(width, height);
applet.init();
applet.start();
frame.setVisible(true);
}
public static void
run(JPanel panel, int width, int height) {
JFrame frame = new JFrame(title(panel));
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.getContentPane().add(panel);
frame.setSize(width, height);
frame.setVisible(true);
}
} ///:~
#4
我一般用JPanel,不会被Menu档住
#5
up
#6
把复杂的事变简单了,是一种贡献。
#7
JPanel 的确不会被挡住,但是就没有更好的解决办法了吗?
#8
重量组件的特点就是窗口不透明,不能生成透明的区域,也只能是矩形外观
轻量组件可以合并透明的区域
Canvas是重量组件,而JMenuBar却是轻量组件
两者相互混合使用,很容易出现楼主说的覆盖的问题
这是没有办法解决的
唯一的建议就是不要把轻量组件和重量组件混合使用
轻量组件可以合并透明的区域
Canvas是重量组件,而JMenuBar却是轻量组件
两者相互混合使用,很容易出现楼主说的覆盖的问题
这是没有办法解决的
唯一的建议就是不要把轻量组件和重量组件混合使用
#9
谢谢大家的回答,请下面的两位高手到
http://expert.csdn.net/Expert/topic/2410/2410008.xml?temp=.6915399
我来结帐,谢谢!
不过,在我把Canvas改成了JPanel以后,却又出现了这样一种情况:
我在自己的类
class MyCanvas extends JPanel
{
public void paint(Graphics g)
{
setBackground(Color.white);
}
public MyCanvas()//构造函数
{
setBackground(Color.white);
}
}
这两句设置背景色的函数好象没有效果,不知道这是为什么?再次对大家表示感谢!
kypfos(我不是深圳人,只爱我家)
LoveRose(旺旺) ( )
http://expert.csdn.net/Expert/topic/2410/2410008.xml?temp=.6915399
我来结帐,谢谢!
不过,在我把Canvas改成了JPanel以后,却又出现了这样一种情况:
我在自己的类
class MyCanvas extends JPanel
{
public void paint(Graphics g)
{
setBackground(Color.white);
}
public MyCanvas()//构造函数
{
setBackground(Color.white);
}
}
这两句设置背景色的函数好象没有效果,不知道这是为什么?再次对大家表示感谢!
kypfos(我不是深圳人,只爱我家)
LoveRose(旺旺) ( )