Java中菜单的实现以及画实线与画虚线之间的自由转化
1、Java画线
1 import java.awt.Color; 2 import java.awt.Container; 3 import java.awt.Graphics; 4 import java.awt.Point; 5 import java.awt.event.ActionEvent; 6 import java.awt.event.ActionListener; 7 8 import javax.swing.JFrame; 9 import javax.swing.JMenu; 10 import javax.swing.JMenuBar; 11 import javax.swing.JMenuItem; 12 import javax.swing.JPanel; 13 14 class MyPanel extends JPanel { 15 public static final int MODE_LINE = 1; 16 public static final int MODE_POINT = 2; 17 // 18 private int drawMode = MODE_POINT; 19 20 @Override 21 protected void paintComponent(Graphics g) { 22 // super.paintComponent(g); 23 24 // g.setColor(Color.red); 25 // g.fillOval(10, 10, 100, 80); 26 drawSinAdapteSize(g); 27 28 g.dispose(); 29 } 30 31 // 自适应的绘图方法 32 private void drawSinAdapteSize(Graphics g) { 33 g.setColor(Color.red); 34 int w = getWidth(); 35 int h = getHeight(); 36 37 int SCALE_X = (int) (w / (2 * Math.PI)); 38 int SCALE_Y = h / 2 - 2; 39 int OFFSET = h / 2; 40 41 Point start = new Point(0, OFFSET); 42 43 for (int x = 1; x <= SCALE_X * 2 * Math.PI; x++) { 44 int y = OFFSET - (int) (SCALE_Y * Math.sin(x * 1.0 / SCALE_X)); 45 46 // 根据属性,决定画线还是画点 47 if (drawMode == MODE_POINT) { 48 g.fillOval(x - 1, y - 1, 2, 2); 49 } else if (drawMode == MODE_LINE) { 50 g.drawLine(start.x, start.y, x, y); 51 52 start.x = x; 53 start.y = y; 54 } 55 } 56 } 57 58 public void setDrawMode(int mode) { 59 this.drawMode = mode; 60 } 61 62 // 绘制制定大小的曲线 63 private void drawSin(Graphics g) { 64 g.setColor(Color.red); 65 // y = 220 - 200 * sin(x/100) 66 for (int x = 0; x <= 100 * 2 * Math.PI; x++) { 67 int y = 220 - (int) (200 * Math.sin(x / 100.0)); 68 69 g.fillOval(x - 1, y - 1, 2, 2); 70 } 71 } 72 } 73 74 public class DrawFrame extends JFrame implements ActionListener { 75 // 属性 76 MyPanel panel = null; 77 78 // 方法 79 public DrawFrame() { 80 this.setTitle("绘图"); 81 setSize(650, 500); 82 setDefaultCloseOperation(EXIT_ON_CLOSE); 83 setLocationRelativeTo(null); 84 85 panel = new MyPanel(); 86 panel.setBackground(Color.LIGHT_GRAY); 87 88 Container cp = getContentPane(); 89 cp.add(panel); 90 91 // JMenuBar 92 JMenuBar menuBar = new JMenuBar(); 93 // JMenu 94 JMenu menu = new JMenu("Draw"); 95 // JMenuItem 96 JMenuItem item = new JMenuItem("Line"); 97 menu.add(item); 98 item.addActionListener(this); 99 100 item = new JMenuItem("Point"); 101 menu.add(item); 102 item.addActionListener(this); 103 104 menuBar.add(menu); 105 this.setJMenuBar(menuBar); 106 } 107 108 public static void main(String[] args) { 109 JFrame frame = new DrawFrame(); 110 frame.setVisible(true); 111 } 112 113 @Override 114 public void actionPerformed(ActionEvent event) { 115 String cmd = event.getActionCommand(); 116 if ("Line".equals(cmd)) { 117 panel.setDrawMode(MyPanel.MODE_LINE); 118 } else if ("Point".equals(cmd)) { 119 panel.setDrawMode(MyPanel.MODE_POINT); 120 } 121 panel.repaint(); 122 } 123 124 }
2、Java菜单:
1 import java.awt.event.ActionEvent; 2 import java.awt.event.ActionListener; 3 4 import javax.swing.JFrame; 5 import javax.swing.JMenu; 6 import javax.swing.JMenuBar; 7 import javax.swing.JMenuItem; 8 import javax.swing.JOptionPane; 9 10 public class MenuFrame extends JFrame implements ActionListener { 11 /** 12 * JMuneBar(菜单栏)中包含多项Jmenu(菜单),Jmenu下有多项JMenuItem(菜单项) 13 */ 14 public MenuFrame() { 15 this.setTitle("MenuFrame"); 16 this.setSize(600, 400); 17 setLocationRelativeTo(null); 18 setDefaultCloseOperation(EXIT_ON_CLOSE); 19 /** 20 * 由于失误,将所有的组件都用awt下的Menu,MenuItem,MenuBar来实现了,导致菜单中的汉字为乱码 21 */ 22 JMenuBar menuBar=new JMenuBar(); 23 JMenu menu=new JMenu("File"); 24 JMenuItem menuItem=new JMenuItem("新建"); 25 JMenuItem exit=new JMenuItem("退出"); 26 exit.addActionListener(this); 27 28 JMenuItem about=new JMenuItem("关于..."); 29 about.addActionListener(this); 30 menu.add(menuItem); 31 menu.add(exit); 32 menu.add(about); 33 menuBar.add(menu); 34 35 menuBar.add(new JMenu("Edit")); 36 37 this.setJMenuBar(menuBar);//注意最后要将菜单栏加到MenuFrame组件中去,不然菜单不可见 38 } 39 public static void main(String[] args) { 40 JFrame frame=new MenuFrame(); 41 frame.setVisible(true); 42 } 43 @Override 44 public void actionPerformed(ActionEvent e) { 45 String cmd=e.getActionCommand(); 46 if(cmd.equals("Exit")) { 47 dispose(); 48 }else if(cmd.equals("About")) { 49 JOptionPane.showMessageDialog(null, "菜单演示系统"); 50 } 51 } 52 }