⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 counter.java

📁 Graphical user interface example from class 2006
💻 JAVA
字号:
package handlingEvents;
import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.SwingConstants;
import javax.swing.WindowConstants;

public class Counter extends JPanel {
	private static final long serialVersionUID = -3527403861216559992L;
	private int value = 0;
	private JLabel display = new JLabel("0");
	JFrame frame = new JFrame("Counter");

	public Counter() {
		setLayout(new BorderLayout());
		
		/* Adds the panel */
		frame.setContentPane(this); 
		
		/* Centralizes the number that is displayed and adds display */
		display.setHorizontalAlignment(SwingConstants.CENTER);
		add(display, BorderLayout.NORTH);
		
		/* Adds the button */
		JButton increment = new JButton("Increment");
		add(increment, BorderLayout.CENTER);
	
		/* Adds some action to the button */
		increment.addActionListener(new ButtonListener());
		
		/* Adding another button */
		JButton decrement = new JButton("Decrement");
		add(decrement, BorderLayout.SOUTH);
		
		/* Adding action using an anonymous inner class */
		decrement.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				value--;
				display.setText(Integer.toString(value));
				repaint();
			}					
		});
		
		frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
		frame.pack();
		frame.setVisible(true);
	}
	
	public static void main(String[] args) {
		javax.swing.SwingUtilities.invokeLater(new Runnable() {
			public void run() {
				new Counter();
			}
		});
	}
	
	/* Listener for the button */
	private class ButtonListener implements ActionListener {
		public void actionPerformed(ActionEvent arg0) {
			value++;
			display.setText(Integer.toString(value));
			repaint();
		}		
	}
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -