转载:https://how2j.cn/stage/25.html
图形界面
hello swing
import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.PrintWriter;
import javax.swing.JButton;
import javax.swing.JFrame;
public class Test1 {
public static void main(String[] args) {
//保存位置的文件
File file = new File("C:\\Users\\LENOVO\\Desktop\\location.txt");
JFrame frame = new JFrame("LOL");
//读取上一次的位置
try {
BufferedReader br = new BufferedReader(new FileReader(file));
frame.setLocation(Integer.valueOf(br.readLine()).intValue(), Integer.valueOf(br.readLine()).intValue());
} catch (Exception e2) {
e2.printStackTrace();
}
// 设置窗口大小
frame.setSize(400, 300);
// 设置窗口位置
// frame.setLocation(550, 250);
frame.setLayout(null);
JButton button = new JButton("开启");
button.setBounds(50, 50, 280, 30);
frame.add(button);
// 关闭窗体的时候,退出程序
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
//开启一个线程记录实时位置
new Thread() {
public void run() {
while (true) {
try {
Thread.sleep(100);
} catch (InterruptedException e1) {
// TODO 自动生成的 catch 块
e1.printStackTrace();
}
//缓冲流写操作
//为什么只有两行数据呢,是因为每次new printwriter的时候会抹除之前的内容并从第一行的第一个位置开始写
try (PrintWriter pw = new PrintWriter(new FileWriter(file))) {
pw.println(frame.getX());
pw.println(frame.getY());
pw.flush();
} catch (Exception e) {
e.printStackTrace();
}
}
}
}.start();
}
}
事件监听
package gui;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
public class TestGUI {
public static void main(String[] args) {
JFrame f = new JFrame("LoL");
f.setSize(400, 300);
f.setLocation(580, 200);
f.setLayout(null);
final JLabel l = new JLabel();
ImageIcon i = new ImageIcon("e:/project/j2se/shana.png");
l.setIcon(i);
l.setBounds(50, 50, i.getIconWidth(), i.getIconHeight());
JButton b = new JButton("隐藏图片");
b.setBounds(150, 200, 100, 30);
// 给按钮 增加 监听
b.addActionListener(new ActionListener() {
// 当按钮被点击时,就会触发 ActionEvent事件
// actionPerformed 方法就会被执行,图片就不可见了
public void actionPerformed(ActionEvent e) {
l.setVisible(false);
}
});
f.add(l);
f.add(b);
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.setVisible(true);
}
}
package gui;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
public class TestGUI {
public static void main(String[] args) {
JFrame f = new JFrame("LoL");
f.setSize(400, 300);
f.setLocation(580, 200);
f.setLayout(null);
final JLabel l = new JLabel();
ImageIcon i = new ImageIcon("e:/project/j2se/shana.png");
l.setIcon(i);
l.setBounds(50, 50, i.getIconWidth(), i.getIconHeight());
// 增加键盘监听
f.addKeyListener(new KeyListener() {
// 键被弹起
public void keyReleased(KeyEvent e) {
// 39代表按下了 “右键”
if (e.getKeyCode() == 39) {
// 图片向右移动 (y坐标不变,x坐标增加)
l.setLocation(l.getX() + 10, l.getY());
}
}
//键被按下
public void keyPressed(KeyEvent e) {
// TODO Auto-generated method stub
}
// 一个按下弹起的组合动作
public void keyTyped(KeyEvent e) {
}
});
f.add(l);
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.setVisible(true);
}
}
package gui;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.util.Random;
import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JLabel;
public class TestGUI {
public static void main(String[] args) {
final JFrame f = new JFrame("LoL");
f.setSize(800, 600);
f.setLocationRelativeTo(null);
f.setLayout(null);
final JLabel l = new JLabel();
ImageIcon i = new ImageIcon("e:/project/j2se/shana_heiheihei.png");
l.setIcon(i);
l.setBounds(375, 275, i.getIconWidth(), i.getIconHeight());
f.add(l);
l.addMouseListener(new MouseListener() {
// 释放鼠标
public void mouseReleased(MouseEvent e) {
// TODO Auto-generated method stub
}
// 按下鼠标
public void mousePressed(MouseEvent e) {
// TODO Auto-generated method stub
}
// 鼠标退出
public void mouseExited(MouseEvent e) {
// TODO Auto-generated method stub
}
// 鼠标进入
public void mouseEntered(MouseEvent e) {
Random r = new Random();
int x = r.nextInt(f.getWidth() - l.getWidth());
int y = r.nextInt(f.getHeight() - l.getHeight());
l.setLocation(x, y);
}
// 按下释放组合动作为点击鼠标
public void mouseClicked(MouseEvent e) {
// TODO Auto-generated method stub
}
});
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.setVisible(true);
}
}
package gui;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.util.Random;
import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JLabel;
public class TestGUI {
public static void main(String[] args) {
final JFrame f = new JFrame("LoL");
f.setSize(800, 600);
f.setLocationRelativeTo(null);
f.setLayout(null);
final JLabel l = new JLabel("");
ImageIcon i = new ImageIcon("e:/project/j2se/shana_heiheihei.png");
l.setIcon(i);
l.setBounds(375, 275, i.getIconWidth(), i.getIconHeight());
f.add(l);
// MouseAdapter 适配器,只需要重写用到的方法,没有用到的就不用写了
l.addMouseListener(new MouseAdapter() {
// 只有mouseEntered用到了
public void mouseEntered(MouseEvent e) {
Random r = new Random();
int x = r.nextInt(f.getWidth() - l.getWidth());
int y = r.nextInt(f.getHeight() - l.getHeight());
l.setLocation(x, y);
}
});
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.setVisible(true);
}
}
在这里插入代码片
容器
package gui;
import javax.swing.JButton;
import javax.swing.JFrame;
public class TestGUI {
public static void main(String[] args) {
//普通的窗体,带最大和最小化按钮
JFrame f = new JFrame("LoL");
f.setSize(400, 300);
f.setLocation(200, 200);
f.setLayout(null);
JButton b = new JButton("一键秒对方基地挂");
b.setBounds(50, 50, 280, 30);
f.add(b);
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.setVisible(true);
}
}
package gui;
import javax.swing.JButton;
import javax.swing.JDialog;
public class TestGUI {
public static void main(String[] args) {
//普通的窗体,带最大和最小化按钮,而对话框却不带
JDialog d = new JDialog();
d.setTitle("LOL");
d.setSize(400, 300);
d.setLocation(200, 200);
d.setLayout(null);
JButton b = new JButton("一键秒对方基地挂");
b.setBounds(50, 50, 280, 30);
d.add(b);
d.setVisible(true);
}
}
package gui;
import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JFrame;
public class TestGUI {
public static void main(String[] args) {
JFrame f = new JFrame("外部窗体");
f.setSize(800, 600);
f.setLocation(100, 100);
// 根据外部窗体实例化JDialog
JDialog d = new JDialog(f);
// 设置为模态
d.setModal(true);
d.setTitle("模态的对话框");
d.setSize(400, 300);
d.setLocation(200, 200);
d.setLayout(null);
JButton b = new JButton("一键秒对方基地挂");
b.setBounds(50, 50, 280, 30);
d.add(b);
f.setVisible(true);
d.setVisible(true);
}
}
package gui;
import javax.swing.JButton;
import javax.swing.JFrame;
public class TestGUI {
public static void main(String[] args) {
JFrame f = new JFrame("LoL");
f.setSize(400, 300);
f.setLocation(200, 200);
f.setLayout(null);
JButton b = new JButton("一键秒对方基地挂");
b.setBounds(50, 50, 280, 30);
f.add(b);
// 窗体大小不可变化
f.setResizable(false);
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.setVisible(true);
}
}
在这里插入代码片
布局器
布局器是用在容器上的。 用来决定容器上的组件摆放的位置和大小
package gui;
import javax.swing.JButton;
import javax.swing.JFrame;
public class TestGUI {
public static void main(String[] args) {
JFrame f = new JFrame("LoL");
f.setSize(400, 300);
f.setLocation(200, 200);
// 设置布局器为null,即进行绝对定位,容器上的组件都需要指定位置和大小
f.setLayout(null);
JButton b1 = new JButton("英雄1");
// 指定位置和大小
b1.setBounds(50, 50, 80, 30);
JButton b2 = new JButton("英雄2");
b2.setBounds(150, 50, 80, 30);
JButton b3 = new JButton("英雄3");
b3.setBounds(250, 50, 80, 30);
// 没有指定位置和大小,不会出现在容器上
JButton b4 = new JButton("英雄3");
f.add(b1);
f.add(b2);
f.add(b3);
// b4没有指定位置和大小,不会出现在容器上
f.add(b4);
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.setVisible(true);
}
}
package gui;
import java.awt.FlowLayout;
import javax.swing.JButton;
import javax.swing.JFrame;
public class TestGUI {
public static void main(String[] args) {
JFrame f = new JFrame("LoL");
f.setSize(400, 300);
f.setLocation(200, 200);
// 设置布局器为FlowLayerout
// 容器上的组件水平摆放
f.setLayout(new FlowLayout());
JButton b1 = new JButton("英雄1");
JButton b2 = new JButton("英雄2");
JButton b3 = new JButton("英雄3");
// 加入到容器即可,无需单独指定大小和位置
f.add(b1);
f.add(b2);
f.add(b3);
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.setVisible(true);
}
}
package gui;
import java.awt.BorderLayout;
import java.awt.FlowLayout;
import javax.swing.JButton;
import javax.swing.JFrame;
public class TestGUI {
public static void main(String[] args) {
JFrame f = new JFrame("LoL");
f.setSize(400, 300);
f.setLocation(200, 200);
// 设置布局器为BorderLayerout
// 容器上的组件按照上北下南左西右东中的顺序摆放
f.setLayout(new BorderLayout());
JButton b1 = new JButton("洪七");
JButton b2 = new JButton("段智兴");
JButton b3 = new JButton("欧阳锋");
JButton b4 = new JButton("黄药师");
JButton b5 = new JButton("周伯通");
// 加入到容器的时候,需要指定位置
f.add(b1, BorderLayout.NORTH);
f.add(b2, BorderLayout.SOUTH);
f.add(b3, BorderLayout.WEST);
f.add(b4, BorderLayout.EAST);
f.add(b5, BorderLayout.CENTER);
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.setVisible(true);
}
}
package gui;
import java.awt.GridLayout;
import javax.swing.JButton;
import javax.swing.JFrame;
public class TestGUI {
public static void main(String[] args) {
JFrame f = new JFrame("LoL");
f.setSize(400, 300);
f.setLocation(200, 200);
// 设置布局器为GridLayerout,即网格布局器
// 该GridLayerout的构造方法表示该网格是2行3列
f.setLayout(new GridLayout(2, 3));
JButton b1 = new JButton("洪七");
JButton b2 = new JButton("段智兴");
JButton b3 = new JButton("欧阳锋");
JButton b4 = new JButton("黄药师");
JButton b5 = new JButton("周伯通");
f.add(b1);
f.add(b2);
f.add(b3);
f.add(b4);
f.add(b5);
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.setVisible(true);
}
}
package gui;
import java.awt.Dimension;
import java.awt.FlowLayout;
import javax.swing.JButton;
import javax.swing.JFrame;
public class TestGUI {
public static void main(String[] args) {
JFrame f = new JFrame("LoL");
f.setSize(400, 300);
f.setLocation(200, 200);
f.setLayout(new FlowLayout());
JButton b1 = new JButton("英雄1");
JButton b2 = new JButton("英雄2");
JButton b3 = new JButton("英雄3");
// 即便 使用 布局器 ,也可以 通过setPreferredSize,向布局器建议该组件显示的大小
b3.setPreferredSize(new Dimension(180, 40));
f.add(b1);
f.add(b2);
f.add(b3);
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.setVisible(true);
}
}
package swing.test;
import javax.swing.*;
import java.awt.*;
public class SimpleCom {
//使用布局器做出计算器上的按钮效果
public static void main(String[] args) {
JFrame jf=new JFrame("计算器");
jf.setBounds(50,50,300,200);
jf.setLayout(new GridLayout(4,5,8,8)); //4行5列。后面两个8是指每个块之间的缝隙
String[] arr={"7","8","9","/","sq","4","5","6","*","%","1","2","3","-","1/x","0","+/-",".","+","="};
for (int i = 0; i < 20; i++) {
jf.add(new JButton(arr[i]));
}
jf.setVisible(true);
}
}
组件
package gui;
import java.awt.Color;
import javax.swing.JFrame;
import javax.swing.JLabel;
public class TestGUI {
public static void main(String[] args) {
JFrame f = new JFrame("LoL");
f.setSize(400, 300);
f.setLocation(200, 200);
f.setLayout(null);
JLabel l = new JLabel("LOL文字");
//文字颜色
l.setForeground(Color.red);
l.setBounds(50, 50, 280, 30);
f.add(l);
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.setVisible(true);
}
}
package gui;
import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JLabel;
public class TestGUI {
public static void main(String[] args) {
JFrame f = new JFrame("LoL");
f.setSize(400, 300);
f.setLocation(580, 200);
f.setLayout(null);
JLabel l = new JLabel();
//根据图片创建ImageIcon对象
ImageIcon i = new ImageIcon("e:/project/j2se/shana.png");
//设置ImageIcon
l.setIcon(i);
//label的大小设置为ImageIcon,否则显示不完整
l.setBounds(50, 50, i.getIconWidth(), i.getIconHeight());
//i.getIconWidth(), i.getIconHeight()是指在label的宽度和长度根据i(背景图片)的宽度和长度而定,其坐标是(50,50)
f.add(l);
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.setVisible(true);
}
}
package gui;
import javax.swing.JButton;
import javax.swing.JFrame;
public class TestGUI {
public static void main(String[] args) {
JFrame f = new JFrame("LoL");
f.setSize(400, 300);
f.setLocation(200, 200);
f.setLayout(null);
JButton b = new JButton("一键秒对方基地挂");
b.setBounds(50, 50, 280, 30);
f.add(b);
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.setVisible(true);
}
}
package gui;
import javax.swing.ImageIcon;
import javax.swing.JCheckBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
public class TestGUI {
public static void main(String[] args) {
JFrame f = new JFrame("LoL");
f.setSize(400, 300);
f.setLocation(580, 200);
f.setLayout(null);
JCheckBox bCheckBox = new JCheckBox("物理英雄"); //复选框
//设置 为 默认被选中
bCheckBox.setSelected(true);
bCheckBox.setBounds(50, 50, 130, 30);
JCheckBox bCheckBox2 = new JCheckBox("魔法 英雄");
bCheckBox2.setBounds(50, 100, 130, 30);
//判断 是否 被 选中
System.out.println(bCheckBox2.isSelected());
f.add(bCheckBox);
f.add(bCheckBox2);
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.setVisible(true);
}
}
package gui;
import javax.swing.ImageIcon;
import javax.swing.JCheckBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JRadioButton;
public class TestGUI {
public static void main(String[] args) {
JFrame f = new JFrame("LoL");
f.setSize(400, 300);
f.setLocation(580, 200);
f.setLayout(null);
JRadioButton b1 = new JRadioButton("物理英雄"); //单选框
// 设置 为 默认被选中
b1.setSelected(true);
b1.setBounds(50, 50, 130, 30);
JRadioButton b2 = new JRadioButton("魔法 英雄");
b2.setBounds(50, 100, 130, 30);
System.out.println(b2.isSelected()); //判断是否被选中
f.add(b1);
f.add(b2);
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.setVisible(true);
}
}
package gui;
import javax.swing.ButtonGroup;
import javax.swing.ImageIcon;
import javax.swing.JCheckBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JRadioButton;
public class TestGUI {
public static void main(String[] args) {
JFrame f = new JFrame("LoL");
f.setSize(400, 300);
f.setLocation(580, 200);
f.setLayout(null);
JRadioButton b1 = new JRadioButton("物理英雄"); //单选框
// 设置 为 默认被选中
b1.setSelected(true);
b1.setBounds(50, 50, 130, 30);
JRadioButton b2 = new JRadioButton("魔法 英雄");
b2.setBounds(50, 100, 130, 30);
// 按钮分组
ButtonGroup bg = new ButtonGroup();
// 把b1,b2放在 同一个 按钮分组对象里 ,这样同一时间,只有一个 按钮 会被选中,而JRadioButton同一时间可以选中好几个
bg.add(b1);
bg.add(b2);
f.add(b1);
f.add(b2);
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.setVisible(true);
}
}
package gui;
import javax.swing.JComboBox;
import javax.swing.JFrame;
public class TestGUI {
public static void main(String[] args) {
JFrame f = new JFrame("LoL");
f.setSize(400, 300);
f.setLocation(580, 240);
f.setLayout(null);
//下拉框出现的条目
String[] heros = new String[] { "卡特琳娜", "库奇" };
JComboBox cb = new JComboBox(heros);
cb.setBounds(50, 50, 80, 30);
f.add(cb);
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.setVisible(true);
}
}
package gui;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
public class TestGUI {
public static void main(String[] args) {
JFrame f = new JFrame("LoL");
f.setSize(400, 300);
f.setLocation(580, 240);
f.setLayout(null);
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.setVisible(true);
int option = JOptionPane.showConfirmDialog(f, "是否 使用外挂 ?");
if (JOptionPane.OK_OPTION == option) {
String answer = JOptionPane.showInputDialog(f, "请输入yes,表明使用外挂后果自负");
if ("yes".equals(answer))
JOptionPane.showMessageDialog(f, "你使用外挂被抓住! 罚拣肥皂3次!");
}
}
}
package gui;
import java.awt.Dimension;
import java.awt.FlowLayout;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JTextField;
public class TestGUI {
public static void main(String[] args) {
JFrame f = new JFrame("LoL");
f.setSize(400, 300);
f.setLocation(200, 200);
f.setLayout(new FlowLayout());
JLabel lName = new JLabel("账号:");
// 输入框
JTextField tfName = new JTextField("");
tfName.setText("请输入账号");
tfName.setPreferredSize(new Dimension(80, 30));
JLabel lPassword = new JLabel("密码:");
// 输入框
JTextField tfPassword = new JTextField("");
tfPassword.setText("请输入密码");
tfPassword.setPreferredSize(new Dimension(80, 30));
f.add(lName);
f.add(tfName);
f.add(lPassword);
f.add(tfPassword);
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.setVisible(true);
tfPassword.grabFocus();
}
}
package gui;
import java.awt.Dimension;
import java.awt.FlowLayout;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPasswordField;
public class TestGUI {
public static void main(String[] args) {
JFrame f = new JFrame("LoL");
f.setSize(400, 300);
f.setLocation(200, 200);
f.setLayout(new FlowLayout());
JLabel l = new JLabel("密码:");
// 密码框
JPasswordField pf = new JPasswordField("");
pf.setText("&48kdh4@#");
pf.setPreferredSize(new Dimension(80, 30));
// 与文本框不同,获取密码框里的内容,推荐使用getPassword,该方法会返回一个字符数组,而非字符串
char[] password = pf.getPassword();
String p = String.valueOf(password);
System.out.println(p);
f.add(l);
f.add(pf);
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.setVisible(true);
}
}
package gui;
import java.awt.Dimension;
import java.awt.FlowLayout;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JTextArea;
public class TestGUI {
public static void main(String[] args) {
JFrame f = new JFrame("LoL");
f.setSize(400, 300);
f.setLocation(200, 200);
f.setLayout(new FlowLayout());
JLabel l = new JLabel("文本域:");
JTextArea ta = new JTextArea();
ta.setPreferredSize(new Dimension(200, 150));
//\n换行符
ta.setText("抢人头!\n抢你妹啊抢!\n");
//追加数据
ta.append("我去送了了了了了了了了了了了了了了了了了了了了了了了了");
//设置自动换行
ta.setLineWrap(true);
f.add(l);
f.add(ta);
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.setVisible(true);
}
}
package gui;
import java.awt.Dimension;
import java.awt.FlowLayout;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JProgressBar;
import javax.swing.JTextArea;
public class TestGUI {
public static void main(String[] args) {
JFrame f = new JFrame("LoL");
f.setSize(400, 300);
f.setLocation(200, 200);
f.setLayout(new FlowLayout());
JProgressBar pb = new JProgressBar();
//进度条最大100
pb.setMaximum(100);
//当前进度是50
pb.setValue(50);
//显示当前进度
pb.setStringPainted(true);
f.add(pb);
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.setVisible(true);
}
}
package gui;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;
import javax.swing.JButton;
import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.filechooser.FileFilter;
public class TestGUI {
public static void main(String[] args) {
final JFrame f = new JFrame("LOL");
f.setLayout(new FlowLayout());
final JFileChooser fc = new JFileChooser();
fc.setFileFilter(new FileFilter() {
@Override
public String getDescription() {
// TODO Auto-generated method stub
return ".txt";
}
@Override
public boolean accept(File f) {
return f.getName().toLowerCase().endsWith(".txt");
}
});
JButton bOpen = new JButton("打开文件");
JButton bSave = new JButton("保存文件");
f.add(bOpen);
f.add(bSave);
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.setSize(250, 150);
f.setLocationRelativeTo(null);
f.setVisible(true);
bOpen.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
int returnVal = fc.showOpenDialog(f);
File file = fc.getSelectedFile();
if (returnVal == JFileChooser.APPROVE_OPTION) {
JOptionPane.showMessageDialog(f, "计划打开文件:" + file.getAbsolutePath());
}
}
});
bSave.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
int returnVal = fc.showSaveDialog(f);
File file = fc.getSelectedFile();
if (returnVal == JFileChooser.APPROVE_OPTION) {
JOptionPane.showMessageDialog(f, "计划保存到文件:" + file.getAbsolutePath());
}
}
});
}
}
面板
package gui;
import java.awt.Color;
import java.awt.FlowLayout;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
public class TestGUI {
public static void main(String[] args) {
JFrame f = new JFrame("LoL");
f.setSize(400, 300);
f.setLocation(200, 200);
f.setLayout(null);
JPanel p1 = new JPanel();
// 设置面板大小
p1.setBounds(50, 50, 300, 60);
// 设置面板背景颜色
p1.setBackground(Color.RED);
// 这一句可以没有,因为JPanel默认就是采用的FlowLayout
p1.setLayout(new FlowLayout());
JButton b1 = new JButton("英雄1");
JButton b2 = new JButton("英雄2");
JButton b3 = new JButton("英雄3");
// 把按钮加入面板
p1.add(b1);
p1.add(b2);
p1.add(b3);
JPanel p2 = new JPanel();
JButton b4 = new JButton("英雄4");
JButton b5 = new JButton("英雄5");
JButton b6 = new JButton("英雄6");
p2.add(b4);
p2.add(b5);
p2.add(b6);
p2.setBackground(Color.BLUE);
p2.setBounds(10, 150, 300, 60);
// 把面板加入窗口
f.add(p1);
f.add(p2);
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.setVisible(true);
}
}
package gui;
import javax.swing.JButton;
import javax.swing.JFrame;
public class TestGUI {
public static void main(String[] args) {
JFrame f = new JFrame("LoL");
f.setSize(400, 300);
f.setLocation(200, 200);
f.setLayout(null);
JButton b = new JButton("一键秒对方基地挂");
b.setBounds(50, 50, 280, 30);
f.add(b);
// JFrame上有一层面板,叫做ContentPane
// 平时通过f.add()向JFrame增加组件,其实是向JFrame上的 ContentPane加东西
// f.add等同于f.getContentPane().add(b);
f.getContentPane().add(b);
// b.getParent()获取按钮b所处于的容器
// 打印出来可以看到,实际上是ContentPane而非JFrame
System.out.println(b.getParent());
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.setVisible(true);
}
}
package gui;
import java.awt.Color;
import java.awt.FlowLayout;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JSplitPane;
public class TestGUI {
public static void main(String[] args) {
JFrame f = new JFrame("LoL");
f.setSize(400, 300);
f.setLocation(200, 200);
f.setLayout(null);
JPanel pLeft = new JPanel();
pLeft.setBounds(50, 50, 300, 60);
pLeft.setBackground(Color.RED);
pLeft.setLayout(new FlowLayout());
JButton b1 = new JButton("盖伦");
JButton b2 = new JButton("提莫");
JButton b3 = new JButton("安妮");
pLeft.add(b1);
pLeft.add(b2);
pLeft.add(b3);
JPanel pRight = new JPanel();
JButton b4 = new JButton("英雄4");
JButton b5 = new JButton("英雄5");
JButton b6 = new JButton("英雄6");
pRight.add(b4);
pRight.add(b5);
pRight.add(b6);
pRight.setBackground(Color.BLUE);
pRight.setBounds(10, 150, 300, 60);
// 创建一个水平JSplitPane,左边是p1,右边是p2
JSplitPane sp = new JSplitPane(JSplitPane.HORIZONTAL_SPLIT, pLeft, pRight);
// 设置分割条的位置
sp.setDividerLocation(80);
// 把sp当作ContentPane
f.setContentPane(sp);
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.setVisible(true);
}
}
package gui;
import javax.swing.JFrame;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
public class TestGUI {
public static void main(String[] args) {
JFrame f = new JFrame("LoL");
f.setSize(400, 300);
f.setLocation(200, 200);
f.setLayout(null);
//准备一个文本域,在里面放很多数据
JTextArea ta = new JTextArea();
for (int i = 0; i < 1000; i++) {
ta.append(String.valueOf(i));
}
//自动换行
ta.setLineWrap(true);
JScrollPane sp = new JScrollPane(ta);
f.setContentPane(sp);
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.setVisible(true);
}
}
package gui;
import java.awt.Color;
import java.awt.FlowLayout;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JTabbedPane;
public class TestGUI {
public static void main(String[] args) {
JFrame f = new JFrame("LoL");
f.setSize(400, 300);
f.setLocation(200, 200);
f.setLayout(null);
JPanel p1 = new JPanel();
p1.setBounds(50, 50, 300, 60);
p1.setBackground(Color.RED);
p1.setLayout(new FlowLayout());
JButton b1 = new JButton("英雄1");
JButton b2 = new JButton("英雄2");
JButton b3 = new JButton("英雄3");
p1.add(b1);
p1.add(b2);
p1.add(b3);
JPanel p2 = new JPanel();
JButton b4 = new JButton("英雄4");
JButton b5 = new JButton("英雄5");
JButton b6 = new JButton("英雄6");
p2.add(b4);
p2.add(b5);
p2.add(b6);
p2.setBackground(Color.BLUE);
p2.setBounds(10, 150, 300, 60);
JTabbedPane tp = new JTabbedPane();
tp.add(p1);
tp.add(p2);
// 设置tab的标题
tp.setTitleAt(0, "红色tab");
tp.setTitleAt(1, "蓝色tab");
ImageIcon i = new ImageIcon("e:/project/j2se/j.png");
tp.setIconAt(0,i );
tp.setIconAt(1,i );
f.setContentPane(tp);
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.setVisible(true);
}
}
package gui;
import java.awt.BorderLayout;
import java.awt.CardLayout;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JTextField;
public class TestGUI {
public static void main(String[] args) {
JFrame f = new JFrame("CardLayerout");
JPanel comboBoxPane = new JPanel();
String buttonPanel = "按钮面板";
String inputPanel = "输入框面板";
String comboBoxItems[] = { buttonPanel, inputPanel };
JComboBox<String> cb = new JComboBox<>(comboBoxItems);
comboBoxPane.add(cb);
// 两个Panel充当卡片
JPanel card1 = new JPanel();
card1.add(new JButton("按钮 1"));
card1.add(new JButton("按钮 2"));
card1.add(new JButton("按钮 3"));
JPanel card2 = new JPanel();
card2.add(new JTextField("输入框", 20));
final JPanel cards; // a panel that uses CardLayout
cards = new JPanel(new CardLayout());
cards.add(card1, buttonPanel);
cards.add(card2, inputPanel);
f.add(comboBoxPane, BorderLayout.NORTH);
f.add(cards, BorderLayout.CENTER);
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.setSize(250, 150);
f.setLocationRelativeTo(null);
f.setVisible(true);
cb.addItemListener(new ItemListener() {
@Override
public void itemStateChanged(ItemEvent evt) {
CardLayout cl = (CardLayout) (cards.getLayout());
cl.show(cards, (String) evt.getItem());
}
});
}
}
组件综合练习
package gui;
import java.awt.BorderLayout;
import java.awt.CardLayout;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTextField;
public class TestGUI {
public static void main(String[] args) {
final JFrame f = new JFrame("LoL");
f.setSize(400, 300);
f.setLocation(200, 200);
f.setLayout(new FlowLayout());
// 输入框
final JTextField tfName = new JTextField();
tfName.setPreferredSize(new Dimension(80, 30));
//按钮
final JButton b = new JButton("检测");
b.setPreferredSize(new Dimension(80, 30));
//给按钮增加监听,点击按钮才有效果
b.addActionListener(new ActionListener() {
// 当按钮被点击时,就会触发 ActionEvent事件
// actionPerformed 方法就会被执行
@Override
public void actionPerformed(ActionEvent e) {
String text=tfName.getText();
if(text.equals("")){
JOptionPane.showMessageDialog(f, "文本内容为空"); // JOptionPane 用于弹出对话框
}
else{
JOptionPane.showMessageDialog(f, "检测到输入为:"+text);
}
} });
f.add(tfName);
f.add(b);
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.setVisible(true);
}
}
package gui;
import java.awt.BorderLayout;
import java.awt.CardLayout;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTextField;
public class TestGUI {
public static void main(String[] args) {
final JFrame f = new JFrame("LoL");
f.setSize(400, 300);
f.setLocation(200, 200);
f.setLayout(new FlowLayout());
// 输入框
final JTextField tfName = new JTextField();
tfName.setPreferredSize(new Dimension(80, 30));
//按钮
final JButton b = new JButton("检测");
b.setPreferredSize(new Dimension(80, 30));
//给按钮增加监听,点击按钮才有效果
b.addActionListener(new ActionListener() {
// 当按钮被点击时,就会触发 ActionEvent事件
// actionPerformed 方法就会被执行
@Override
public void actionPerformed(ActionEvent e) {
String text=tfName.getText();
// if(text.matches(".*\\d.*")){
// d:用于匹配从0到9的数字;
// *:表示其前面字符出现0次或多次
// .:匹配除 "\n" 之外的任何单个字符
// text.matches(".*\\d.*")表示任意长度任意文本只要出现一次数字(d)就符合,
if(text.matches("[0-9]*")){ //此处用的正则表达式,判断字符串是否只包含数字
JOptionPane.showMessageDialog(f,"检测到输入为:"+text); // JOptionPane 用于弹出对话框
}
else{
JOptionPane.showMessageDialog(f,"输入框里的数据不全是数字");
}
} });
f.add(tfName);
f.add(b);
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.setVisible(true);
}
}
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
public class Login {
String name;
String password;
public static boolean login(Login l) {
String name=l.name;
String password=l.password;
boolean str = true;
try {
Class.forName("com.mysql.jdbc.Driver");
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
try (Connection c=DriverManager.getConnection("jdbc:mysql://127.0.0.1:3306/how2java?characterEncoding=UTF-8",
"root","admin");Statement s=c.createStatement()){
String sql = "select * from user where name = '" + name +"' and password = '" + password+"'";
ResultSet rs=s.executeQuery(sql);
if (rs.next()) {
str=true;
}else {
str=false;
}
} catch (SQLException e) {
e.printStackTrace();
}
return str;
}
import java.awt.Dimension;
import java.awt.FlowLayout;
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.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JPasswordField;
import javax.swing.JTextField;
public class practice3 extends Login{
/**
* 准备两个JTextFiled,一个用于输入账号,一个用于输入密码。 再准备一个JButton,上面的文字是登陆
* 点击按钮之后,首先进行为空判断,如果都不为空, 则把账号和密码,拿到数据库中进行比较(SQL语句判断账号密码是否正确)
* 根据判断结果,使用JOptionPane进行提示。s
*/
public static void main(String[] args) {
JFrame f = new JFrame("LOL");
f.setSize(400, 300);
f.setLocation(500, 500);
f.setLayout(null);
JPanel p = new JPanel();
p.setSize(400, 30);
p.setLayout(new FlowLayout());
JLabel l1 = new JLabel("账号:");
JLabel l2 = new JLabel("密码:");
JButton b = new JButton("登录");
b.setBounds(150, 50, 80, 30);
JTextField tf = new JTextField();
tf.setPreferredSize(new Dimension(80, 30));
tf.setText("请输入账号");
JTextField tp = new JPasswordField();
tp.setPreferredSize(new Dimension(80, 30));
tp.setText("请输入密码");
b.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
Login user= new Login();
user.name = tf.getText();
user.password = tp.getText();
if (user.name.equals("")) {
JOptionPane.showMessageDialog(f, "账号不能为空");
} else if (user.password.equals("")) {
JOptionPane.showMessageDialog(f, "密码不能为空");
} else {
if(login(user)) { //这个类继承了login这个类
JOptionPane.showMessageDialog(f, "登录成功");
}else {
JOptionPane.showMessageDialog(f, "账号或密码错误");
}
}
}
});
p.add(l1);
p.add(tf);
p.add(l2);
p.add(tp);
f.add(p);
f.add(b);
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.setVisible(true);
}
}
package GUI;
import javax.swing.*;
import java.awt.*;
import java.util.ArrayList;
public class AutoText {
public static boolean checkIsBlank(ArrayList<JTextField> jt){
for(JTextField t : jt){
if(t.getText().isBlank()) return true;
}
return false;
}
public static void main(String[] args){
JFrame jf = new JFrame("AutoText");
jf.setBounds(500,200,430,500);
jf.setLayout(null);
JPanel jp1 = new JPanel();
jp1.setBounds(8,8,400,150);
jp1.setLayout(new FlowLayout(FlowLayout.LEFT,8,8));
ArrayList<String> strs = new ArrayList<>(){
{
add("姓名:");
add("公司类型:");
add("公司名称:");
add("老板名称:");
add("金额:");
add("产品:");
add("价格计量单位:");
}
};
ArrayList<JLabel> jl = new ArrayList<>();
ArrayList<JTextField> jt = new ArrayList<>();
for(int i = 0 ; i < strs.size();i++){
JLabel l = new JLabel(strs.get(i));
l.setPreferredSize(new Dimension(90,30));
JTextField t = new JTextField("");
t.setPreferredSize(new Dimension(90,30));
jl.add(l);
jt.add(t);
jp1.add(jl.get(i));
jp1.add(jt.get(i));
}
jf.add(jp1);
JPanel jp3 = new JPanel();
jp3.setBounds(8,220,400,270);
JTextArea ja = new JTextArea("");
ja.setPreferredSize(new Dimension(400,200));
ja.setLineWrap(true);
jp3.add(ja);
jf.add(jp3);
JPanel jp2 = new JPanel();
jp2.setBounds(8,170,400,40);
JButton jb = new JButton("生成");
jb.addActionListener(e->{
if(!checkIsBlank(jt)){
ArrayList<String> newStr = new ArrayList<>();
for(JTextField t : jt){
newStr.add(t.getText());
}
String s = String.format(" %s最大%s%s倒闭了,王八蛋老板%s吃喝嫖赌,欠下了%s个亿," +
"带着他的小姨子跑了!我们没有办法,拿着%s抵工资,原价都是一%s多、二%s多、三%s多" +
"的代码,现在通通只卖二十块,通通只卖二十块!%s王八蛋,你不是人!我们辛辛苦苦给你" +
"干了大半年,你不发工资,你还我血汗钱,还我血汗钱!",newStr.get(0),newStr.get(1),
newStr.get(2),newStr.get(3),newStr.get(4),newStr.get(5),newStr.get(6),
newStr.get(6),newStr.get(6),newStr.get(3));
ja.setText(s);
}
});
jp2.add(jb);
jf.add(jp2);
jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
jf.setVisible(true);
}
}
package 组件综合练习;
import java.awt.Dimension;
import java.awt.FlowLayout;
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.JPanel;
import javax.swing.JTextArea;
import javax.swing.JTextField;
public class practice4 {
public static void main(String[] args) {
JFrame f=new JFrame("LOL");
f.setSize(400,500);
f.setLocation(500,500);
f.setLayout(null);
JPanel p1=new JPanel();
p1.setBounds(0, 0, 90, 170);
p1.setLayout(new FlowLayout(FlowLayout.LEFT,10,18));
JPanel p2=new JPanel();
p2.setBounds(80, 0, 110, 170);
p2.setLayout(new FlowLayout(FlowLayout.LEFT,20,15));
JPanel p3=new JPanel();
p3.setBounds(190, 0, 80, 170);
p3.setLayout(new FlowLayout(FlowLayout.LEFT,10,18));
JPanel p4=new JPanel();
p4.setBounds(260, 0, 110, 170);
p4.setLayout(new FlowLayout(FlowLayout.LEFT,20,15));
JLabel l1=new JLabel("地名:");
JLabel l2=new JLabel("公司类型:");
JLabel l3=new JLabel("公司名称:");
JLabel l4=new JLabel("老板名称:");
JLabel l5=new JLabel("金额:");
JLabel l6=new JLabel("产品:");
JLabel l7=new JLabel("价格计量单位:");
JTextField tf1=new JTextField();
tf1.setPreferredSize(new Dimension(90,22));
JTextField tf2=new JTextField();
tf2.setPreferredSize(new Dimension(90,22));
JTextField tf3=new JTextField();
tf3.setPreferredSize(new Dimension(90,22));
JTextField tf4=new JTextField();
tf4.setPreferredSize(new Dimension(90,22));
JTextField tf5=new JTextField();
tf5.setPreferredSize(new Dimension(90,22));
JTextField tf6=new JTextField();
tf6.setPreferredSize(new Dimension(90,22));
JTextField tf7=new JTextField();
tf7.setPreferredSize(new Dimension(90,22));
p1.add(l1);p1.add(l3);p1.add(l5);p1.add(l7);
p2.add(tf1);p2.add(tf3);p2.add(tf5);p2.add(tf7);
p3.add(l2);p3.add(l4);p3.add(l6);
p4.add(tf2);p4.add(tf4);p4.add(tf6);
f.add(p1);f.add(p2);f.add(p3);f.add(p4);
JButton b=new JButton("生成");
b.setBounds(160,170,80,30);
JTextArea ta=new JTextArea();
ta.setBounds(10, 220, 365, 230);
ta.setLineWrap(true);
b.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
String str=tf1.getText()+"最大"+tf2.getText()+tf3.getText()+"倒闭了,"
+ "王八蛋老板"+tf4.getText()+"吃喝嫖赌,欠下了"+tf5.getText()+","
+ "带着他的小姨子跑了!我们没有办法,拿着"+tf6.getText()+"抵工资!"
+ "原价都是一"+tf7.getText()+"多、两"+tf7.getText()+"多、三"+tf7.getText()+"多的"+tf6.getText()+","
+ "现在全部只卖二十块,统统只要二十块!"+tf4.getText()+"王八蛋,"
+ "你不是人!我们辛辛苦苦给你干了大半年,你不发工资,你还我血汗钱,还我血汗钱!";
ta.setText(str);
}
});
f.add(b);
f.add(ta);
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.setVisible(true);
}
}
菜单
GUI的菜单分为 菜单栏,菜单和菜单项
package gui;
import javax.swing.JFrame;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
public class TestGUI {
public static void main(String[] args) {
JFrame f = new JFrame("LoL");
f.setSize(400, 300);
f.setLocation(200, 200);
// 菜单栏
JMenuBar mb = new JMenuBar();
// 菜单
JMenu mHero = new JMenu("英雄");
JMenu mItem = new JMenu("道具");
JMenu mWord = new JMenu("符文");
JMenu mSummon = new JMenu("召唤师");
JMenu mTalent = new JMenu("天赋树");
// 把菜单加入到菜单栏
mb.add(mHero);
mb.add(mItem);
mb.add(mWord);
mb.add(mSummon);
mb.add(mTalent);
// 把菜单栏加入到frame,这里用的是set而非add
f.setJMenuBar(mb);
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.setVisible(true);
}
}
package gui;
import javax.swing.JFrame;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
public class TestGUI {
public static void main(String[] args) {
JFrame f = new JFrame("LoL");
f.setSize(400, 400);
f.setLocation(200, 200);
JMenuBar mb = new JMenuBar();
JMenu mHero = new JMenu("英雄");
JMenu mItem = new JMenu("道具");
JMenu mWord = new JMenu("符文");
JMenu mSummon = new JMenu("召唤师");
JMenu mTalent = new JMenu("天赋树");
// 菜单项
mHero.add(new JMenuItem("近战-Warriar"));
mHero.add(new JMenuItem("远程-Range"));
mHero.add(new JMenuItem("物理-physical"));
mHero.add(new JMenuItem("坦克-Tank"));
mHero.add(new JMenuItem("法系-Mage"));
mHero.add(new JMenuItem("辅助-Support"));
mHero.add(new JMenuItem("打野-Jungle"));
mHero.add(new JMenuItem("突进-Charge"));
mHero.add(new JMenuItem("男性-Boy"));
mHero.add(new JMenuItem("女性-Girl"));
// 分隔符
mHero.addSeparator();
mHero.add(new JMenuItem("所有-All"));
mb.add(mHero);
mb.add(mItem);
mb.add(mWord);
mb.add(mSummon);
mb.add(mTalent);
f.setJMenuBar(mb);
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.setVisible(true);
}
}
package gui;
import javax.swing.JFrame;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JTextArea;
public class TestGUI {
public static void main(String[] args) {
JFrame f=new JFrame("记事本");
f.setSize(550,550);
f.setLocation(500,300);
JMenuBar mb=new JMenuBar();
JMenu file=new JMenu("文件(F)");
JMenu edit=new JMenu("编辑(E)");
JMenu layout=new JMenu("格式(O)");
JMenu check=new JMenu("查看(V)");
JMenu help=new JMenu("帮助(H)");
JMenu[] mbs= {file,edit,layout,check,help};
file.add(new JMenuItem(" 新建(N) Ctrl+N "));
file.add(new JMenuItem(" 打开(O)... Ctrl+O "));
file.add(new JMenuItem(" 保存(S) Ctrl+S "));
file.add(new JMenuItem(" 另存为(A)... "));
file.addSeparator();
file.add(new JMenuItem(" 页面设置(U) "));
file.add(new JMenuItem(" 打印(P)... Ctrl+O "));
file.addSeparator();
file.add(new JMenuItem(" 退出(X) "));
edit.add(new JMenuItem(" 撤销(U) Ctrl+N "));
edit.addSeparator();
edit.add(new JMenuItem(" 剪切(T)... Ctrl+X "));
edit.add(new JMenuItem(" 复制(C) Ctrl+C "));
edit.add(new JMenuItem(" 粘贴(P)... Ctrl+V "));
edit.add(new JMenuItem(" 删除(D) Del "));
edit.addSeparator();
edit.add(new JMenuItem(" 使用Bing搜索(P)... Ctrl+E "));
edit.add(new JMenuItem(" 查找(X) Ctrl+F "));
edit.add(new JMenuItem(" 查找下一个(X) F3 "));
edit.add(new JMenuItem(" 替换(X) Ctrl+H "));
edit.add(new JMenuItem(" 转到(X) Ctrl+G "));
edit.addSeparator();
edit.add(new JMenuItem(" 全选(X) Ctrl+A "));
edit.add(new JMenuItem(" 时间/日期(X) F5 "));
layout.add(new JMenuItem(" 自动换行(W) "));
layout.add(new JMenuItem(" 字体(F)... "));
JMenu zoom=new JMenu(" 缩放(Z) ");
check.add(zoom);
zoom.add(new JMenuItem(" 放大 Ctrl+Plus "));
zoom.add(new JMenuItem(" 缩小 Ctrl+Minus "));
zoom.add(new JMenuItem(" 恢复默认缩放 Ctrl+0 "));
check.add(new JMenuItem(" 状态栏(S) "));
help.add(new JMenuItem(" 查看帮助(H) "));
help.addSeparator();
help.add(new JMenuItem(" 关于记事本(A) "));
for (int i = 0; i < mbs.length; i++) {
mb.add(mbs[i]);
}
JTextArea ta=new JTextArea();
f.setJMenuBar(mb);
f.add(ta);
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.setVisible(true);
}
}
工具栏
工具栏用于存放常用的按钮
package gui;
import java.awt.BorderLayout;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JToolBar;
public class TestGUI {
public static void main(String[] args) {
JFrame f = new JFrame("LoL");
f.setSize(400, 300);
f.setLocation(200, 200);
// 菜单
addMenu(f);
// 工具栏
JToolBar tb = new JToolBar();
// 为工具栏增加按钮
JButton b1 = new JButton(new ImageIcon("e:/project/j2se/1.jpg"));
JButton b2 = new JButton(new ImageIcon("e:/project/j2se/2.jpg"));
JButton b3 = new JButton(new ImageIcon("e:/project/j2se/3.jpg"));
JButton b4 = new JButton(new ImageIcon("e:/project/j2se/4.jpg"));
JButton b5 = new JButton(new ImageIcon("e:/project/j2se/5.jpg"));
JButton b6 = new JButton(new ImageIcon("e:/project/j2se/6.jpg"));
tb.add(b1);
tb.add(b2);
tb.add(b3);
tb.add(b4);
tb.add(b5);
tb.add(b6);
// 把工具栏放在north的位置
f.setLayout(new BorderLayout());
f.add(tb, BorderLayout.NORTH);
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.setVisible(true);
}
private static void addMenu(JFrame f) {
JMenuBar mb = new JMenuBar();
JMenu mHero = new JMenu("英雄");
JMenu mItem = new JMenu("道具");
JMenu mWord = new JMenu("符文");
JMenu mSummon = new JMenu("召唤师");
JMenu mTalent = new JMenu("天赋树");
// 菜单项
mHero.add(new JMenuItem("近战-Warriar"));
mHero.add(new JMenuItem("远程-Range"));
mHero.add(new JMenuItem("物理-physical"));
mHero.add(new JMenuItem("坦克-Tank"));
mHero.add(new JMenuItem("法系-Mage"));
mHero.add(new JMenuItem("辅助-Support"));
mHero.add(new JMenuItem("打野-Jungle"));
mHero.add(new JMenuItem("突进-Charge"));
mHero.add(new JMenuItem("男性-Boy"));
mHero.add(new JMenuItem("女性-Girl"));
mb.add(mHero);
mb.add(mItem);
mb.add(mWord);
mb.add(mSummon);
mb.add(mTalent);
f.setJMenuBar(mb);
}
}
package gui;
import java.awt.BorderLayout;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JToolBar;
public class TestGUI {
public static void main(String[] args) {
JFrame f = new JFrame("LoL");
f.setSize(400, 300);
f.setLocation(200, 200);
addMenu(f);
JToolBar tb = new JToolBar();
JButton b1 = new JButton(new ImageIcon("e:/project/j2se/1.jpg"));
JButton b2 = new JButton(new ImageIcon("e:/project/j2se/2.jpg"));
JButton b3 = new JButton(new ImageIcon("e:/project/j2se/3.jpg"));
JButton b4 = new JButton(new ImageIcon("e:/project/j2se/4.jpg"));
JButton b5 = new JButton(new ImageIcon("e:/project/j2se/5.jpg"));
JButton b6 = new JButton(new ImageIcon("e:/project/j2se/6.jpg"));
tb.add(b1);
tb.add(b2);
tb.add(b3);
tb.add(b4);
tb.add(b5);
tb.add(b6);
// 给按钮设置提示信息
b1.setToolTipText("坑爹英雄");
// 把工具栏放在north的位置
f.setLayout(new BorderLayout());
f.add(tb, BorderLayout.NORTH);
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.setVisible(true);
}
private static void addMenu(JFrame f) {
JMenuBar mb = new JMenuBar();
JMenu mHero = new JMenu("英雄");
JMenu mItem = new JMenu("道具");
JMenu mWord = new JMenu("符文");
JMenu mSummon = new JMenu("召唤师");
JMenu mTalent = new JMenu("天赋树");
// 菜单项
mHero.add(new JMenuItem("近战-Warriar"));
mHero.add(new JMenuItem("远程-Range"));
mHero.add(new JMenuItem("物理-physical"));
mHero.add(new JMenuItem("坦克-Tank"));
mHero.add(new JMenuItem("法系-Mage"));
mHero.add(new JMenuItem("辅助-Support"));
mHero.add(new JMenuItem("打野-Jungle"));
mHero.add(new JMenuItem("突进-Charge"));
mHero.add(new JMenuItem("男性-Boy"));
mHero.add(new JMenuItem("女性-Girl"));
mb.add(mHero);
mb.add(mItem);
mb.add(mWord);
mb.add(mSummon);
mb.add(mTalent);
f.setJMenuBar(mb);
}
}
package gui;
import java.awt.BorderLayout;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JToolBar;
public class TestGUI {
public static void main(String[] args) {
JFrame f = new JFrame("LoL");
f.setSize(400, 300);
f.setLocation(200, 200);
addMenu(f);
JToolBar tb = new JToolBar();
JButton b1 = new JButton(new ImageIcon("e:/project/j2se/1.jpg"));
JButton b2 = new JButton(new ImageIcon("e:/project/j2se/2.jpg"));
JButton b3 = new JButton(new ImageIcon("e:/project/j2se/3.jpg"));
JButton b4 = new JButton(new ImageIcon("e:/project/j2se/4.jpg"));
JButton b5 = new JButton(new ImageIcon("e:/project/j2se/5.jpg"));
JButton b6 = new JButton(new ImageIcon("e:/project/j2se/6.jpg"));
tb.add(b1);
tb.add(b2);
tb.add(b3);
tb.add(b4);
tb.add(b5);
tb.add(b6);
b1.setToolTipText("坑爹英雄");
// 禁止工具栏拖动
tb.setFloatable(false);
// 把工具栏放在north的位置
f.setLayout(new BorderLayout());
f.add(tb, BorderLayout.NORTH);
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.setVisible(true);
}
private static void addMenu(JFrame f) {
JMenuBar mb = new JMenuBar();
JMenu mHero = new JMenu("英雄");
JMenu mItem = new JMenu("道具");
JMenu mWord = new JMenu("符文");
JMenu mSummon = new JMenu("召唤师");
JMenu mTalent = new JMenu("天赋树");
// 菜单项
mHero.add(new JMenuItem("近战-Warriar"));
mHero.add(new JMenuItem("远程-Range"));
mHero.add(new JMenuItem("物理-physical"));
mHero.add(new JMenuItem("坦克-Tank"));
mHero.add(new JMenuItem("法系-Mage"));
mHero.add(new JMenuItem("辅助-Support"));
mHero.add(new JMenuItem("打野-Jungle"));
mHero.add(new JMenuItem("突进-Charge"));
mHero.add(new JMenuItem("男性-Boy"));
mHero.add(new JMenuItem("女性-Girl"));
mb.add(mHero);
mb.add(mItem);
mb.add(mWord);
mb.add(mSummon);
mb.add(mTalent);
f.setJMenuBar(mb);
}
}
表格
package gui;
import java.awt.BorderLayout;
import javax.swing.JFrame;
import javax.swing.JTable;
public class TestGUI {
public static void main(String[] args) {
JFrame f = new JFrame("LoL");
f.setSize(400, 300);
f.setLocation(200, 200);
f.setLayout(new BorderLayout());
// 表格上的title
String[] columnNames = new String[] { "id", "name", "hp", "damage" };
// 表格中的内容,是一个二维数组
String[][] heros = new String[][] { { "1", "盖伦", "616", "100" },
{ "2", "提莫", "512", "102" }, { "3", "奎因", "832", "200" } };
JTable t = new JTable(heros, columnNames);
f.add(t, BorderLayout.CENTER);
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.setVisible(true);
}
}
package gui;
import java.awt.BorderLayout;
import javax.swing.JFrame;
import javax.swing.JScrollPane;
import javax.swing.JTable;
public class TestGUI {
public static void main(String[] args) {
JFrame f = new JFrame("LoL");
f.setSize(400, 300);
f.setLocation(200, 200);
f.setLayout(new BorderLayout());
String[] columnNames = new String[] { "id", "name", "hp", "damage" };
String[][] heros = new String[][] { { "1", "盖伦", "616", "100" },
{ "2", "提莫", "512", "102" }, { "3", "奎因", "832", "200" } };
JTable t = new JTable(heros, columnNames);
// 根据t创建 JScrollPane
JScrollPane sp = new JScrollPane(t);
//或则创建一个空的JScrollPane,再通过setViewportView把table放在JScrollPane中
// JScrollPane sp = new JScrollPane(t);
// sp.setViewportView(t);
// 把sp而非JTable加入到JFrame上,
f.add(sp, BorderLayout.CENTER);
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.setVisible(true);
}
}
package gui;
import java.awt.BorderLayout;
import javax.swing.JFrame;
import javax.swing.JScrollPane;
import javax.swing.JTable;
public class TestGUI {
public static void main(String[] args) {
JFrame f = new JFrame("LoL");
f.setSize(400, 300);
f.setLocation(200, 200);
f.setLayout(new BorderLayout());
String[] columnNames = new String[] { "id", "name", "hp", "damage" };
String[][] heros = new String[][] { { "1", "盖伦", "616", "100" },
{ "2", "提莫", "512", "102" }, { "3", "奎因", "832", "200" } };
JTable t = new JTable(heros, columnNames);
JScrollPane sp = new JScrollPane(t);
// 设置列宽度
t.getColumnModel().getColumn(0).setPreferredWidth(10);
f.add(sp, BorderLayout.CENTER);
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.setVisible(true);
}
}
package gui;
import javax.swing.table.AbstractTableModel;
public class HeroTableModel extends AbstractTableModel {
String[] columnNames = new String[] { "id", "name", "hp", "damage" };
String[][] heros = new String[][] { { "1", "盖伦", "616", "100" },
{ "2", "提莫", "512", "102" }, { "3", "奎因", "832", "200" } };
// 返回一共有多少行
public int getRowCount() {
// TODO Auto-generated method stub
return heros.length;
}
// 返回一共有多少列
public int getColumnCount() {
// TODO Auto-generated method stub
return columnNames.length;
}
// 获取每一列的名称
public String getColumnName(int columnIndex) {
// TODO Auto-generated method stub
return columnNames[columnIndex];
}
// 单元格是否可以修改
public boolean isCellEditable(int rowIndex, int columnIndex) {
return false;
}
// 每一个单元格里的值
public Object getValueAt(int rowIndex, int columnIndex) {
// TODO Auto-generated method stub
return heros[rowIndex][columnIndex];
}
}
package gui;
import java.awt.BorderLayout;
import javax.swing.JFrame;
import javax.swing.JScrollPane;
import javax.swing.JTable;
public class TestGUI {
public static void main(String[] args) {
JFrame f = new JFrame("LoL");
f.setSize(400, 300);
f.setLocation(200, 200);
f.setLayout(new BorderLayout());
//创建一个TableModel
HeroTableModel htm= new HeroTableModel();
//根据 TableModel来创建 Table
JTable t = new JTable(htm);
JScrollPane sp = new JScrollPane(t);
f.add(sp, BorderLayout.CENTER);
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.setVisible(true);
}
}
package gui;
import java.util.List;
import javax.swing.table.AbstractTableModel;
import jdbc.HeroDAO;
import charactor.Hero;
public class HeroTableModel extends AbstractTableModel {
String[] columnNames = new String[] { "id", "name", "hp", "damage" };
// 使用从DAO返回的List作为TableModel的数据
public List<Hero> heros = new HeroDAO().list();
// heros.size返回一共有多少行
public int getRowCount() {
// TODO Auto-generated method stub
return heros.size();
}
public int getColumnCount() {
// TODO Auto-generated method stub
return columnNames.length;
}
public String getColumnName(int columnIndex) {
// TODO Auto-generated method stub
return columnNames[columnIndex];
}
public boolean isCellEditable(int rowIndex, int columnIndex) {
return false;
}
// 先通过heros.get(rowIndex)获取行对应的Hero对象
// 然后根据columnIndex返回对应的属性
public Object getValueAt(int rowIndex, int columnIndex) {
Hero h = heros.get(rowIndex);
if (0 == columnIndex)
return h.id;
if (1 == columnIndex)
return h.name;
if (2 == columnIndex)
return h.hp;
if (3 == columnIndex)
return h.damage;
return null;
}
}
package gui;
import java.awt.BorderLayout;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener;
import charactor.Hero;
public class TestGUI {
public static void main(String[] args) {
JFrame f = new JFrame("LoL");
f.setSize(400, 300);
f.setLocation(200, 200);
f.setLayout(new BorderLayout());
final HeroTableModel htm = new HeroTableModel();
final JTable t = new JTable(htm);
// 准备一个Panel上面放一个Label用于显示哪条被选中了
JPanel p = new JPanel();
final JLabel l = new JLabel("暂时未选中条目");
p.add(l);
JScrollPane sp = new JScrollPane(t);
// 使用selection监听器来监听table的哪个条目被选中
t.getSelectionModel().addListSelectionListener(
new ListSelectionListener() {
// 当选择了某一行的时候触发该事件
public void valueChanged(ListSelectionEvent e) {
// 获取哪一行被选中了
int row = t.getSelectedRow();
// 根据选中的行,到HeroTableModel中获取对应的对象
Hero h = htm.heros.get(row);
// 更新标签内容
l.setText("当前选中的英雄是: " + h.name);
}
});
f.add(p, BorderLayout.NORTH);
f.add(sp, BorderLayout.CENTER);
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.setVisible(true);
}
}
package gui;
import java.awt.BorderLayout;
import java.awt.Dimension;
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.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.JTextField;
import jdbc.HeroDAO;
import charactor.Hero;
public class TestGUI {
public static void main(String[] args) {
JFrame f = new JFrame("LoL");
f.setSize(400, 300);
f.setLocation(200, 200);
f.setLayout(new BorderLayout());
final HeroTableModel htm = new HeroTableModel();
final JTable t = new JTable(htm);
// 增加 一个 panel用于放置名称,血量输入框和增加 按钮
JPanel p = new JPanel();
final JLabel lName = new JLabel("名称");
final JTextField tfName = new JTextField("");
final JLabel lHp = new JLabel("血量");
final JTextField tfHp = new JTextField("");
JButton bAdd = new JButton("增加");
tfName.setPreferredSize(new Dimension(80, 30));
tfHp.setPreferredSize(new Dimension(80, 30));
p.add(lName);
p.add(tfName);
p.add(lHp);
p.add(tfHp);
p.add(bAdd);
// 为增加按钮添加监听
bAdd.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
HeroDAO dao = new HeroDAO();
// 根据输入框数据创建一个Hero对象
Hero h = new Hero();
h.name = tfName.getText();
h.hp = Integer.parseInt(tfHp.getText());
// 通过dao把该对象加入到数据库
dao.add(h);
// 通过dao更新tablemodel中的数据
htm.heros = dao.list();
// 调用JTable的updateUI,刷新界面。
// 刷新界面的时候,会到tablemodel中去取最新的数据
// 就能看到新加进去的数据了
t.updateUI();
}
});
JScrollPane sp = new JScrollPane(t);
f.add(p, BorderLayout.NORTH);
f.add(sp, BorderLayout.CENTER);
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.setVisible(true);
}
}
package gui;
import java.awt.BorderLayout;
import java.awt.Dimension;
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.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.JTextField;
import jdbc.HeroDAO;
import charactor.Hero;
public class TestGUI {
public static void main(String[] args) {
final JFrame f = new JFrame("LoL");
f.setSize(400, 300);
f.setLocation(200, 200);
f.setLayout(new BorderLayout());
final HeroTableModel htm = new HeroTableModel();
final JTable t = new JTable(htm);
JPanel p = new JPanel();
final JLabel lName = new JLabel("名称");
final JTextField tfName = new JTextField("");
final JLabel lHp = new JLabel("血量");
final JTextField tfHp = new JTextField("");
JButton bAdd = new JButton("增加");
tfName.setPreferredSize(new Dimension(80, 30));
tfHp.setPreferredSize(new Dimension(80, 30));
p.add(lName);
p.add(tfName);
p.add(lHp);
p.add(tfHp);
p.add(bAdd);
bAdd.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
HeroDAO dao = new HeroDAO();
Hero h = new Hero();
String name = tfName.getText();
// 通过name长度判断 名称是否为空
if (name.length() == 0) {
// 弹出对话框提示用户
JOptionPane.showMessageDialog(f, "名称不能为空");
// 名称输入框获取焦点
tfName.grabFocus();
return;
}
String hp = tfHp.getText().trim();
try {
// 把hp转换为浮点型,如果出现异常NumberFormatException表示不是浮点型格式
Float.parseFloat(hp);
} catch (NumberFormatException e1) {
JOptionPane.showMessageDialog(f, "血量只能是小数 ");
tfHp.grabFocus();
return;
}
h.name = name;
h.hp = Float.parseFloat(hp);
dao.add(h);
htm.heros = dao.list();
t.updateUI();
}
});
JScrollPane sp = new JScrollPane(t);
f.add(p, BorderLayout.NORTH);
f.add(sp, BorderLayout.CENTER);
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.setVisible(true);
}
}
package gui;
import java.awt.BorderLayout;
import java.awt.Dimension;
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.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.JTextField;
import javax.swing.ListSelectionModel;
import jdbc.HeroDAO;
import charactor.Hero;
public class TestGUI {
public static void main(String[] args) {
final JFrame f = new JFrame("LoL");
f.setSize(400, 300);
f.setLocation(200, 200);
f.setLayout(new BorderLayout());
final HeroTableModel htm = new HeroTableModel();
final JTable t = new JTable(htm);
// 设置选择模式为 只能选中一行
t.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
// 选中第一行 (基本0)
t.getSelectionModel().setSelectionInterval(0, 0);
JPanel p = new JPanel();
final JLabel lName = new JLabel("名称");
final JTextField tfName = new JTextField("");
final JLabel lHp = new JLabel("血量");
final JTextField tfHp = new JTextField("");
JButton bAdd = new JButton("增加");
tfName.setPreferredSize(new Dimension(80, 30));
tfHp.setPreferredSize(new Dimension(80, 30));
p.add(lName);
p.add(tfName);
p.add(lHp);
p.add(tfHp);
p.add(bAdd);
bAdd.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
HeroDAO dao = new HeroDAO();
Hero h = new Hero();
String name = tfName.getText();
if (name.length() == 0) {
JOptionPane.showMessageDialog(f, "名称不能为空");
tfName.grabFocus();
return;
}
String hp = tfHp.getText().trim();
try {
Float.parseFloat(hp);
} catch (NumberFormatException e1) {
JOptionPane.showMessageDialog(f, "血量只能是小数 ");
tfHp.grabFocus();
return;
}
h.name = name;
h.hp = Float.parseFloat(hp);
dao.add(h);
htm.heros = dao.list();
t.updateUI();
// 选中 第一行 ,因为 DAO是按照 ID倒排序查询,所以第一行就是新加入的数据
t.getSelectionModel().setSelectionInterval(0, 0);
}
});
JScrollPane sp = new JScrollPane(t);
f.add(p, BorderLayout.NORTH);
f.add(sp, BorderLayout.CENTER);
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.setVisible(true);
}
}
日期控件
swing没有自带的日期控件,需要第三方的类
package gui;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Date;
import java.util.Locale;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import com.eltima.components.ui.DatePicker;
public class TestGUI {
public static void main(String[] args) {
JFrame f = new JFrame("LoL");
f.setSize(400, 300);
f.setLocation(200, 200);
f.setLayout(null);
final DatePicker datepick;
datepick = getDatePicker();
f.add(datepick);
JButton b = new JButton("获取时间");
b.setBounds(137, 183, 100, 30);
f.add(b);
b.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
JOptionPane.showMessageDialog(f, "获取控件中的日期:" + datepick.getValue());
System.out.println(datepick.getValue());
}
});
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.setVisible(true);
}
private static DatePicker getDatePicker() {
final DatePicker datepick;
// 格式
String DefaultFormat = "yyyy-MM-dd HH:mm:ss";
// 当前时间
Date date = new Date();
// 字体
Font font = new Font("Times New Roman", Font.BOLD, 14);
Dimension dimension = new Dimension(177, 24);
int[] hilightDays = { 1, 3, 5, 7 };
int[] disabledDays = { 4, 6, 5, 9 };
datepick = new DatePicker(date, DefaultFormat, font, dimension);
datepick.setLocation(137, 83);
datepick.setBounds(137, 83, 177, 24);
// 设置一个月份中需要高亮显示的日子
datepick.setHightlightdays(hilightDays, Color.red);
// 设置一个月份中不需要的日子,呈灰色显示
datepick.setDisableddays(disabledDays);
// 设置国家
datepick.setLocale(Locale.CHINA);
// 设置时钟面板可见
datepick.setTimePanleVisible(true);
return datepick;
}
}
package gui;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Date;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import org.jdesktop.swingx.JXDatePicker;
public class TestGUI {
public static void main(String[] args) {
JFrame f = new JFrame("LoL");
f.setSize(400, 300);
f.setLocation(200, 200);
f.setLayout(null);
Date date = new Date();
final JXDatePicker datepick = new JXDatePicker();
// 设置 date日期
datepick.setDate(date);
datepick.setBounds(137, 83, 177, 24);
f.add(datepick);
JButton b = new JButton("获取时间");
b.setBounds(137, 183, 100, 30);
f.add(b);
b.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
// 获取 日期
Date d = datepick.getDate();
JOptionPane.showMessageDialog(f, "获取控件中的日期 :" + d);
}
});
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.setVisible(true);
}
}
表格综合练习
swing中的线程
package gui;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.SwingUtilities;
public class TestGUI {
public static void main(String[] args) {
new TestFrame().setVisible(true);
// SwingUtilities.invokeLater(new Runnable() {
// public void run() {
// new TestFrame().setVisible(true);
// }
// });
}
static class TestFrame extends JFrame {
public TestFrame() {
setTitle("LoL");
setSize(400, 300);
setLocation(200, 200);
setLayout(null);
JButton b = new JButton("一键秒对方基地挂");
b.setBounds(50, 50, 280, 30);
add(b);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setVisible(true);
System.out.println("当前线程是否是 事件调度线程: " + SwingUtilities.isEventDispatchThread());
}
}
}
package gui;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.SwingUtilities;
import javax.swing.SwingWorker;
public class TestGUI {
public static void main(String[] args) {
JFrame f = new JFrame("LoL");
f.setSize(400, 300);
f.setLocation(580, 200);
f.setLayout(null);
final JLabel l = new JLabel();
ImageIcon i = new ImageIcon("e:/project/j2se/shana.png");
l.setIcon(i);
l.setBounds(50, 50, i.getIconWidth(), i.getIconHeight());
JButton b = new JButton("隐藏图片");
b.setBounds(150, 200, 100, 30);
b.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
l.setVisible(false);
System.out.println("当前使用的是事件调度线程:" + SwingUtilities.isEventDispatchThread());
}
});
f.add(l);
f.add(b);
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.setVisible(true);
}
}
package gui;
import java.awt.FlowLayout;
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.SwingWorker;
public class TestGUI {
public static void main(String[] args) {
JFrame f = new JFrame("LoL");
f.setSize(300, 300);
f.setLocation(200, 200);
f.setLayout(new FlowLayout());
JButton b1 = new JButton("在事件调度线程中执行长耗时任务");
JButton b2 = new JButton("使用SwingWorker执行长耗时任务");
JLabel l = new JLabel("任务执行结果");
f.add(b1);
f.add(b2);
f.add(l);
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
b1.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
l.setText("开始执行完毕");
try {
Thread.sleep(5000);
} catch (InterruptedException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
l.setText("任务执行完毕");
}
});
b2.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
SwingWorker<Void, Void> worker = new SwingWorker<Void, Void>() {
@Override
protected Void doInBackground() throws Exception {
System.out.println("执行这个SwingWorder的线程是:" + Thread.currentThread().getName());
l.setText("开始执行完毕");
try {
Thread.sleep(5000);
} catch (InterruptedException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
l.setText("任务执行完毕");
return null;
}
};
worker.execute();
}
});
f.setVisible(true);
}
}
皮肤
Java提供了非常便捷的方式切换界面风格
package gui;
import javax.swing.JButton;
import javax.swing.JFrame;
public class TestGUI {
public static void main(String[] args) {
//设置皮肤
setLookAndFeel();
JFrame f = new JFrame("LoL");
f.setSize(400, 300);
f.setLocation(200, 200);
f.setLayout(null);
JButton b = new JButton("一键秒对方基地挂");
b.setBounds(50, 50, 280, 30);
f.add(b);
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.setVisible(true);
}
private static void setLookAndFeel() {
try {
javax.swing.UIManager.setLookAndFeel("com.birosoft.liquid.LiquidLookAndFeel");
// javax.swing.UIManager.setLookAndFeel("com.jtattoo.plaf.smart.SmartLookAndFeel");
// javax.swing.UIManager.setLookAndFeel("com.jtattoo.plaf.mcwin.McWinLookAndFeel");
// javax.swing.UIManager.setLookAndFeel("com.jtattoo.plaf.luna.LunaLookAndFeel");
// javax.swing.UIManager.setLookAndFeel("com.jtattoo.plaf.aluminium.AluminiumLookAndFeel");
// javax.swing.UIManager.setLookAndFeel("com.jtattoo.plaf.bernstein.BernsteinLookAndFeel");
// javax.swing.UIManager.setLookAndFeel("com.jtattoo.plaf.hifi.HiFiLookAndFeel");
// javax.swing.UIManager.setLookAndFeel("com.jtattoo.plaf.mint.MintLookAndFeel");
// javax.swing.UIManager.setLookAndFeel("com.jtattoo.plaf.aero.AeroLookAndFeel");
// javax.swing.UIManager.setLookAndFeel("com.jtattoo.plaf.fast.FastLookAndFeel");
// javax.swing.UIManager.setLookAndFeel("com.jtattoo.plaf.acryl.AcrylLookAndFeel");
// javax.swing.UIManager.setLookAndFeel("com.jtattoo.plaf.noire.NoireLookAndFeel");
} catch (Exception e) {
e.printStackTrace();
// handle exception
}
}
}