📄 testdice.java
字号:
package com.xmu.typot.ui;import java.awt.BorderLayout;import java.awt.Color;import java.awt.Graphics;import java.awt.Graphics2D;import java.awt.Image;import java.awt.event.ActionEvent;import java.awt.event.ActionListener;import java.awt.image.BufferedImage;import java.io.File;import java.io.IOException;import javax.imageio.*;import javax.swing.JButton;import javax.swing.JFrame;import javax.swing.JPanel;import java.util.*;import javax.swing.WindowConstants;import javax.swing.Timer;/*** This code was edited or generated using CloudGarden's Jigloo* SWT/Swing GUI Builder, which is free for non-commercial* use. If Jigloo is being used commercially (ie, by a corporation,* company or business for any purpose whatever) then you* should purchase a license for each developer using Jigloo.* Please visit www.cloudgarden.com for details.* Use of Jigloo implies acceptance of these licensing terms.* A COMMERCIAL LICENSE HAS NOT BEEN PURCHASED FOR* THIS MACHINE, SO JIGLOO OR THIS CODE CANNOT BE USED* LEGALLY FOR ANY CORPORATE OR COMMERCIAL PURPOSE.*/public class TestDice extends javax.swing.JPanel { { //Set Look & Feel try { javax.swing.UIManager.setLookAndFeel("com.sun.java.swing.plaf.windows.WindowsLookAndFeel"); } catch(Exception e) { e.printStackTrace(); } } private JPanel showPanel; private JButton diceButton; private JButton stopButton; private JPanel controlPanel; private Timer timer; public static int dice; private TestFrame frame; /** * Auto-generated main method to display this JFrame */ public static void main(String[] args) { JFrame frame = new JFrame(); frame.getContentPane().add(new TestDice()); frame.setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE); frame.pack(); frame.setVisible(true); } public TestDice() { super(); initGUI(); DiceActionListener listener = new DiceActionListener(); timer = new Timer(25,listener); } public TestDice(TestFrame frame) { // TODO Auto-generated constructor stub super(); initGUI(); DiceActionListener listener = new DiceActionListener(); timer = new Timer(25,listener); this.frame = frame; } private void initGUI() { try { BorderLayout thisLayout = new BorderLayout(); setLayout(thisLayout); { controlPanel = new JPanel(); add(controlPanel, BorderLayout.SOUTH); controlPanel.setPreferredSize(new java.awt.Dimension(150, 36)); { diceButton = new JButton(); controlPanel.add(diceButton); diceButton.setText("Dice"); diceButton.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent arg0) { // TODO Auto-generated method stub timer.start(); stopButton.setEnabled(true); } }); } { stopButton = new JButton(); controlPanel.add(stopButton); stopButton.setText("Stop"); stopButton.setEnabled(false); stopButton.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent arg0) { // TODO Auto-generated method stub timer.stop(); stop(); frame.run(dice); } }); } } { showPanel = new JPanel(); add(showPanel, BorderLayout.NORTH); showPanel.setPreferredSize(new java.awt.Dimension(144, 61)); } this.setSize(154, 143); this.setPreferredSize(new java.awt.Dimension(144, 96)); } catch (Exception e) { e.printStackTrace(); } } private class DiceActionListener implements ActionListener { public void actionPerformed(ActionEvent arg0) { // TODO Auto-generated method stub dice = (int) (Math.random() * 6 + 1); BufferedImage bf = new BufferedImage(55,55,BufferedImage.TYPE_INT_RGB); Graphics2D g2d = (Graphics2D) bf.getGraphics(); g2d.setPaint(Color.WHITE); g2d.fillRect(0, 0, 55, 55); g2d.setPaint(Color.black); File file = new File("image/"+dice+".gif"); try { bf = ImageIO.read(file); Graphics gf = showPanel.getGraphics(); gf.drawImage(bf,50,0,null);// showPanel.repaint(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } } public void stop() { diceButton.setEnabled(false); stopButton.setEnabled(false); } public void run() { diceButton.setEnabled(true); } public void dice(int d) { dice = d; BufferedImage bf = new BufferedImage(55,55,BufferedImage.TYPE_INT_RGB); Graphics2D g2d = (Graphics2D) bf.getGraphics(); g2d.setPaint(Color.WHITE); g2d.fillRect(0, 0, 55, 55); g2d.setPaint(Color.black); File file = new File("image/"+dice+".gif"); try { bf = ImageIO.read(file); Graphics gf = showPanel.getGraphics(); gf.drawImage(bf,50,0,null);// showPanel.repaint(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } frame.run(dice); }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -