【Java-GUI】08 Swing02 边框和选择器
边框案例:
package cn.dzz.swing; import javax.swing.*; import javax.swing.border.*; import java.awt.*; public class Demo02 { JFrame jFrame = new JFrame("边框测试"); public void init() { // 修改为栅格布局 jFrame.setLayout(new GridLayout(2,4)); // 向单元格填充组件,设置边框和内容 // 1斜角边框 Border bevelBorder = BorderFactory.createBevelBorder( BevelBorder.RAISED, Color.RED, Color.GREEN, Color.BLUE, Color.GRAY ); JPanel bevelBorderJPanel = getJPanelWithBorder(bevelBorder, "BevelBorder"); // 2线条边框 Border lineBorder = BorderFactory.createLineBorder( Color.ORANGE, 10 ); JPanel lineBorderJPanel = getJPanelWithBorder(lineBorder, "LineBorder"); // 3空边框 Border emptyBorder = BorderFactory.createEmptyBorder(10, 5, 20, 10); JPanel emptyBorderJPanel = getJPanelWithBorder(emptyBorder, "EmptyBorder"); // 4蚀刻边框 Border etchedBorder = BorderFactory.createEtchedBorder( EtchedBorder.RAISED, Color.RED, Color.GREEN ); JPanel etchedBorderJPanel = getJPanelWithBorder(etchedBorder, "EtchedBorder"); // 5标题边框 TitledBorder titledBorder = new TitledBorder(new LineBorder( Color.ORANGE, 10 ), "测试标题", TitledBorder.LEFT, TitledBorder.BOTTOM, new Font("StSong", Font.BOLD, 18), Color.BLUE ); JPanel titleBorderJPanel = getJPanelWithBorder(titledBorder, "TitleBorder"); // 6哑光边框 MatteBorder matteBorder = new MatteBorder(10, 5, 20, 10, Color.GREEN); JPanel matteBorderJPanel = getJPanelWithBorder(matteBorder, "MatteBorder"); // 7复合边框 // CompoundBorder compoundBorder = new CompoundBorder(titledBorder, new LineBorder(Color.RED, 10)); CompoundBorder compoundBorder = new CompoundBorder(new LineBorder(Color.RED, 10), titledBorder); JPanel compoundBorderJPanel = getJPanelWithBorder(compoundBorder, "CompoundBorder"); // 组装 jFrame.add(bevelBorderJPanel); jFrame.add(lineBorderJPanel); jFrame.add(emptyBorderJPanel); jFrame.add(etchedBorderJPanel); jFrame.add(titleBorderJPanel); jFrame.add(matteBorderJPanel); jFrame.add(compoundBorderJPanel); // 设置窗口最佳大小 和 可见 可关闭 jFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); jFrame.pack(); jFrame.setVisible(true); } // 要设置8个面板对象,就要写8次,所以进行了一个封装处理 JPanel getJPanelWithBorder(Border border,String content) { JPanel jPanel = new JPanel(); jPanel.add(new JLabel(content)); jPanel.setBorder(border); return jPanel; } public static void main(String[] args) { new Demo02().init(); } }
边框设置的参数种类很多
工具条案例JToolBar:
package cn.dzz.swing; import javax.swing.*; import java.awt.*; import java.awt.event.ActionEvent; public class Demo03 { JFrame jFrame = new JFrame("工具条演示案例"); JTextArea jTextArea = new JTextArea(6, 35); JToolBar jToolBar = new JToolBar("PlayerToolBar", SwingConstants.HORIZONTAL); // 3个action对象 Action preAct = new AbstractAction("👈 上一首") { @Override public void actionPerformed(ActionEvent e) { jTextArea.append("上一曲\n"); } }; Action pauseAct = new AbstractAction("⏸ 暂停") { @Override public void actionPerformed(ActionEvent e) { jTextArea.append("暂停播放\n"); } }; Action nextAct = new AbstractAction("👉 下一首") { @Override public void actionPerformed(ActionEvent e) { jTextArea.append("下一曲\n"); } }; public void init() { // 直接组装 jToolBar.add(preAct); jToolBar.addSeparator(); jToolBar.add(pauseAct); jToolBar.addSeparator(); jToolBar.add(nextAct); jFrame.add(jToolBar,BorderLayout.NORTH); jFrame.add(jTextArea,BorderLayout.CENTER); jFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); jFrame.pack(); jFrame.setVisible(true); } public static void main(String[] args) { new Demo03().init(); } }
颜色选择和文件选择:
ColorChooser & FileChooser
package cn.dzz.swing; import javax.swing.*; import java.awt.*; import java.awt.event.ActionEvent; public class Demo04 { JFrame jFrame = new JFrame("颜色选择器"); JTextArea jTextArea = new JTextArea("我爱Java",6,30); JButton changeBgColor = new JButton( new AbstractAction("更改编辑区背景色") { @Override public void actionPerformed(ActionEvent e) { Color color = JColorChooser.showDialog( jFrame, // 父级组件 "选择颜色", // 对话框标题 Color.WHITE // 初始颜色 ); jTextArea.setBackground(color); } }); public void init(){ jFrame.add(jTextArea, BorderLayout.CENTER); jFrame.add(changeBgColor, BorderLayout.SOUTH); jFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); jFrame.pack(); jFrame.setVisible(true); } public static void main(String[] args) { new Demo04().init(); } }
效果显示
、
文件选择器:
package cn.dzz.swing; 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 Demo05 { JFrame jFrame = new JFrame("文件选择器"); JMenuBar jMenuBar = new JMenuBar(); JMenu jMenu = new JMenu("文件"); BufferedImage buffImage; // 画布渲染 class MyCanvas extends JPanel { @Override public void paint(Graphics graphics) { graphics.drawImage(buffImage, 0, 0, null); } } MyCanvas myCanvas = new MyCanvas(); JMenuItem openMenuItem = new JMenuItem(new AbstractAction("选择文件") { @Override public void actionPerformed(ActionEvent e) { JFileChooser jFileChooser = new JFileChooser("."); jFileChooser.showOpenDialog(jFrame); File selectedFile = jFileChooser.getSelectedFile(); try { buffImage = ImageIO.read(selectedFile); myCanvas.repaint(); } catch (IOException ioException) { ioException.printStackTrace(); } } }); JMenuItem saveMenuItem = new JMenuItem(new AbstractAction("另存为") { @Override public void actionPerformed(ActionEvent e) { JFileChooser jFileChooser = new JFileChooser("."); jFileChooser.showSaveDialog(jFrame); File selectedFile = jFileChooser.getSelectedFile(); try { ImageIO.write(buffImage, "jpg", selectedFile ); } catch (IOException ioException) { ioException.printStackTrace(); } } }); void init() { // 按钮菜单组装 jMenu.add(openMenuItem); jMenu.add(saveMenuItem); jMenuBar.add(jMenu); // 窗体组装 jFrame.setJMenuBar(jMenuBar); myCanvas.setPreferredSize(new Dimension(740, 500)); jFrame.add(myCanvas); jFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); jFrame.pack(); jFrame.setVisible(true); } public static void main(String[] args) { new Demo05().init(); } }
效果:
存在的一些BUG:如果图片不足画布大小,会将外层的图形写入其中,超过画布大小的,则不会缩放渲染
保存功能不可以自己获取文件后缀