package com.shiyan.course;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JButton;
import javax.swing.JTextField;
import java.awt.Color;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
public class MySwingWindow extends JFrame {
private JLabel myLabel;
private JTextField myTextField;
private JButton myButton;
public MySwingWindow() {
// 子类的构造函数中默认调用父类的无参构造函数
this.setSize(400, 300);
this.getContentPane().setLayout(null);
this.setTitle("My First Swing Window");
this.add(getJLabel(), null);
this.add(getJTextField(), null);
this.add(getJButton(), null);
}
private JLabel getJLabel() {
if (this.myLabel == null) {
this.myLabel = new JLabel();
this.myLabel.setBounds(5, 10, 250, 30);
this.myLabel.setText("Hello! Welcome to shiyanlou.com");
}
return this.myLabel;
}
private JButton getJButton() {
if (this.myButton == null) {
this.myButton = new JButton();
this.myButton.setBounds(5, 80, 100, 40);
this.myButton.setText("Click me!");
this.myButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
myLabel.setForeground(Color.RED);
myTextField.setBackground(Color.gray);
}
});
}
return this.myButton;
}
private JTextField getJTextField() {
if (this.myTextField == null) {
this.myTextField = new JTextField();
this.myTextField.setBounds(5, 45, 200, 30);
this.myTextField.setText("Shi Yan Lou");
}
return this.myTextField;
}
public static void main(String[] args) {
MySwingWindow window = new MySwingWindow();
window.setVisible(true);
}
}
[小练习] 初识Swing
©著作权归作者所有,转载或内容合作请联系作者
- 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
- 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
- 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
推荐阅读更多精彩内容
- 在永澄老大目标管理的机缘巧合下,我在小荻老师的《沟通训练营》搬了个小板凳,先感谢下永澄宝宝和俱乐部的“福利”,心存...
- Vue.js 一个流行的MVVM前端框架,数据驱动思想使得前端开发易于理解和维护。PHPSpreadsheet 提...