package test.object;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JList;
import javax.swing.JOptionPane;
import javax.swing.JTabbedPane;
import javax.swing.SwingConstants;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;
public class TextMain {
public void init() {
JFrame jf = new JFrame("test JTabbedPan");
// 参数:生命在左边,标签页策略
JTabbedPane jTabbedPane = new JTabbedPane(SwingConstants.LEFT, JTabbedPane.SCROLL_TAB_LAYOUT);
// 添加标签
jTabbedPane.addTab("用户管理", new ImageIcon("D:\\Image\\1.png"),
new JList<String>(new String[] { "用户已", "用户二", "用户三" }), "1111111");
jTabbedPane.addTab("商品管理", new JList<String>(new String[] { "商品已", "商品二", "商品三" }));
jTabbedPane.addTab("订单管理", new ImageIcon("D:\\Image\\3.png"),
new JList<String>(new String[] { "订单已", "订单二", "订单三" }), "1111111");
// 完成设置
jTabbedPane.setEnabledAt(0, false);
// 默认选中
jTabbedPane.setSelectedIndex(2);
// 监听标签面板选中情况
jTabbedPane.addChangeListener(new ChangeListener() {
@Override
public void stateChanged(ChangeEvent e) {
// TODO Auto-generated method stub
int selectedIndex = jTabbedPane.getSelectedIndex();
JOptionPane.showMessageDialog(jf, "选中了第" + (selectedIndex + 1) + "个标签");
}
});
jf.add(jTabbedPane);
jf.pack();
// 设置窗口大小及位置
jf.setBounds(300, 300, 500, 500);
// 固定窗口大小
jf.setResizable(false);
jf.setVisible(true);
jf.addWindowListener(new WindowAdapter() {
@Override
public void windowClosing(WindowEvent e) {
System.exit(0);
}
});
}
public static void main(String[] age) {
new TextMain().init();
}
}
image.png
网友评论