【Java-GUI】10 Swing04
面板容器:
package cn.dzz.swing; import javax.swing.*; import javax.swing.event.ChangeEvent; import javax.swing.event.ChangeListener; public class Demo07 { JFrame jFrame = new JFrame("案例"); JTabbedPane jTabbedPane = new JTabbedPane(SwingConstants.LEFT, JTabbedPane.SCROLL_TAB_LAYOUT); void init() { // 组装视图 // 添加标签 jTabbedPane.addTab( "用户管理", new ImageIcon("img/icon01.png"), new JList<String>(new String[]{ "用户一", "用户二", "用户三", } ) ); jTabbedPane.addTab( "商品管理", new JList<String>(new String[]{ "商品一", "商品二", "商品三", } ) ); jTabbedPane.addTab( "订单管理", new ImageIcon("img/icon01.png"), new JList<String>(new String[]{ "订单一", "订单二", "订单三", } ) ); jTabbedPane.setEnabledAt(0, false); jTabbedPane.setSelectedIndex(1); jTabbedPane.addChangeListener(new ChangeListener() { @Override public void stateChanged(ChangeEvent e) { int selectedIndex = jTabbedPane.getSelectedIndex(); JOptionPane.showMessageDialog( jFrame, "当前选中第" + (selectedIndex + 1) + "标签" ); } }); jFrame.add(jTabbedPane); jFrame.setResizable(false); jFrame.setBounds(400, 400, 400, 400); jFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); // jFrame.pack(); jFrame.setVisible(true); } public static void main(String[] args) { new Demo07().init(); } }
效果:
进度条:
package cn.dzz.swing; import javax.swing.*; import java.awt.*; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; public class Demo01 { JFrame jFrame = new JFrame("ProgressBar"); // 水平进度条 JProgressBar jProgressBar = new JProgressBar( JProgressBar.HORIZONTAL ); // 按钮 JCheckBox indeterminate = new JCheckBox("不确定进度"); JCheckBox noBorder = new JCheckBox("无绘制边框"); public void init() { // 添加 indeterminate.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { // 获取状态是否为选中 boolean selected = indeterminate.isSelected(); jProgressBar.setIndeterminate(selected); jProgressBar.setStringPainted(!selected); } }); noBorder.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { boolean selected = noBorder.isSelected(); noBorder.setBorderPainted(!selected); } }); Box verticalBox = Box.createVerticalBox(); verticalBox.add(indeterminate); verticalBox.add(noBorder); jProgressBar.setStringPainted(true); jProgressBar.setBorderPainted(true); // 改变窗体东西南北中的布局,设置流布局方式 jFrame.setLayout(new FlowLayout()); jFrame.add(verticalBox); jFrame.add(jProgressBar); jFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); jFrame.pack(); jFrame.setVisible(true); // 模拟进度加载 for (int i = 0; i <= 100; i++) { jProgressBar.setValue(i); try { Thread.sleep(1000); } catch (InterruptedException e) { e.printStackTrace(); } } } public static void main(String[] args) { new Demo01().init(); } }
效果: