QQ登录

只需一步,快速开始

 找回密码
 注册

QQ登录

只需一步,快速开始

查看: 1438|回复: 2

关于swing

[复制链接]
发表于 2003-11-17 17:28:32 | 显示全部楼层 |阅读模式
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.io.*;

public class Mainframe extends JFrame {


JDesktopPane desktop;
Container contentPane;
JMenuBar MBar;
JToolBar toolBar;
//ButtonBar p1;
CanvasPanel p1=CanvasPanel.getButtonBar();
CanvasPanel p2=CanvasPanel.getReference();;
JSplitPane splitpane;
Graphics gp2;
int count=1;


//-----------------------------------
public Mainframe() {
super("Mainframe");
contentPane=getContentPane();
//contentPane.addMouseListener(this);
//contentPane.addMouseMotionListener(this);
setBounds(100,100,630,400);
buildContent();
buildMenu();
buildToolBar();
contentPane.add(toolBar,BorderLayout.NORTH);
//buildButton();


p1.setBackground(Color.white);
p2.setBackground(Color.white);
p2.addMouseListener(p2);
p2.addMouseMotionListener(p2);
p1.buildButton();

splitpane=new JSplitPane(JSplitPane.HORIZONTAL_SPLIT,false,p1,p2);
splitpane.setDividerLocation(50);
splitpane.setDividerSize(5);
contentPane.add(splitpane);
//gp2=p2.getGraphics();

addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {
System.exit(0);
}
});
setResizable(false);
setVisible(true);
}

//---------------------------------------
protected void buildContent() {
desktop=new JDesktopPane();
desktop.setBackground(Color.white);
contentPane.add(desktop);
}
//-----------------------------------
protected void buildMenu() {
MBar = new JMenuBar();
MBar.setOpaque(true);
JMenu mfile = buildFileMenu();
JMenu mhelp=buildHelpMenu();

MBar.add(mfile);
MBar.add(mhelp);
setJMenuBar(MBar);
}//end of bulidMenu()
//-----------------------------------
public JMenu buildFileMenu() {

JMenu file = new JMenu("File");
ToolBarAction tbaNewF=new ToolBarAction("new",new ImageIcon("scr/new.jpg"));
//JMenuItem newf = new JMenuItem("New");

//newf.setIcon(new ImageIcon("scr/new.jpg"));
ToolBarAction tbaExit=new ToolBarAction("exit",new ImageIcon("scr/close.gif"));
//JMenuItem quit = new JMenuItem("Exit");

file.add(tbaNewF);
//file.add(newf);
file.addSeparator();
file.add(tbaExit);
return file;
}//end of buildFileMenu()
public JMenu buildHelpMenu() {
JMenu help=new JMenu("Help");
ToolBarAction tbaHelp=new ToolBarAction("help",new ImageIcon("scr/help.gif"));
//JMenuItem help1=new JMenuItem("");
//help1.setIcon(new ImageIcon("scr/help.gif"));
ToolBarAction tbaVersion=new ToolBarAction("about Easy Painter",new ImageIcon("scr/exe.gif"));
//JMenuItem version=new JMenuItem("Version");
//version.setIcon(new ImageIcon("scr/exe.gif"));
help.add(tbaHelp);
help.addSeparator();
help.add(tbaVersion);
return help;
}//end of buildHelpMenu()
//----------------------------------

//----------------------------------
protected void buildToolBar() {
toolBar=new JToolBar();
toolBar.setFloatable(true);
ToolBarAction tbaNew=new ToolBarAction("新建",new ImageIcon("scr/new.jpg"));
ToolBarAction tbaExit=new ToolBarAction("退出",new ImageIcon("scr/close.gif"));
JButton jb;
jb=toolBar.add(tbaNew);
jb.setActionCommand("new");
jb.setToolTipText((String)tbaNew.getValue(Action.NAME));
jb=toolBar.add(tbaExit);
jb.setActionCommand("exit");
jb.setToolTipText((String)tbaExit.getValue(Action.NAME));
toolBar.addSeparator();

ToolBarAction tbaHelp=new ToolBarAction("帮助",new ImageIcon("scr/help.gif"));
ToolBarAction tbaVersion=new ToolBarAction("关于EasyPainter",new ImageIcon("scr/exe.gif"));
jb=toolBar.add(tbaHelp);
jb.setActionCommand("help");
jb.setToolTipText((String)tbaHelp.getValue(Action.NAME));
jb=toolBar.add(tbaVersion);
jb.setActionCommand("about Easy Painter");
jb.setToolTipText((String)tbaVersion.getValue(Action.NAME));
toolBar.addSeparator();

}
//-----------------------------------



//-------------------------------



public static void main(String[] args) {
new Mainframe();
}

class ToolBarAction extends AbstractAction{
public ToolBarAction(String name,Icon icon) {
super(name,icon);
}
public void actionPerformed(ActionEvent e) {
if(e.getActionCommand()=="exit") //以下是exit
{
final JFrame win=new JFrame();
Container contentPane=win.getContentPane();
//JDialog dialog=new JDialog(win,"离开",true);
//dialog.setSize(100,100);
//contentPane.add(dialog);
contentPane.setLayout(new BorderLayout());
JButton jb1=new JButton(" Exit ");
JButton jb2=new JButton("Cancel");
jb1.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
System.exit(0);
}
});
jb2.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
win.dispose();
}
});

JPanel p1=new JPanel();
contentPane.add(new JLabel("离开本系统",JLabel.CENTER),BorderLayout.NORTH);
contentPane.add(p1,BorderLayout.CENTER);
p1.setLayout(new FlowLayout());
p1.add(jb1);
p1.add(jb2);
win.setBounds(325,250,180,80);
//win.pack();
win.setVisible(true);
win.setResizable(false);
win.addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {
win.dispose();
}
});

//System.exit(0);
}
if(e.getActionCommand()=="new") {//此功能未能实现
JInternalFrame internalFrame=new JInternalFrame("Internal Frame"+(count++),true,true,true,true);
internalFrame.setLocation(20,20);
internalFrame.setSize(400,400);
internalFrame.setVisible(true);
p2.add(internalFrame);
//splitpane=new JSplitPane(JSplitPane.HORIZONTAL_SPLIT,false,p1,internalFrame);
try
{
internalFrame.setSelected(true);
}
catch (java.beans.PropertyVetoException ex)
{
System.out.println("Exception while selecting");
}
}
//以下是help
if(e.getActionCommand()=="help")
{
JEditorPane editPane=null;
try
{
File file=new File("scr/help.txt");
String str=file.getAbsolutePath();
str="file:"+str;
editPane=new JEditorPane();
editPane.setPage(str);

}
catch (IOException ee)
{
System.out.println("Error");
ee.printStackTrace();
}


final JFrame win=new JFrame("Help");
win.setContentPane(new JScrollPane(editPane));
//win.setVisible(true);
win.setBounds(170,100,450,450);
win.setVisible(true);
win.setResizable(false);
win.addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {
win.dispose();
}
});

//win.setSize(400,200);
}
//以下是about Easy Painter
if(e.getActionCommand()=="about Easy Painter")
{
//System.out.println(getWidth());
final JFrame win=new JFrame("About Easy Painter");
//Dimension size=getSize();
Container contentPane=win.getContentPane();
contentPane.setLayout(new GridLayout(4,1));
JLabel label=new JLabel(new ImageIcon("scr/logo.gif"),JLabel.CENTER);
JLabel label2=new JLabel("Welcome to using this software",JLabel.CENTER);
JLabel label3=new JLabel("CopyRight by www.webbug.net 2003",JLabel.CENTER);
JLabel label4=new JLabel("魏东亮 webmaster of webbug",JLabel.CENTER);
contentPane.add(label2);
contentPane.add(label);
contentPane.add(label3);
contentPane.add(label4);

win.setBounds(250,150,250,280);
win.setVisible(true);
win.setResizable(false);
win.addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {
win.dispose();
}
});
}
}
}
}
//----------------------------------------------------------------------
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

public class CanvasPanel extends JPanel implements MouseListener,MouseMotionListener
{
private static CanvasPanel p2=new CanvasPanel();
//p2.setDoubleBuffered(false);
private static CanvasPanel p1=new CanvasPanel();
private int x=0,y=0;
private int flag=0;
private int startX=0,startY=0,endX=0,endY=0;
public int getStartX() {return p2.startX;}
public int getStartY() {return p2.startY;}
public int getEndX() {return p2.endX;}
public int getEndY() {return p2.endY;}

public static CanvasPanel getReference() { return p2;}
public static CanvasPanel getButtonBar() { return p1;}
Shape1 sp=new Shape1();

Graphics gg;
private CanvasPanel() {
//setDoubleBuffered(false);
setEnabled(false);
}
JToolBar toolBar=new JToolBar(JToolBar.VERTICAL);

public void buildButton()
{
toolBar.setFloatable(false);
//setLayout(new GridLayout(7,1));
ButtonAction lineButton=new ButtonAction("直线",new ImageIcon("scr/line.gif"));
//lineButton.setBackground(Color.white);
//lineButton.setForeground(Color.red);
ButtonAction ovalButton=new ButtonAction("圆形",new ImageIcon("scr/oval.gif"));
//ovalButton.setBackground(Color.white);
ButtonAction rectButton=new ButtonAction("矩形",new ImageIcon("scr/rect.gif"));
//rectButton.setBackground(Color.white);
ButtonAction arcButton=new ButtonAction("圆弧",new ImageIcon("scr/arc.gif"));
//arcButton.setBackground(Color.white);
ButtonAction clipButton=new ButtonAction("剪切",new ImageIcon("scr/clip.gif"));
//clipButton.setBackground(Color.white);
JButton jb;
jb=toolBar.add(lineButton);
jb.setActionCommand("line");
jb.setToolTipText((String)lineButton.getValue(Action.NAME));
jb=toolBar.add(ovalButton);
jb.setActionCommand("oval");
jb.setToolTipText((String)ovalButton.getValue(Action.NAME));
jb=toolBar.add(rectButton);
jb.setActionCommand("rect");
jb.setToolTipText((String)rectButton.getValue(Action.NAME));
jb=toolBar.add(arcButton);
jb.setActionCommand("arc");
jb.setToolTipText((String)arcButton.getValue(Action.NAME));
jb=toolBar.add(clipButton);
jb.setActionCommand("clip");
jb.setToolTipText((String)clipButton.getValue(Action.NAME));
add(toolBar);

}
class ButtonAction extends AbstractAction{
public ButtonAction(String name,Icon icon) {
super(name,icon);
}
public void actionPerformed(ActionEvent e) {
if(e.getActionCommand()=="line") {
sp=new Line();
//System.out.println("Line");
//i=0;
}
if(e.getActionCommand()=="oval") {
sp=new Oval();
//System.out.println("Oval");
//i=1;
}
if(e.getActionCommand()=="rect") {
sp=new Rect();
}
if(e.getActionCommand()=="arc") {
//sp=new Arc();
}
if(e.getActionCommand()=="clip") {
sp=new Clip();
}
//else sp=new Line();
}
}

//Object a[]={new Line(),new Rect()};
public void paintComponent(Graphics g) {
super.paintComponent(g);
if(flag==1) {

//g.drawLine(p2.startX,p2.startY,p2.endX,p2.endY);
//sp=(Shape1)a;
//System.out.println(i);
sp.paintComponent(g,p2.startX,p2.startY,p2.endX,p2.endY);
}
if(flag==2) {
//g.drawLine(p2.startX,p2.startY,p2.x,p2.y);
//sp=(Shape1)a;
//System.out.println(i);
sp.paintComponent(g,p2.startX,p2.startY,p2.x,p2.y);
}
}

public static void main(String[] args)
{
//System.out.println("Hello World!");
}
public void update(Graphics g) {}
//public void paint(Graphics g) {}


public void mousePressed(MouseEvent e) {
p2.startX=e.getX();
p2.startY=e.getY();
}
public void mouseReleased(MouseEvent e) {
p2.endX=e.getX();
p2.endY=e.getY();
//System.out.println(ip.getEndX());
}
public void mouseEntered(MouseEvent e) {
}
public void mouseExited(MouseEvent e) {
}
public void mouseClicked(MouseEvent e) {
}
public void mouseMoved(MouseEvent e) {
flag=1;
gg=p2.getGraphics();
p2.x=e.getX();
p2.y=e.getY();
paintComponent(gg);

//repaint();
//setVisible(true);
}
public void mouseDragged(MouseEvent e) {
flag=2;
gg=p2.getGraphics();
p2.x=e.getX();
p2.y=e.getY();
paintComponent(gg);
//repaint();
//setVisible(true);
}

}
//--------------------------------------------------------------
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

public class Shape1 {
public void paintComponent(Graphics g) {}
public void paintComponent(Graphics g,int a,int b,int c,int d) {System.out.println("Shape1");}
}

//----------------------------------------------
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

public class Line extends Shape1 {
CanvasPanel p2=CanvasPanel.getReference();
int startX=0;
//int startY=ip.getStartY();
//int endX=ip.getEndX();
//int endY=ip.getEndY();
public void paintComponent(Graphics g,int a,int b,int c,int d) {
//startX=p2.getStartX();
g.drawLine(a,b,c,d);
}
public void paintComponent(Graphics g) {}

public static void main(String[] args) {
//new Line();
}

}

//--------------------------------------------------------
以上是部分的源代码
我发现在CanvasPanel类中,我无法改变sh这个引用
我原本的想法是动态改变(Shape1)sh的引用,如果我选的是画直线
那么sh就=new Line()
然后就调用sh.paintComponent()方法,实现多态
可是编译通过,执行的时候发现无法做到这一点
每次sh还是Shape1类的
我的表述不太好
但是希望大家能帮帮我!!!!
发表于 2003-11-18 10:35:09 | 显示全部楼层
你的代码里哪有sh?
回复

使用道具 举报

 楼主| 发表于 2003-11-20 17:48:50 | 显示全部楼层
不好意思亚,写错了
用了Singleton和simple factory解决问题了
不过出了新问题:Swing里面作图,每次重新画一次,原先画的都没了,我估计是每次Swing的JFrame有刷新机制
不知道怎么能取消这种机制???
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 注册

本版积分规则

GMT+8, 2024-11-23 03:44 , Processed in 0.065923 second(s), 15 queries .

© 2021 Powered by Discuz! X3.5.

快速回复 返回顶部 返回列表