<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>

                ThinkChat2.0新版上線,更智能更精彩,支持會話、畫圖、視頻、閱讀、搜索等,送10W Token,即刻開啟你的AI之旅 廣告
                # 適合初學者的 Java Swing 教程 > 原文: [https://beginnersbook.com/2015/07/java-swing-tutorial/](https://beginnersbook.com/2015/07/java-swing-tutorial/) Swing 是 Java 基礎類(JFC)的一部分,JFC 的其他部分是 java2D 和抽象 window 工具包(AWT)。 AWT,Swing 和 Java 2D 用于在 java 中構建圖形用戶界面(GUI)。在本教程中,我們將主要討論用于在 AWT 頂部構建 GUI 的 Swing API,與 AWT 相比,它更輕量級。 ## 一個簡單的例子 在下面的示例中,我們將使用您在本教程中到目前為止尚未學習的幾個 swing 組件。我們將在即將到來的搖擺教程中詳細討論每一個和所有內容。 下面的 swing 程序會創建一個登錄界面。 ```java import javax.swing.JButton; import javax.swing.JFrame; import javax.swing.JLabel; import javax.swing.JPanel; import javax.swing.JPasswordField; import javax.swing.JTextField; public class SwingFirstExample { ?? ?public static void main(String[] args) {?? ? ?? ??? ?// Creating instance of JFrame ?? ??? ?JFrame frame = new JFrame("My First Swing Example"); ?? ??? ?// Setting the width and height of frame ?? ??? ?frame.setSize(350, 200); ?? ??? ?frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); ?? ??? ?/* Creating panel. This is same as a div tag in HTML ?? ??? ? * We can create several panels and add them to specific ?? ??? ? * positions in a JFrame. Inside panels we can add text ?? ??? ? * fields, buttons and other components. ?? ??? ? */ ?? ??? ?JPanel panel = new JPanel();?? ? ?? ??? ?// adding panel to frame ?? ??? ?frame.add(panel); ?? ??? ?/* calling user defined method for adding components ?? ??? ? * to the panel. ?? ??? ? */ ?? ??? ?placeComponents(panel); ?? ??? ?// Setting the frame visibility to true ?? ??? ?frame.setVisible(true); ?? ?} ?? ?private static void placeComponents(JPanel panel) { ?? ??? ?/* We will discuss about layouts in the later sections ?? ??? ? * of this tutorial. For now we are setting the layout ?? ??? ? * to null ?? ??? ? */ ?? ??? ?panel.setLayout(null); ?? ??? ?// Creating JLabel ?? ??? ?JLabel userLabel = new JLabel("User"); ?? ??? ?/* This method specifies the location and size ?? ??? ? * of component. setBounds(x, y, width, height) ?? ??? ? * here (x,y) are cordinates from the top left ?? ??? ? * corner and remaining two arguments are the width ?? ??? ? * and height of the component. ?? ??? ? */ ?? ??? ?userLabel.setBounds(10,20,80,25); ?? ??? ?panel.add(userLabel); ?? ??? ?/* Creating text field where user is supposed to ?? ??? ? * enter user name. ?? ??? ? */ ?? ??? ?JTextField userText = new JTextField(20); ?? ??? ?userText.setBounds(100,20,165,25); ?? ??? ?panel.add(userText); ?? ??? ?// Same process for password label and text field. ?? ??? ?JLabel passwordLabel = new JLabel("Password"); ?? ??? ?passwordLabel.setBounds(10,50,80,25); ?? ??? ?panel.add(passwordLabel); ?? ??? ?/*This is similar to text field but it hides the user ?? ??? ? * entered data and displays dots instead to protect ?? ??? ? * the password like we normally see on login screens. ?? ??? ? */ ?? ??? ?JPasswordField passwordText = new JPasswordField(20); ?? ??? ?passwordText.setBounds(100,50,165,25); ?? ??? ?panel.add(passwordText); ?? ??? ?// Creating login button ?? ??? ?JButton loginButton = new JButton("login"); ?? ??? ?loginButton.setBounds(10, 80, 80, 25); ?? ??? ?panel.add(loginButton); ?? ?} } ``` **輸出:** ![Swing Login Screen Example](https://img.kancloud.cn/63/88/638859146346d21d72647ec5bc3c9ee3_350x200.jpg) 在上面的例子中,我們使用了幾個組件。我們先討論一下它們,然后我們將在下一個教程中詳細討論它們。 **`JFrame`** - 幀是`JFrame`的一個實例。框架是一個窗口,可以有標題,邊框,菜單,按鈕,文本字段和其他幾個組件。 Swing 應用必須有一個框架才能添加組件。 **`JPanel`** - 面板是`JPanel`的一個實例。一個框架可以有多個面板,每個面板可以有幾個組件。你也可以稱它們為 Frame 的一部分。面板可用于對組件進行分組并將它們放置在框架中的適當位置。 **`JLabel`** - 標簽是`JLabel`類的一個實例。標簽是不可選擇的文本和圖像。如果要在框架上顯示字符串或圖像,可以使用標簽。在上面的例子中,我們想要在文本字段之前顯示文本`"User"`和`"Password"`,我們通過創建標簽并將其添加到適當的位置來實現此目的。 **`JTextField`** - 用于捕獲用戶輸入,這些是用戶輸入數據的文本框。 **`JPasswordField`** - 與文本字段類似,但輸入的數據被隱藏并在 GUI 上顯示為點。 **`JButton`** - 一個按鈕是`JButton`類的一個實例。在上面的例子中,我們有一個“登錄”按鈕。
                  <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>

                              哎呀哎呀视频在线观看