Hello World(創建空白窗口)
代碼
package com.edu.wuaa;
import javax.swing.*;
/**
* 2020年5月27日
* 程序功能:產生一個無內容窗口,設定標題、退出後的操作、窗口大小、顯示居中、窗口可視
* 編寫目的:學習基於Swing庫的 GUI編寫
*/
public class HelloFrame extends JFrame{
public HelloFrame(){
// 設置標題
super("Hello World");
// 設置用戶點擊關閉的操作
// EXIT_ON_CLOSE: 使用System.exit()方法退出程序
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setSize(500, 500);
// 讓窗口在屏幕上居中顯示
setLocationRelativeTo(null);
// 窗口可視,不設置則不顯示窗口,程序直接結束。
setVisible(true);
}
public static void main(String[] args) {
HelloFrame frame = new HelloFrame();
}
}
運行結果
關閉窗口後程序結束
創建 Swing components
此處使用了JLabel, JTextField, JButton。都是以J開頭
代碼
package com.edu.wuaa;
import javax.swing.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
/**
* 2020年5月27日
* 程序功能:
* 編寫目的:學習 Java Swing GUI component使用
*/
public class Frame extends JFrame {
public static void main(String[] args) {
Frame frame = new Frame();
}
public Frame() {
// 設置標題
super("An Attempt to Learn Swing Component");
// 設置窗口大小
setSize(500, 500);
// 窗口屏幕居中
setLocationRelativeTo(null);
initComponents();
// 不使用佈局,直接設置components相對於窗口邊界的絕對位置
// components的位置不會隨窗口尺寸改變而改變
setLayout(null);
setDefaultCloseOperation(EXIT_ON_CLOSE);
setVisible(true);
}
private void initComponents() {
// 設置用於顯示字符串 "username" 的 JLabel
JLabel yournameLabel = new JLabel("name");
/* 也可以這樣寫:
JLabel yournameLabel = new JLabel();
yournameLabel.setText("username"); */
// 設置Label的位置和尺寸
yournameLabel.setBounds(40, 20, 100, 30);
// 將Label加入到容器(Frame)中
add(yournameLabel);
// 設置用戶名的輸入框 nameTextField
JTextField nameTextField = new JTextField();
nameTextField.setBounds(160, 20, 100, 30);
add(nameTextField);
// 設置Accept按鈕位置
JButton acceptButton = new JButton("Accept");
acceptButton.setBounds(100, 70, 100, 30);
add(acceptButton);
// 設置一個 JPanel,並在上面設置一個顯示 Hello,{username} 的JLabel
// 設置用於顯示 Hello,{username} 的JLabel,(按鈕點擊後才顯示)
JLabel helloTextLabel = new JLabel();
helloTextLabel.setBounds(50, 200, 100, 30);
// 設置水平居中對齊、垂直居中對齊,然而並沒有產生效果
// helloTextLabel.setHorizontalAlignment(SwingConstants.CENTER);
// helloTextLabel.setVerticalAlignment(SwingConstants.CENTER);
add(helloTextLabel);
// 設置用戶點擊Accept按鈕的監視器和響應邏輯
// 此處使用了 lambda表達式
acceptButton.addActionListener(e -> {
String helloText = "Hello";
String name = nameTextField.getText();
if (name != null && name.trim().length() > 0) {
helloText += String.format(", %s", name);
}
helloTextLabel.setText(helloText);
});
// 也可以這麼寫,不用lambda表達式
// acceptButton.addActionListener(new ActionListener() {
// @Override
// public void actionPerformed(ActionEvent e) {
// String helloText = "Hello";
// String name = nameTextField.getText();
// // trim() 刪去字符串的開頭和結尾空白符(包括空格在內的ascii值 <= 0x20 的任何字符)
// if (name != null && name.trim().length() > 0){
// helloText += String.format(", %s", name);
// }
// helloTextLabel.setText(helloText);
// }
// });
}
}
運行結果
點擊按鈕之前:
不輸入name,點擊按鈕:
輸入name,並點擊按鈕:
有個問題就是,上面代碼中的57,58行用於設定水平對齊和垂直對齊的代碼沒有效果。於是下面做了一個小測試
測試兩個Alignment方法
測試兩種情況下 setVerticalAlignment()
和setHorizonAlignment()
是否奏效:
- 不設置Panel和Layout (測試結果:無效)
- 創建Panel並設置其Layout爲BorderLayout(有效)
代碼
import javax.swing.*;
import java.awt.*;
/**
* 2020年5月27日
* 程序功能:創建打印Hello的窗口(兩種)
* 編寫目的:
* 測試 setHorizonAlignment() 和 setVerticalAlignment()
* 在無Panel,窗口無Layout和有Panel,Panel有Layout時的表現
*/
public class Frame extends JFrame {
public static void main(String[] args) {
// 以下兩行註釋其中一個
// Frame frame = new Frame(false);
Frame frame = new Frame(true);
}
Frame(boolean isLayoutPresent){
super("Test");
setLocationRelativeTo(null);
setSize(300, 300);
if (!isLayoutPresent){
setLayout(null);
}
initComponents(isLayoutPresent);
setDefaultCloseOperation(EXIT_ON_CLOSE);
setVisible(true);
}
private void initComponents(boolean isLayoutPresent) {
JLabel helloTextLabel = new JLabel("Hello!");
helloTextLabel.setBounds(50, 50, 100, 30);
if (!isLayoutPresent){
add(helloTextLabel);
// 以下兩行代碼並不起作用
helloTextLabel.setVerticalAlignment(SwingConstants.CENTER);
helloTextLabel.setHorizontalAlignment(SwingConstants.CENTER);
}
if (isLayoutPresent) {
JPanel helloPanel = new JPanel();
helloPanel.setBounds(40, 150, 220, 70);
helloPanel.setLayout(new BorderLayout());
helloPanel.setBackground(Color.GREEN);
add(helloPanel);
helloTextLabel.setVerticalAlignment(SwingConstants.CENTER);
helloTextLabel.setHorizontalAlignment(SwingConstants.CENTER);
helloPanel.add(helloTextLabel);
}
}
}
運行結果
- 無Panel和Layout時:
此時以下兩行代碼無效
helloTextLabel.setVerticalAlignment(SwingConstants.CENTER); helloTextLabel.setHorizontalAlignment(SwingConstants.CENTER);
- 有Panel,Panel爲BorderLayout時:
此時以下兩行代碼有效
helloTextLabel.setVerticalAlignment(SwingConstants.CENTER); helloTextLabel.setHorizontalAlignment(SwingConstants.CENTER);