当前位置: 代码迷 >> Java相关 >> [求助]我盲目了,看不出那里出问题了
  详细解决方案

[求助]我盲目了,看不出那里出问题了

热度:244   发布时间:2006-08-19 23:34:28.0
[求助]我盲目了,看不出那里出问题了

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  

----------------解决方案--------------------------------------------------------
再说明一点,我在当前文件夹里有个Images文件夹,里面有比如New.gif、Open.gif等等的工具栏图片。
----------------解决方案--------------------------------------------------------
你的代码又长又没注释
我也盲目了
----------------解决方案--------------------------------------------------------
水平有限,没注释的代码看得眼睛都花了!
----------------解决方案--------------------------------------------------------

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


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