| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 662 人关注过本帖
标题:[求助]我盲目了,看不出那里出问题了
只看楼主 加入收藏
ada518618
Rank: 1
等 级:新手上路
帖 子:184
专家分:0
注 册:2006-3-27
收藏
 问题点数:0 回复次数:4 
[求助]我盲目了,看不出那里出问题了

package Constrants;

import java.awt.Color;

public class SketcherConstrants {
public final static int LINE = 101;
public final static int RECTANGLE = 102;
public final static int CIRCLE = 103;
public final static int CURVE = 104;

public final static int DEFAULT_ELEMENT_TYPE = LINE;
public final static Color DEFAULT_ELEMENT_COLOR = Color.BLUE;
}

import javax.swing.JFrame;
import javax.swing.JButton;
import javax.swing.Action;
import javax.swing.AbstractAction;
import javax.swing.BorderFactory;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JToolBar;
import javax.swing.KeyStroke;
import javax.swing.ImageIcon;

import java.awt.event.ActionEvent;
import java.awt.event.InputEvent;

import java.awt.Color;
import java.awt.Container;
import java.awt.Toolkit;
import java.awt.Dimension;

import java.io.File;

import static Constrants.SketcherConstrants.*;
import static java.awt.Color.*;

public class Exercise1 extends JFrame{
public Exercise1(String title){
setTitle(title);
JMenu file = new JMenu("File");
file.setMnemonic('F');
JMenu element = new JMenu("Element");
element.setMnemonic('E');

newAction = new FileAction("New",KeyStroke.getKeyStroke('N',InputEvent.CTRL_DOWN_MASK),"Create new file");
openAction = new FileAction("Open",KeyStroke.getKeyStroke('O',InputEvent.CTRL_DOWN_MASK),"Open the file");
closeAction = new FileAction("Close",KeyStroke.getKeyStroke('C',InputEvent.CTRL_DOWN_MASK),"Close the file");
saveAction = new FileAction("Save",KeyStroke.getKeyStroke('S',InputEvent.CTRL_DOWN_MASK),"Save the file");
saveAsAction = new FileAction("Save as",KeyStroke.getKeyStroke('A',InputEvent.CTRL_DOWN_MASK),"Save as...");
printAction = new FileAction("Print",KeyStroke.getKeyStroke('P',InputEvent.CTRL_DOWN_MASK),"Print the file");
exitAction = new FileAction("Exit",KeyStroke.getKeyStroke('E',InputEvent.CTRL_DOWN_MASK),"Exit the program");

file.add(new JMenuItem(newAction)).setIcon(null);
file.addSeparator();
file.add(new JMenuItem(openAction)).setIcon(null);
file.add(new JMenuItem(closeAction)).setIcon(null);
file.addSeparator();
file.add(new JMenuItem(saveAction)).setIcon(null);
file.add(new JMenuItem(saveAsAction)).setIcon(null);
file.addSeparator();
file.add(new JMenuItem(printAction)).setIcon(null);
file.addSeparator();
file.add(new JMenuItem(exitAction)).setIcon(null);


element.add(new JMenuItem(lineAction = new TypeAction("Line",LINE,"Draw line"))).setIcon(null);
element.add(new JMenuItem(rectangleAction = new TypeAction("Rectangle",RECTANGLE,"Draw rectangle"))).setIcon(null);
element.add(new JMenuItem(circleAction = new TypeAction("Circle",CIRCLE,"Draw circle"))).setIcon(null);
element.add(new JMenuItem(curveAction = new TypeAction("Curve",CURVE,"Draw curve"))).setIcon(null);


JMenu color = new JMenu("Color");
color.setMnemonic('C');

color.add(redAction =new ColorAction("Red",RED,"Draw in red")).setIcon(null);
color.add(blueAction = new ColorAction("Blue",BLUE,"Draw in blue")).setIcon(null);
color.add(greenAction = new ColorAction("Green",GREEN,"Draw in green")).setIcon(null);
color.add(yellowAction = new ColorAction("Yellow",YELLOW,"Draw in yellow")).setIcon(null);

element.addSeparator();
element.add(color);

menubar.add(file);
menubar.add(element);

setJMenuBar(menubar);

toolbar.addSeparator();
addToolBarButton(newAction);
addToolBarButton(openAction);
addToolBarButton(saveAction);
addToolBarButton(printAction);
addToolBarButton(exitAction);
toolbar.addSeparator();
addToolBarButton(lineAction);
addToolBarButton(curveAction);
addToolBarButton(rectangleAction);
addToolBarButton(circleAction);
toolbar.addSeparator();
addToolBarButton(redAction);
addToolBarButton(blueAction);
addToolBarButton(greenAction);
addToolBarButton(yellowAction);

toolbar.setBorder(BorderFactory.createEtchedBorder(BLUE,LIGHT_GRAY));
toolbar.setFloatable(false);
c.add(toolbar,"North");
}

class FileAction extends AbstractAction{
public FileAction(String name){
super(name);
String fileImageName = "Images//"+name+".gif";
if(new File(fileImageName).exists())
putValue(SMALL_ICON,new ImageIcon(fileImageName));
}

public FileAction(String name,KeyStroke keystroke){
this(name);
if(keystroke != null)
putValue(ACCELERATOR_KEY,keystroke);
}

public FileAction(String name,KeyStroke keystroke,String tooltip){
this(name,keystroke);
if(tooltip != null)
putValue(SHORT_DESCRIPTION,tooltip);
}

public void actionPerformed(ActionEvent e){

}
}

class TypeAction extends AbstractAction{
public TypeAction(String name,int typeID){
super(name);
this.typeID = typeID;
String fileImageName = "Images//"+name+".gif";
if(new File(fileImageName).exists())
putValue(SMALL_ICON,new ImageIcon(fileImageName));
}

public TypeAction(String name,int typeID,String tooltip){
this(name,typeID);
if(tooltip != null)
putValue(SHORT_DESCRIPTION,tooltip);
}
public void actionPerformed(ActionEvent e){
elementType = typeID;
}

private int typeID;
}

class ColorAction extends AbstractAction{
public ColorAction(String name,Color color){
super(name);
this.color = color;
String fileImageName = "Images//"+name+".gif";
if(new File(fileImageName).exists())
putValue(SMALL_ICON,new ImageIcon(fileImageName));
}

public ColorAction(String name,Color color,String tooltip){
this(name,color);
if(tooltip != null)
putValue(SHORT_DESCRIPTION,tooltip);
}
public void actionPerformed(ActionEvent e){
elementColor = color;

c.setBackground(color);
}

private Color color;
}

/*添加菜单时用辅助器会比较好一点
* private JMenuItem addMenuItem(JMenu menu,Action action){
JMenuItem item = menu.add(action);
KeyStroke keystroke = (KeyStroke)action.getValue(action.ACCELERATOR_KEY);
if(keystroke != null)
item.setAccelerator(keystroke);
return item;
}*/
private JButton addToolBarButton(Action action){
JButton button = new JButton(action);
button.setBorder(BorderFactory.createRaisedBevelBorder());
button.setText(null);
toolbar.add(button);

return button;
}

public static void main(String[] args){
test = new Exercise1("Exercise 1");
Toolkit kit = test.getToolkit();
Dimension size = kit.getScreenSize();
test.setBounds(size.width/4,size.height/4,size.width/2,size.height/2);
test.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
test.setVisible(true);
}

private static Exercise1 test;
private Container c = getContentPane();
private JMenuBar menubar = new JMenuBar();
private JToolBar toolbar = new JToolBar();

private FileAction newAction,openAction,
closeAction,saveAction,saveAsAction,printAction,exitAction;

private TypeAction lineAction,
rectangleAction,circleAction,curveAction;

private ColorAction redAction,
blueAction,greenAction,yellowAction;

private int elementType = DEFAULT_ELEMENT_TYPE;
private Color elementColor = DEFAULT_ELEMENT_COLOR;
}
为什么工具栏的菜单没有图标,就只有那么一小丁点!?找不出问题啊

搜索更多相关主题的帖子: public final int static import 
2006-08-19 23:34
ada518618
Rank: 1
等 级:新手上路
帖 子:184
专家分:0
注 册:2006-3-27
收藏
得分:0 
再说明一点,我在当前文件夹里有个Images文件夹,里面有比如New.gif、Open.gif等等的工具栏图片。

Eclipse!尽管我现在不懂祢,可是我却对你情有独钟……
2006-08-19 23:47
无理取闹
Rank: 9Rank: 9Rank: 9
等 级:贵宾
威 望:53
帖 子:4264
专家分:0
注 册:2006-7-26
收藏
得分:0 
你的代码又长又没注释
我也盲目了

win32汇编
病毒 加密
目前兴趣所在
2006-08-20 21:44
lgdcky
Rank: 2
等 级:论坛游民
威 望:5
帖 子:576
专家分:18
注 册:2006-8-5
收藏
得分:0 
水平有限,没注释的代码看得眼睛都花了!

2006-08-20 22:37
ada518618
Rank: 1
等 级:新手上路
帖 子:184
专家分:0
注 册:2006-3-27
收藏
得分:0 

呵呵,不好意思,我看代码很少看注释,所以自己也很少写注释!
不过我问题解决了,还是麻烦你们了!


Eclipse!尽管我现在不懂祢,可是我却对你情有独钟……
2006-08-21 09:20
快速回复:[求助]我盲目了,看不出那里出问题了
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.017965 second(s), 8 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved