java 一个swing应用程序,模拟一堆字母整数的概念,允许堆栈被推,拉,反转和重置。

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了java 一个swing应用程序,模拟一堆字母整数的概念,允许堆栈被推,拉,反转和重置。相关的知识,希望对你有一定的参考价值。

package practicefunctions;

import java.awt.EventQueue;

import javax.swing.JFrame;
import java.util.*
;
import javax.swing.JTextField;
import javax.swing.JButton;
import javax.swing.JLabel;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import javax.swing.SwingConstants;public class Stacker {

	private JFrame frame;
	private JTextField current_stack;
	private JTextField new_value;

	/**
	 * Launch the application.
	 */
	public static String reverseIt(String source) {
	    int i, len = source.length();
	    StringBuilder dest = new StringBuilder(len);

	    for (i = (len - 1); i >= 0; i--){
	        dest.append(source.charAt(i));
	    }

	    return dest.toString();
	}
	public static void main(String[] args) {
		EventQueue.invokeLater(new Runnable() {
			public void run() {
				try {
					Stacker window = new Stacker();
					window.frame.setVisible(true);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
	}

	/**
	 * Create the application.
	 */
	public Stacker() {
		initialize();
	}

	/**
	 * Initialize the contents of the frame.
	 */
	private void initialize() {
		frame = new JFrame();
		frame.setBounds(100, 100, 450, 300);
		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		frame.getContentPane().setLayout(null);
		frame.setTitle("Stacker");
		
		current_stack = new JTextField();
		current_stack.setBounds(6, 36, 438, 28);
		frame.getContentPane().add(current_stack);
		current_stack.setColumns(30);
		
		new_value = new JTextField();
		new_value.setBounds(180, 86, 59, 28);
		frame.getContentPane().add(new_value);
		new_value.setColumns(1);
		
		JButton add_value = new JButton("Push");
		add_value.addMouseListener(new MouseAdapter() {
			@Override
			public void mouseClicked(MouseEvent e) {
				String newval = new_value.getText();
				current_stack.setText(current_stack.getText()+newval);
				new_value.setText("");
			}
		});
		add_value.setBounds(169, 126, 82, 28);
		frame.getContentPane().add(add_value);
		
		JButton reset_values = new JButton("Reset");
		reset_values.addMouseListener(new MouseAdapter() {
			@Override
			public void mouseClicked(MouseEvent e) {
				current_stack.setText("");
			}
		});
		reset_values.setBounds(41, 126, 82, 28);
		frame.getContentPane().add(reset_values);
		
		JButton erase_value = new JButton("Pop");
		erase_value.addMouseListener(new MouseAdapter() {
			@Override
			public void mouseClicked(MouseEvent e) {
				String current = current_stack.getText();
				current_stack.setText(current.substring(0, current.length()-1));
			}
		});
		erase_value.setBounds(301, 126, 74, 28);
		frame.getContentPane().add(erase_value);
		
		JLabel lblCurrentStack = new JLabel("Current Stack");
		lblCurrentStack.setBounds(180, 19, 101, 16);
		frame.getContentPane().add(lblCurrentStack);
		
		JLabel lblBottom = new JLabel("Bottom");
		lblBottom.setBounds(6, 64, 61, 16);
		frame.getContentPane().add(lblBottom);
		
		JLabel lblTop = new JLabel("Top");
		lblTop.setHorizontalAlignment(SwingConstants.RIGHT);
		lblTop.setBounds(383, 64, 61, 16);
		frame.getContentPane().add(lblTop);
		
		
		JButton btnReverse = new JButton("Reverse");
		btnReverse.addMouseListener(new MouseAdapter() {
			@Override
			public void mouseClicked(MouseEvent e) {
				current_stack.setText(reverseIt(current_stack.getText()));
			}
		});
		btnReverse.setBounds(41, 227, 82, 29);
		frame.getContentPane().add(btnReverse);
	}
}

以上是关于java 一个swing应用程序,模拟一堆字母整数的概念,允许堆栈被推,拉,反转和重置。的主要内容,如果未能解决你的问题,请参考以下文章

将文件从操作系统拖放到 Java 应用程序 (Swing)

Java GUI 实时字数计数器

使用 Java Swing 的 ASCII 终端模拟 [关闭]

Java Swing 保存和加载工作区/设置

您推荐哪种 Swing 布局? [关闭]

LeetCode 13. 罗马数字转整数 java 两种解法