自用的,不是闲着无聊的别看我的,不专业不认真不是分享态度,纯属自用。
要不是tmd我在word文档写半天笔记然后电脑突然死机我整个文档全没了我才不会来这里写!!
烦死了!!联想小新大辣鸡!!
看的b站视频:Java Swing入门课程_图形界面GUI编程_100集视频教程
讲得贼好贼细,不过时间好长,而且没学过Java的估计挺难的。
一、首先把窗口存放于Java文件MyDemo中,MyDemo只设置最基础的窗口参数:
public class MyDemo {
public static void main(String[] args) {
Jframe frame = new Myframe("ohyeah"); //窗口名
frame.setDefaultCloseOperation(Jframe.EXIT_ON_CLOSE); //当关闭窗口时,结束运行
frame.setSize(700,500); //长宽
frame.setVisible(true); //显示窗口
}
}
二、具体细节放在另一个java文件Myframe中,Myframe继承Jframe,这样它会有Jframe的所有功能:
public class Myframe extends Jframe { ……
三、在里面放一个同名构造方法,里面就可以放置各种东西了!
public Myframe(String title)
{
super(title);
……
四、里面放的东西:
1.容器 JPenel
JPanel root = new JPanel(); //容器,一个面板 this.setContentPane(root); //设置一个容器
注意:因为是在构造方法里所以用this喔!
2.按钮 JButton
JButton button = new JButton("test"); //一个按钮
root.add(button); //向容器中添加一个按钮控件
3.事件处理 ActionListener
事件处理的代码大概就是:先定义一个按钮或者复选框之类的,然后用内部类button.addActionListener……(详见下面代码第三行开始。内部类的大括号里面可以写任何你希望点击按钮/点击复选框以后发生的事。)
内部类简洁写法(一般建议使用):
以下是点击button后,将会展示当前时间的代码。其中包含时间控件和获取当前时间。
JLabel timelabel = new JLabel("00:00:00");
root.add(timelabel); //添加时间控件
button.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
System.out.println("test: 我被点了");
//取得当前时间
SimpleDateFormat sdf = new SimpleDateFormat("HH:mm:ss");
String timeStr = sdf.format(new Date());
//System.out.println(timeStr);
timelabel.setText(timeStr); //把时间写入label
}
});
还有复杂但是保险的非内部类写法,偶尔使用内部类写法会出莫名其妙的错,但是我懒得重新打一遍了……所以就用上面那种吧。如有需要再回看视频P12。
4.文本框输入 JTextField
相关API:
setText():事先在文本框中放入字符串
getText():获取文本框中输入的字符串
//文本框输入
JTextField textField = new JTextField(20); //这个20是指20个英文字母的长度
这段放在构造方法外面,作为Myframe的属性之一,方便使用。(上)
再在构造方法里把他添加到容器root中。(下)
//文本框输入
root.add(textField);
String strset = "ohye";
textField.setText(strset);
JButton button1 = new JButton("OK");
root.add(button1);
button1.addActionListener( new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
test();
}
});
public void test()
{
String strget = textField.getText();
System.out.println(strget);
}
注意:这里使用了上面事件处理的代码咯!可以复习一下。
注意注意:test()的代码放在构造方法外面
5.复选框 JCheckBox
相关API:
setSelected(true/false) //设置选中状态
isSelected //是否选中
addActionListener //勾选或取消获取事件
//复选框
JCheckBox agreeFiled = new JCheckBox("同意用户协议");
这段放在构造方法外面,作为Myframe的属性之一,方便使用。(上)
再在构造方法里把他添加到容器root中。(下)
//复选框
root.add(agreeFiled);
JButton button2 = new JButton("下一步");
root.add(button2);
agreeFiled.setSelected(false); //设置初始选中状态为不选中
button2.setEnabled(false); //把按钮一开始设为禁用状态
agreeFiled.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
boolean isSelected = agreeFiled.isSelected();
if(isSelected==true)
{
button2.setEnabled(true);
}
else
button2.setEnabled(false);
}
});
这段代码是关于实现:不勾选复选框就禁用button2,勾选复选框就允许使用button2,一个联动效果。
6.下拉列表 JComboBox
数据项:
addItem() //添加数据项
getItemCount() //获取数据项的个数
getItemAt(int) //获取数据中的某项
//下拉列表
JComboBox colorFiled = new JComboBox<>();
下拉列表比较特殊,有点类似于ArrayList的列表形式。注意,同样把这一行踢去构造方法外。
JComboBox
//下拉列表
root.add(colorFiled);
colorFiled.addItem("红");
colorFiled.addItem("黄");
colorFiled.addItem("蓝");
root.add(colorFiled);
//添加一个测试按钮
JButton button3 =new JButton("测试");
root.add(button3);
button3.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
testComboBox();
}
});
(上)构造方法内:都和上面类似啦
(下)构造方法外:试用了下拉列表的API
public void testComboBox()
{
System.out.println(colorFiled.getItemCount());
System.out.println(colorFiled.getItemAt(0));
}
more about下拉列表:
当 JComboBox
toString()中写的是什么,在下拉列表中就会显示什么。
这个代码不搞了,累。如有需要用就看视频P18。
7.标签控件 JLabel
JLabel label = new JLabel();
root.add(label);
label.setText("ohyeyeye你好 ");
相关API:常用的就这些,其他要用再说吧
label.setFont(new Font("宋体",Font.ITALIC,14)); //设置字体、粗体、字号
label.setForeground(new Color(255,0,0)); //设置颜色,颜色是rgb颜色
label.setOpaque(true); //在这之前的背景都是默认为透明的,因此需要这行代码让他不透明
label.setBackground(new Color(100,200,200)); //设置背景色
label.setPreferredSize(new Dimension(200,30)); //设置控件大小,默认情况下是正好包含字的大小
label.setHorizontalAlignment(SwingConstants.CENTER); //关于对齐
这些好难记,我一般就打出Font.然后直接找,有粗体、斜体之类的。
IDEA还有直接给rgb颜色看的,就很方便。
但如果有多个控件,一个一个设置肯定太麻烦,可以设置一个private类(放在构造函数外面):
private static class ColorLabel extends JLabel
{
public ColorLabel(String text, Color bgColor)
{
this.setText(text);
this.setOpaque(true);
this.setBackground(bgColor); //设置背景色
this.setPreferredSize(new Dimension(50,40)); //设置控件大小,默认情况下是正好包含字的大小
this.setHorizontalAlignment(SwingConstants.CENTER); //关于对齐
}
}
构造函数内:
JLabel label1 = new ColorLabel("1",Color.YELLOW);
JLabel label2 = new ColorLabel("2",Color.MAGENTA);
JLabel label3 = new ColorLabel("3",Color.ORANGE);
JLabel label4 = new ColorLabel("4",Color.PINK);
root.add(label1);
root.add(label2);
root.add(label3);
root.add(label4);
然后就是这样子~



