Java画图板程序设计报告(4)

2019-08-03 10:29

helpMenu.add(aboutItem); bar.add(helpMenu);

items=new ImageIcon[names.length];

4. 工具栏图形按钮功能实现

创建基本图形按钮、导入图标、加入动作侦听器、字体风格选择、字体选择、画图板等功能的实现。 代码如下:

//创建各种基本图形的按钮

drawingArea=new DrawPanel(); choices=new JButton[names.length];

buttonPanel = new JToolBar( JToolBar.VERTICAL ) ; buttonPanel = new JToolBar( JToolBar.HORIZONTAL) ; ButtonHandler handler=new ButtonHandler(); ButtonHandler1 handler1=new ButtonHandler1();

//导入图形图标,这些图标都存放在与源文件相同的目录下面 for(int i=0;i

items[i]=new ImageIcon(names[i] + \ choices[i]=new JButton(\ choices[i].setToolTipText(tipText[i]); buttonPanel.add(choices[i]); }

//将动作侦听器加入按钮里面

for(int i=3;i

choices[i].addActionListener(handler); }

choices[0].addActionListener( new ActionListener(){

public void actionPerformed(ActionEvent e) {

newFile(); } } );

choices[1].addActionListener( new ActionListener(){

public void actionPerformed(ActionEvent e) {

16 / 31

loadFile(); } } );

choices[2].addActionListener( new ActionListener(){

public void actionPerformed(ActionEvent e) {

saveFile(); } } );

choices[choices.length-3].addActionListener(handler1); choices[choices.length-2].addActionListener(handler1); choices[choices.length-1].addActionListener(handler1);

//字体风格选择

styles=new JComboBox(styleNames); styles.setMaximumRowCount(8); styles.addItemListener( new ItemListener(){

public void itemStateChanged(ItemEvent e) {

style1=styleNames[styles.getSelectedIndex()]; } } );

//字体选择

bold=new JCheckBox(\ italic=new JCheckBox(\

checkBoxHandler cHandler=new checkBoxHandler(); bold.addItemListener(cHandler); italic.addItemListener(cHandler); JPanel wordPanel=new JPanel(); buttonPanel.add(bold); buttonPanel.add(italic); buttonPanel.add(styles);

styles.setMinimumSize( new Dimension ( 50, 20 ) ); styles.setMaximumSize(new Dimension ( 100, 20 ) );

Container c=getContentPane(); super.setJMenuBar( bar );

17 / 31

c.add(buttonPanel,BorderLayout.NORTH); c.add(drawingArea,BorderLayout.CENTER);

statusBar=new JLabel();

c.add(statusBar,BorderLayout.SOUTH);

statusBar.setText(\

createNewItem(); setSize(width,height); show(); }

//按钮侦听器ButtonHanler类,内部类,用来侦听基本按钮的操作 public class ButtonHandler implements ActionListener {

public void actionPerformed(ActionEvent e) {

for(int j=3;j

if(e.getSource()==choices[j]) {currentChoice=j; createNewItem(); repaint();} } } }

//按钮侦听器ButtonHanler1类,用来侦听颜色选择、画笔粗细设置、文字输入按钮的操作 public class ButtonHandler1 implements ActionListener {

public void actionPerformed(ActionEvent e) {

if(e.getSource()==choices[choices.length-3]) {chooseColor();}

if(e.getSource()==choices[choices.length-2]) {setStroke();}

if(e.getSource()==choices[choices.length-1]) {JOptionPane.showMessageDialog(null,

\hit the drawing pad to choose the word input position\ \INFORMATION_MESSAGE ); currentChoice=14; createNewItem(); repaint(); }

18 / 31

} }

//鼠标事件mouseA类,继承了MouseAdapter,用来完成鼠标相应事件操作 class mouseA extends MouseAdapter {

public void mousePressed(MouseEvent e)

{statusBar.setText(\ \设置状态提示

itemList[index].x1=itemList[index].x2=e.getX(); itemList[index].y1=itemList[index].y2=e.getY();

//如果当前选择的图形是随笔画或者橡皮擦,则进行下面的操作 if(currentChoice==3||currentChoice==13) {

itemList[index].x1=itemList[index].x2=e.getX(); itemList[index].y1=itemList[index].y2=e.getY(); index++;

createNewItem(); }

//如果当前选择的图形式文字输入,则进行下面操作 if(currentChoice==14) {

itemList[index].x1=e.getX(); itemList[index].y1=e.getY();

String input;

input=JOptionPane.showInputDialog( \ itemList[index].s1=input; itemList[index].x2=f1; itemList[index].y2=f2; itemList[index].s2=style1;

index++;

currentChoice=14; createNewItem(); drawingArea.repaint(); } }

public void mouseReleased(MouseEvent e)

19 / 31

{statusBar.setText(\ \

if(currentChoice==3||currentChoice==13) {

itemList[index].x1=e.getX(); itemList[index].y1=e.getY(); }

itemList[index].x2=e.getX(); itemList[index].y2=e.getY(); repaint(); index++;

createNewItem(); }

public void mouseEntered(MouseEvent e) {

statusBar.setText(\ \ }

public void mouseExited(MouseEvent e) {

statusBar.setText(\ \ } }

class mouseB extends MouseMotionAdapter {

public void mouseDragged(MouseEvent e)

{statusBar.setText(\\

if(currentChoice==3||currentChoice==13) {

itemList[index-1].x1=itemList[index].x2=itemList[index].x1=e.getX();

itemList[index-1].y1=itemList[index].y2=itemList[index].y1=e.getY(); index++;

createNewItem(); } else {

itemList[index].x2=e.getX(); itemList[index].y2=e.getY(); }

20 / 31


Java画图板程序设计报告(4).doc 将本文的Word文档下载到电脑 下载失败或者文档不完整,请联系客服人员解决!

下一篇:《喂自己影子吃饭的人》详细解析

相关阅读
本类排行
× 注册会员免费下载(下载后可以自由复制和排版)

马上注册会员

注:下载文档有可能“只有目录或者内容不全”等情况,请下载之前注意辨别,如果您已付费且无法下载或内容有问题,请联系我们协助你处理。
微信: QQ: