<ruby id="bdb3f"></ruby>

    <p id="bdb3f"><cite id="bdb3f"></cite></p>

      <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
        <p id="bdb3f"><cite id="bdb3f"></cite></p>

          <pre id="bdb3f"></pre>
          <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

          <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
          <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

          <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                <ruby id="bdb3f"></ruby>

                企業??AI智能體構建引擎,智能編排和調試,一鍵部署,支持知識庫和私有化部署方案 廣告
                # 與`JFrame`交互 – 按鈕,監聽器和文本區域 > 原文: [https://javatutorial.net/jframe-buttons-listeners-text-fields](https://javatutorial.net/jframe-buttons-listeners-text-fields) 本教程說明了如何通過使用按鈕,監聽器和文本區域與[`JFrame`](https://javatutorial.net/swing-jframe-basics-create-jframe)進行交互。 ## 背景 交互性是用戶在每個應用程序中想要的。 為了在程序中增加交互性,Java 為我們提供了一種非常簡單的方法。`Javax.swing.JButton`類為我們提供了一種添加按鈕的方法,并且在單擊按鈕后發生事件。 同樣,借助`javax.swing.JTextfield`的幫助,我們可以向[`JFrame`](https://javatutorial.net/swing-jframe-basics-create-jframe)添加文本區域。 ## 添加文本區域 您可以使用`JTextfield()`方法創建一個文本區域。 此類具有許多構造函數,如`JTextField()`:構造一個新的文本區域 `JTextField(String text)`:使用指定的文本構造一個文本區域。 `JTextField(String text, int colomn)`:它使用指定的文本和列數創建一個新的文本區域。 以下程序顯示了向`JFrame`添加文本區域的示例。 ```java 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); } } ``` 這是此代碼的輸出 ![TextField example ](https://img.kancloud.cn/09/d7/09d72f7c425b57f9a8502fb4bc7dfd38_364x196.jpg) 文本區域示例 您可以使用`JTextfield`提供的不同方法進行自定義。 `JTextField.setfont(Font f)`:它設置文本的字體 `JTextField.setHorizo??ntalAlignment(int alignment)`:它設置文本的水平對齊方式。 `JTextField.setScrollOffset(int scrolloffset)`:它以像素為單位設置滾動偏移量。 ## 添加按鈕并應用動作監聽器 同樣,您可以在`JFrame`中添加按鈕。 `JButton`為我們提供了一種添加按鈕和動作監聽器的簡便方法。 它有很多像 `JButton()`:創建一個沒有文本且沒有圖標的按鈕。 `JButton(String text)`:創建一個帶有指定文本的按鈕。 `JButton(Icon icon)`:創建一個帶有指定圖標的按鈕。 `JButton(String text, Icon icon)`:創建帶有指定文本和圖標的按鈕。 這是帶有圖像圖標的`JButton`的簡單示例。 ```java 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(); } } ``` 這是此代碼的輸出 ![Button with icon ](https://img.kancloud.cn/65/c9/65c957b119f95f9f60397eb2041c1565_418x394.jpg) 帶有圖標的按鈕 ## 添加動作監聽器 在`JButton`上添加動作監聽器非常簡單。 `JButton`類提供方法`JButton.addActionListener()`,該方法實現重寫方法`actionPerformed()`。 在下面的示例中,我編寫了一個簡單的程序,要求用戶輸入名稱,當用戶單擊“提交”按鈕時,將顯示一條消息“名稱已提交”。這是代碼。 ```java 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 button JButton b=new JButton("Submit"); b.setBounds(100,100,140, 40); //enter name label JLabel label = new JLabel(); label.setText("Enter Name :"); label.setBounds(10, 10, 100, 100); //empty label which will show event after button clicked JLabel label1 = new JLabel(); label1.setBounds(10, 110, 200, 100); //textfield to enter name JTextField textfield= new JTextField(); textfield.setBounds(110, 50, 130, 30); //add to frame f.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 listener b.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent arg0) { label1.setText("Name has been submitted."); } }); } public static void main(String[] args) { new SimpleJButton(); } } ``` 這是此代碼的輸出 ![Action Listener](https://img.kancloud.cn/40/0a/400aaa215892d902930ce51b9a9fa531_286x294.jpg) 行為監聽器 這是完整代碼的[鏈接](https://github.com/NeelumAyub/Tutorials/tree/master/InteractiveJFrame),您可以下載。
                  <ruby id="bdb3f"></ruby>

                  <p id="bdb3f"><cite id="bdb3f"></cite></p>

                    <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
                      <p id="bdb3f"><cite id="bdb3f"></cite></p>

                        <pre id="bdb3f"></pre>
                        <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

                        <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
                        <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

                        <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                              <ruby id="bdb3f"></ruby>

                              哎呀哎呀视频在线观看