原文: https://javatutorial.net/jframe-buttons-listeners-text-fields
本教程说明了如何通过使用按钮,监听器和文本区域与JFrame进行交互。
背景
交互性是用户在每个应用程序中想要的。 为了在程序中增加交互性,Java 为我们提供了一种非常简单的方法。Javax.swing.JButton类为我们提供了一种添加按钮的方法,并且在单击按钮后发生事件。 同样,借助javax.swing.JTextfield的帮助,我们可以向JFrame添加文本区域。
添加文本区域
您可以使用JTextfield()方法创建一个文本区域。 此类具有许多构造函数,如JTextField():构造一个新的文本区域
JTextField(String text):使用指定的文本构造一个文本区域。
JTextField(String text, int colomn):它使用指定的文本和列数创建一个新的文本区域。
以下程序显示了向JFrame添加文本区域的示例。
package javatutorial.net;import java.awt.FlowLayout;import javax.swing.JFrame;import javax.swing.JTextField;public class Example {static JTextField textfield1, textfield2, textfield3;public static void main(String[] args) {JFrame f = new JFrame("Text Field Examples");f.getContentPane().setLayout(new FlowLayout());textfield1 = new JTextField("Text field 1",10);textfield2 = new JTextField("Text field 2",10);textfield3 = new JTextField("Text field 3",10);f.getContentPane().add(textfield1);f.getContentPane().add(textfield2);f.getContentPane().add(textfield3);f.pack();f.setVisible(true);}}
这是此代码的输出

文本区域示例
您可以使用JTextfield提供的不同方法进行自定义。
JTextField.setfont(Font f):它设置文本的字体
JTextField.setHorizontalAlignment(int alignment):它设置文本的水平对齐方式。
JTextField.setScrollOffset(int scrolloffset):它以像素为单位设置滚动偏移量。
添加按钮并应用动作监听器
同样,您可以在JFrame中添加按钮。 JButton为我们提供了一种添加按钮和动作监听器的简便方法。 它有很多像
JButton():创建一个没有文本且没有图标的按钮。
JButton(String text):创建一个带有指定文本的按钮。
JButton(Icon icon):创建一个带有指定图标的按钮。
JButton(String text, Icon icon):创建带有指定文本和图标的按钮。
这是带有图像图标的JButton的简单示例。
package javatutorial.net;import javax.swing.ImageIcon;import javax.swing.JButton;import javax.swing.JFrame;public class SimpleJButton {SimpleJButton(){JFrame f=new JFrame("Button Example");JButton b=new JButton("Play", new ImageIcon("play.png"));b.setBounds(100,100,140, 40);f.add(b);f.setSize(300,400);f.setLayout(null);f.setVisible(true);f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);}public static void main(String[] args) {new SimpleJButton();}}
这是此代码的输出

带有图标的按钮
添加动作监听器
在JButton上添加动作监听器非常简单。 JButton类提供方法JButton.addActionListener(),该方法实现重写方法actionPerformed()。 在下面的示例中,我编写了一个简单的程序,要求用户输入名称,当用户单击“提交”按钮时,将显示一条消息“名称已提交”。这是代码。
package javatutorial.net;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.JTextField;public class SimpleJButton {SimpleJButton(){JFrame f=new JFrame("Button Example");//submit buttonJButton b=new JButton("Submit");b.setBounds(100,100,140, 40);//enter name labelJLabel label = new JLabel();label.setText("Enter Name :");label.setBounds(10, 10, 100, 100);//empty label which will show event after button clickedJLabel label1 = new JLabel();label1.setBounds(10, 110, 200, 100);//textfield to enter nameJTextField textfield= new JTextField();textfield.setBounds(110, 50, 130, 30);//add to framef.add(label1);f.add(textfield);f.add(label);f.add(b);f.setSize(300,300);f.setLayout(null);f.setVisible(true);f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);//action listenerb.addActionListener(new ActionListener() {@Overridepublic void actionPerformed(ActionEvent arg0) {label1.setText("Name has been submitted.");}});}public static void main(String[] args) {new SimpleJButton();}}
这是此代码的输出

行为监听器
这是完整代码的链接,您可以下载。
