对话框
package Dialog;
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
public class Dome {
JFrame f =new JFrame();
//设置文本域
JTextArea jta =new JTextArea();
JButton jButton =new JButton(new AbstractAction("弹出消息对话框") {
public void actionPerformed(ActionEvent e) {
//弹出一个消息对话框,并且显示 文本域中输入的内容
String text = jta.getText();
// JOptionPane.showMessageDialog(f,text,"消息对话框",JOptionPane.ERROR_MESSAGE);
// JOptionPane.showMessageDialog(f,text,"消息对话框",JOptionPane.INFORMATION_MESSAGE);
// JOptionPane.showMessageDialog(f,text,"消息对话框",JOptionPane.PLAIN_MESSAGE,new ImageIcon("src/main/java/img/list/弄玉.gif"));
int result = JOptionPane.showConfirmDialog(f, text, "确认对话框", JOptionPane.YES_NO_CANCEL_OPTION);
if (result== JOptionPane.YES_OPTION){
jta.append("用户点击了 是 按钮\n");
}
if (result==JOptionPane.NO_OPTION){
jta.append("用户点击了 否 按钮\n");
}
if (result==JOptionPane.OK_OPTION){
jta.append("用户点击了 确认 按钮\n");
}
if (result==JOptionPane.CANCEL_OPTION){
jta.append("用户点击了 取消 按钮\n");
}
}
});
public void init(){
//组装视图
jta.setPreferredSize(new Dimension(400,400));
f.add(jta);
f.add(jButton, BorderLayout.SOUTH);
f.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
f.pack();
f.setVisible(true);
}
public static void main(String[] args) {
new Dome().init();
}
}
package Dialog;
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
public class Dome1 {
JFrame f =new JFrame();
//设置文本域
JTextArea jta =new JTextArea();
JButton jButton =new JButton(new AbstractAction("弹出消息对话框") {
public void actionPerformed(ActionEvent e) {
//弹出一个消息对话框,并且显示 文本域中输入的内容
String text = jta.getText();
String s = JOptionPane.showInputDialog(f, "请填写您的银行账户","输入对话框",JOptionPane.INFORMATION_MESSAGE);
jta.append(s+"\n");
}
});
public void init(){
//组装视图
jta.setPreferredSize(new Dimension(400,400));
f.add(jta);
f.add(jButton, BorderLayout.SOUTH);
f.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
f.pack();
f.setVisible(true);
}
public static void main(String[] args) {
new Dome1().init();
}
}
package Dialog;
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
public class Dome2 {
JFrame f = new JFrame();
//设置文本域
JTextArea jta = new JTextArea();
JButton jButton = new JButton(new AbstractAction("弹出消息对话框") {
public void actionPerformed(ActionEvent e) {
//弹出一个消息对话框,并且显示 文本域中输入的内容
String text = jta.getText();
//选项对话框
int i = JOptionPane.showOptionDialog(f, "请选择尿不湿号码", "选项对话框", JOptionPane.DEFAULT_OPTION,
JOptionPane.INFORMATION_MESSAGE, null, new String[]{"大号", "中号", "小号"}, "中号"
);
switch (i) {
case 0:
jta.append("用户选择了大号");
break;
case 1:
jta.append("用户选择了中号");
break;
case 2:
jta.append("用户选择了小号");
break;
}
}
});
public void init() {
//组装视图
jta.setPreferredSize(new Dimension(400, 400));
f.add(jta);
f.add(jButton, BorderLayout.SOUTH);
f.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
f.pack();
f.setVisible(true);
}
public static void main(String[] args) {
new Dome2().init();
}
}
颜色选择器
package JColorChooser;
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
public class Dome1 {
JFrame f =new JFrame("测试窗口");
JTextArea jta =new JTextArea("我爱中华",6,30);
JButton jButton =new JButton(new AbstractAction("改变文本框背景颜色") {
public void actionPerformed(ActionEvent e) {
//弹出一个颜色选择器
Color color = JColorChooser.showDialog(f, "颜色选择器", Color.red);
//修改文本框背景
jta.setBackground(color);
}
});
public void init(){
f.add(jta);
f.add(jButton,BorderLayout.SOUTH);
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.pack();
f.setVisible(true);
}
public static void main(String[] args) {
new Dome1().init();
}
}
package JColorChooser;
import javax.imageio.ImageIO;
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
public class JFileChooserDemo {
//创建窗口对象
JFrame jf = new JFrame("测试JFileChooser");
//创建打开文件对话框
JFileChooser chooser = new JFileChooser(".");
//创建菜单条
JMenuBar jmb = new JMenuBar();
//创建菜单
JMenu jMenu = new JMenu("文件");
//创建菜单项
JMenuItem open = new JMenuItem(new AbstractAction("打开"){
public void actionPerformed(ActionEvent e) {
chooser.showOpenDialog(jf);
File imageFile = chooser.getSelectedFile();
try {
image = ImageIO.read(imageFile);
drawArea.repaint();
} catch (IOException e1) {
e1.printStackTrace();
}
}
});
JMenuItem save = new JMenuItem(new AbstractAction("另存为"){
public void actionPerformed(ActionEvent e) {
chooser.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
chooser.showSaveDialog(jf);
File dir = chooser.getSelectedFile();
try {
ImageIO.write(image,"jpeg",new File(dir,"a.jpg"));
} catch (Exception e1) {
e1.printStackTrace();
}
}
});
//用来记录用户选择的图片
BufferedImage image;
//显示图片
class MyCanvas extends JPanel{
@Override
public void paint(Graphics g) {
if (image!=null){
g.drawImage(image,0,0,null);
}
}
}
JPanel drawArea = new MyCanvas();
public void init(){
//设置图片显示区域大小
drawArea.setPreferredSize(new Dimension(500,300));
jf.add(drawArea);
//组装并设置菜单条
jMenu.add(open);
jMenu.add(save);
jmb.add(jMenu);
jf.setJMenuBar(jmb);
//显示jf
jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
jf.pack();
jf.setVisible(true);
}
public static void main(String[] args) {
new JFileChooserDemo().init();
}
}
文件选择器
package JFileChooser;
import javax.imageio.ImageIO;
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
public class Dome1 {
JFrame f =new JFrame("测试窗口");
JFileChooser jFileChooser =new JFileChooser("F:\\java\\gui\\12-2\\src\\main\\java\\img\\component\\ly.jpg");
//菜单
JMenuBar menuBar =new JMenuBar();
JMenu file =new JMenu("文件");
//创建菜单项
JMenuItem open = new JMenuItem(new AbstractAction("打开"){
public void actionPerformed(ActionEvent e) {
jFileChooser.showOpenDialog(f);
File imageFile = jFileChooser.getSelectedFile();
try {
image = ImageIO.read(imageFile);
myCanvas.repaint();
} catch (IOException e1) {
e1.printStackTrace();
}
}
});
JMenuItem save = new JMenuItem(new AbstractAction("另存为"){
public void actionPerformed(ActionEvent e) {
jFileChooser.showSaveDialog(f);
File file = jFileChooser.getSelectedFile();
try {
ImageIO.write(image,"jpeg",file);
} catch (IOException ex) {
ex.printStackTrace();
}
}
});
//swing提供的组件,都使用了图像缓冲区技术
BufferedImage image;
class MyCanvas extends JPanel{
@Override
public void paint(Graphics g) {
if (image!=null){
g.drawImage(image,0,0,null);
}
}
}
MyCanvas myCanvas = new MyCanvas();
public void init(){
myCanvas.setPreferredSize(new Dimension(740,500));
f.add(myCanvas);
file.add(open);
file.add(save);
menuBar.add(file);
f.setJMenuBar(menuBar);
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.pack();
f.setVisible(true);
}
public static void main(String[] args) {
new Dome1().init();
}
}
tablepanel
package JTabledPane;
import javax.swing.*;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;
import java.awt.*;
public class Dome {
public int width = Toolkit.getDefaultToolkit().getScreenSize().width;
public int height = Toolkit.getDefaultToolkit().getScreenSize().height;
// 定义窗体的宽高
public int windowsWedth = 600;
public int windowsHeight = 600;
JFrame f =new JFrame();
JTabbedPane jTabbedPane =new JTabbedPane(SwingConstants.LEFT,JTabbedPane.SCROLL_TAB_LAYOUT);
public void init(){
jTabbedPane.addTab("用户管理",null,new JList<String>(new String[]{"用户1","用户2","用户3"}));
jTabbedPane.addTab("商品管理",null,new JList<String>(new String[]{"商品1","商品2","商品3"}));
jTabbedPane.addTab("订单管理",null,new JList<String>(new String[]{"订单1","订单2","订单3"}));
//完成设置
jTabbedPane.setEnabledAt(0,false);
jTabbedPane.setSelectedIndex(1);
//监听当前标签面板
jTabbedPane.addChangeListener(new ChangeListener() {
@Override
public void stateChanged(ChangeEvent e) {
int index = jTabbedPane.getSelectedIndex();
JOptionPane.showMessageDialog(f,"选择了第"+index+"个标签");
}
});
// jTabbedPane.setPreferredSize(new Dimension(400,400));
f.add(jTabbedPane);
//设置窗口的大小和位置
// f.setLocationRelativeTo(null);
// f.setPreferredSize(new Dimension(400,400));
f.setLocation((width - windowsWedth) / 2,(height - windowsHeight) / 2);
f.setPreferredSize(new Dimension(400,400));
//固定窗口大小
f.setResizable(false);
f.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
f.pack();
f.setVisible(true);
}
public static void main(String[] args) {
new Dome().init();
}
}
进度条
package progress;
import javax.swing.*;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
public class Dome1 {
JFrame f =new JFrame();
JCheckBox checkBox1=new JCheckBox("不确定进度");
JCheckBox checkBox2=new JCheckBox("不绘制边框");
//创建进度条
JProgressBar bar =new JProgressBar(JProgressBar.HORIZONTAL,0,100);
public void init(){
checkBox1.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
//获取是否选中
boolean selected = checkBox1.isSelected();
//设置不确定进度
bar.setIndeterminate(selected);
bar.setStringPainted(!selected);
}
});
checkBox2.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
//获取是否选中
boolean selected = checkBox1.isSelected();
bar.setBorderPainted(!selected);
}
});
Box vBox =Box.createVerticalBox();
vBox.add(checkBox1);
vBox.add(checkBox2);
//设置进度条属性
bar.setBorderPainted(true);
bar.setStringPainted(true);
//设置水平布局
f.setLayout(new FlowLayout());
f.add(vBox);
f.add(bar);
f.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
f.pack();
f.setVisible(true);
//通过循环模拟修改进度条
// for (int i = 0; i <= 100; i++) {
// bar.setValue(i);
//
// try {
// Thread.sleep(200);
// } catch (InterruptedException e) {
// e.printStackTrace();
// }
// }
//开启子线程
SimulaterActivity activity = new SimulaterActivity(bar.getMaximum());
new Thread(activity).start();
//设置定时任务
Timer timer =new Timer(200, new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
//读取线程任务对象的当前完成量
int current = activity.getCurrent();
bar.setValue(current);
}
});
timer.start();
//监听进度条的任务变化
bar.addChangeListener(new ChangeListener() {
@Override
public void stateChanged(ChangeEvent e) {
int value = bar.getValue();
if (value==activity.getAmount()){
timer.stop();
}
}
});
}
//子线程改进
private class SimulaterActivity implements Runnable{
//记录任务总量
private int amount;
//记录当前完成量
private volatile int current;
//带参构造
public SimulaterActivity(int amount) {
this.amount = amount;
}
public int getAmount() {
return amount;
}
public void setAmount(int amount) {
this.amount = amount;
}
public int getCurrent() {
return current;
}
public void setCurrent(int current) {
this.current = current;
}
@Override
public void run() {
//子线程的任务
while (current<amount){
//模拟耗时操作
try {
Thread.currentThread().sleep(50);
} catch (InterruptedException e) {
e.printStackTrace();
}
current++;
}
}
}
public static void main(String[] args) {
new Dome1().init();
}
}
package progress;
import javax.swing.*;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
public class Dome2 {
JFrame f =new JFrame();
JCheckBox checkBox1=new JCheckBox("不确定进度");
JCheckBox checkBox2=new JCheckBox("不绘制边框");
//创建进度条
JProgressBar bar =new JProgressBar(JProgressBar.HORIZONTAL,0,100);
//获取内置的进度条模型对象
BoundedRangeModel model =bar.getModel();
public void init(){
checkBox1.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
//获取是否选中
boolean selected = checkBox1.isSelected();
//设置不确定进度
bar.setIndeterminate(selected);
bar.setStringPainted(!selected);
}
});
checkBox2.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
//获取是否选中
boolean selected = checkBox1.isSelected();
bar.setBorderPainted(!selected);
}
});
Box vBox =Box.createVerticalBox();
vBox.add(checkBox1);
vBox.add(checkBox2);
//设置进度条属性
bar.setBorderPainted(true);
bar.setStringPainted(true);
//设置水平布局
f.setLayout(new FlowLayout());
f.add(vBox);
f.add(bar);
f.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
f.pack();
f.setVisible(true);
//通过循环模拟修改进度条
// for (int i = 0; i <= 100; i++) {
// bar.setValue(i);
//
// try {
// Thread.sleep(200);
// } catch (InterruptedException e) {
// e.printStackTrace();
// }
// }
//开启子线程
SimulaterActivity activity = new SimulaterActivity(bar.getMaximum());
new Thread(activity).start();
//设置定时任务
Timer timer =new Timer(200, new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
//读取线程任务对象的当前完成量
int current = activity.getCurrent();
model.setValue(current);
}
});
timer.start();
//监听进度条的任务变化
model.addChangeListener(new ChangeListener() {
@Override
public void stateChanged(ChangeEvent e) {
int value = model.getValue();
if (value==activity.getAmount()){
timer.stop();
}
}
});
}
//子线程改进
private class SimulaterActivity implements Runnable{
//记录任务总量
private int amount;
//记录当前完成量
private volatile int current;
//带参构造
public SimulaterActivity(int amount) {
this.amount = amount;
}
public int getAmount() {
return amount;
}
public void setAmount(int amount) {
this.amount = amount;
}
public int getCurrent() {
return current;
}
public void setCurrent(int current) {
this.current = current;
}
@Override
public void run() {
//子线程的任务
while (current<amount){
//模拟耗时操作
try {
Thread.currentThread().sleep(50);
} catch (InterruptedException e) {
e.printStackTrace();
}
current++;
}
}
}
public static void main(String[] args) {
new Dome2().init();
}
}
package progress;
import javax.swing.*;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
public class Dome3 {
Timer timer;
public void init(){
//创建进度对话框
ProgressMonitor monitor =new ProgressMonitor(null,"等待任务完成","已完成",0,100);
SimulaterActivity simulaterActivity =new SimulaterActivity(100);
new Thread(simulaterActivity).start();
//设置定时任务
timer =new Timer(200, new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
//读取线程任务对象的当前完成量
int current = simulaterActivity.getCurrent();
monitor.setProgress(current);
//判断用户是否点击了提交按钮,停止定时任务,关闭对话框
if (monitor.isCanceled()){
timer.stop();
monitor.close();
System.exit(0);
}
if (monitor.getMaximum()<=current){
timer.stop();
monitor.close();
System.exit(0);
}
}
});
timer.start();
//监听进度条的任务变化
// f.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
// f.pack();
// f.setVisible(true);
//通过循环模拟修改进度条
// for (int i = 0; i <= 100; i++) {
// bar.setValue(i);
//
// try {
// Thread.sleep(200);
// } catch (InterruptedException e) {
// e.printStackTrace();
// }
// }
}
//子线程改进
private class SimulaterActivity implements Runnable{
//记录任务总量
private int amount;
//记录当前完成量
private volatile int current;
//带参构造
public SimulaterActivity(int amount) {
this.amount = amount;
}
public int getAmount() {
return amount;
}
public void setAmount(int amount) {
this.amount = amount;
}
public int getCurrent() {
return current;
}
public void setCurrent(int current) {
this.current = current;
}
@Override
public void run() {
//子线程的任务
while (current<amount){
//模拟耗时操作
try {
Thread.currentThread().sleep(50);
} catch (InterruptedException e) {
e.printStackTrace();
}
current++;
}
}
}
public static void main(String[] args) {
new Dome3().init();
}
}
分割线
package split;
import bean.Book;
import javax.swing.*;
import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener;
import java.awt.*;
public class Dome {
Book[] books=new Book[]{
new Book("java自学宝典",new ImageIcon("src/main/java/img/container/java.png"),"书1"),
new Book("轻量级java应用实战",new ImageIcon("src/main/java/img/container/ee.png"),"书2"),
new Book("android基础教程",new ImageIcon("src/main/java/img/container/android.png"),"书3"),
};
JFrame f =new JFrame();
//list列表
JList<Book> list =new JList<Book>(books);
JLabel bookCover =new JLabel();
JTextArea bookDesc =new JTextArea();
public void init(){
//组装视图
//设置组件大小
list.setPreferredSize(new Dimension(150,400));
bookCover.setPreferredSize(new Dimension(220,270));
bookDesc.setPreferredSize(new Dimension(220,130));
//为jlist设置条目选中监听器
list.addListSelectionListener(new ListSelectionListener() {
@Override
public void valueChanged(ListSelectionEvent e) {
//获取当前是哪个条目
Book book =list.getSelectedValue();
bookCover.setIcon(book.getIcon());
bookDesc.setText(book.getDesc());
}
});
//组装左边区域
JSplitPane left =new JSplitPane(JSplitPane.VERTICAL_SPLIT,bookCover,new JScrollPane(bookDesc));
left.setOneTouchExpandable(true);
JSplitPane hole =new JSplitPane(JSplitPane.HORIZONTAL_SPLIT,left,list);
//支持连续布局
hole.setContinuousLayout(true);
hole.setDividerSize(10);
f.add(hole);
f.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
f.pack();
f.setVisible(true);
}
public static void main(String[] args) {
new Dome().init();
}
}