发布时间 : 星期三 文章Java图形界面程序设计更新完毕开始阅读a9fd7060caaedd3383c4d37b
b2.setActionCommand(\ f.add(b1,\ f.add(b2,\ f.pack(); f.setVisible(true); } }
class Monitor2 implements ActionListener {
public void actionPerformed(ActionEvent e) { System.out.println(e.getActionCommand()); } }
实例2
import java.awt.*;
import java.awt.event.*;
public class MultiListenerTest implements MouseMotionListener,MouseListener { Frame f = new Frame(\多重监听器测试\ TextField tf = new TextField(30); public MultiListenerTest(){ Label l = new Label(\请按下鼠标左键并拖动\ f.add(l, \ f.add(tf, \ f.setBackground(new Color(180,225,225)); f.addMouseMotionListener(this); f.addMouseListener(this); f.setSize(300, 200); f.setVisible(true); }
public static void main(String args[]) { MultiListenerTest t = new MultiListenerTest(); }
public void mouseDragged(MouseEvent e) {
String s = \鼠标拖动到位置(\ \ tf.setText(s); }
public void mouseEntered(MouseEvent e) { tf.setText(\鼠标已进入窗体\ }
public void mouseExited(MouseEvent e) { tf.setText(\鼠标已移出窗体\ }
public void mouseMoved(MouseEvent e) { }
public void mousePressed(MouseEvent e) { } public void mouseClicked(MouseEvent e) { } public void mouseReleased(MouseEvent e) { } }
3、 源代码:
import java.awt.Graphics; import javax.swing.JPanel;
public class Draw3DRect extends JPanel{ public void paint(Graphics g){
g.draw3DRect(60, 50, 70, 30, true); } }
import java.awt.Graphics; import javax.swing.JPanel;
public class DrawLine extends JPanel{ public void paint(Graphics g){ g.drawLine(60,50, 70, 30); } }
//主函数
import java.awt.Graphics; import javax.swing.JPanel;
public class DrawOval extends JPanel{ public void paint(Graphics g){ g.drawOval(60,50, 70, 30); } }
import java.awt.BorderLayout; import java.awt.Dimension; import java.awt.GridLayout; import java.awt.Panel; import java.awt.Toolkit;
import java.awt.event.ActionEvent; import java.awt.event.ActionListener;
import javax.swing.JButton; import javax.swing.JFrame; import javax.swing.JPanel;
public class DrawMain extends JFrame
implements ActionListener{ /**
* @param args */
private JButton oval = new JButton(\椭圆形\ private JButton line = new JButton(\直形\ private JButton rect = new JButton(\矩形\ private Panel p = new Panel(); private JPanel jp = null;
public DrawMain(){ }
private void jbInit() throws Exception{
this.getContentPane().setLayout(new BorderLayout()); p.setLayout(new GridLayout(1,3)); p.add(line); p.add(rect); p.add(oval);
this.add(p,BorderLayout.NORTH); this.setTitle(\图画板\ this.setSize(600,600); this.setVisible(true); Dimension
screenSize
=
Toolkit.getDefaultToolkit().getScreenSize(); Dimension frameSize = this.getSize(); if(frameSize.height > screenSize.height){ }
if(frameSize.width > screenSize.height){ }
this.setLocation((screenSize.width-frameSize.width)/2,(screenSize.height-frameSize.height)/2); this.setVisible(true);
frameSize.width = screenSize.width; frameSize.height = screenSize.height; try{ }
jbInit();
}catch(Exception e){
e.printStackTrace();
line.addActionListener(this); oval.addActionListener(this); rect.addActionListener(this); }
public void actionPerformed(ActionEvent e){
if(e.getSource() == oval){ }
if(jp != null){ this.remove(jp); if(jp != null){ }
jp = new DrawOval();
this.getContentPane().add(jp,BorderLayout.CENTER); this.setVisible(true); System.out.println(132654);
this.remove(jp);
if(e.getSource() == rect){
}
}
jp = new Draw3DRect();
this.getContentPane().add(jp,BorderLayout.CENTER); this.setVisible(true);
if(e.getSource() == line){ }
public static void main(String[] args) { }
}
DrawMain drawmain = new DrawMain();
}
}
jp = new DrawLine();
this.getContentPane().add(jp,BorderLayout.CENTER); this.setVisible(true);
if(jp != null){ this.remove(jp);
五、实验过程中遇到的问题及解决手段: 六、本次实验的体会(结论):实验让我更深刻的体会到java语言。