学了Java的GUI后,做了个super简单的计算器,只能做加法。(萌新日常记录,大佬发现有什么问题欢迎评论区留言)
都是很简单的知识,其中主要涉及GridLayout布局的应用、添加监听器、两个控件、还有响应事件、内部类…
详细代码如下:
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
@SuppressWarnings("serial")
public class CalculatorInWindow extends JFrame {
JButton button;
JButton button1;
JTextField text1;
JTextField text2;
JTextArea text3;
ReaderListen listener;
public CalculatorInWindow() {
init();
setVisible(true);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
void init() {
setLayout(new GridLayout(3,3)); //使用GridLayout布局生成3x3的网格状布局
listener=new ReaderListen();
add(new JLabel("加数1")); //添加便签
text1=new JTextField();
text1.addActionListener(listener); //添加监听器监听texe1
add(new JScrollPane(text1)); //在文本框添加滚动条,并将文本框添加到窗口
add(new JLabel(""));