📄 tabbedpaneexam.java
字号:
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class tabbedPaneExam extends JFrame implements ActionListener {
private JTabbedPane jtab;
private JButton topjbutton = new JButton("顶部");
private JButton bottomjbutton = new JButton("底部");
private JButton leftjbutton = new JButton("左边");
private JButton rightjbutton = new JButton("右边");
private JButton addjbutton = new JButton("添加选项卡");
private JButton removejbutton = new JButton("删除选项卡");
public tabbedPaneExam() { //构造方法
super("TabbedPane演示");
JPanel buttonPanel = new JPanel();
buttonPanel.setLayout(new GridLayout(1, 6));
topjbutton.addActionListener(this);
bottomjbutton.addActionListener(this);
leftjbutton.addActionListener(this);
rightjbutton.addActionListener(this);
addjbutton.addActionListener(this);
removejbutton.addActionListener(this);
buttonPanel.add(topjbutton);
buttonPanel.add(bottomjbutton);
buttonPanel.add(leftjbutton);
buttonPanel.add(rightjbutton);
buttonPanel.add(addjbutton);
buttonPanel.add(removejbutton);
//把选项卡布局管理器和面板加入到内容窗格中
jtab = new JTabbedPane(SwingConstants.TOP);
getContentPane().add("South", buttonPanel);
getContentPane().add("Center", jtab);
//创建3个选项卡
createTab();
createTab();
createTab();
//显示第一个选项卡
jtab.setSelectedIndex(0);
}
public void createTab() {
JLabel jl = null;
switch (jtab.getTabCount() % 3) {
case 0:
jl = new JLabel("选项卡" + jtab.getTabCount(),SwingConstants.CENTER);
break;
case 1:
jl = new JLabel("选项卡" + jtab.getTabCount(),SwingConstants.CENTER);
break;
case 2:
jl = new JLabel("选项卡" + jtab.getTabCount(),SwingConstants.CENTER);
break;
}
jl.setVerticalTextPosition(SwingConstants.BOTTOM);
jl.setHorizontalTextPosition(SwingConstants.CENTER);
jl.setOpaque(true);
jl.setBackground(Color.white);
jtab.addTab("选项卡" + jtab.getTabCount(), jl);
}
public void deleteTab() {
if (jtab.getTabCount() > 0)
jtab.removeTabAt(jtab.getTabCount() - 1);
}
public void actionPerformed(ActionEvent e) {
if (e.getSource() == topjbutton)
jtab.setTabPlacement(SwingConstants.TOP);
else if (e.getSource() == bottomjbutton)
jtab.setTabPlacement(SwingConstants.BOTTOM);
else if (e.getSource() == leftjbutton)
jtab.setTabPlacement(SwingConstants.LEFT);
else if (e.getSource() == rightjbutton)
jtab.setTabPlacement(SwingConstants.RIGHT);
else if (e.getSource() == addjbutton)
createTab();
else if (e.getSource() == removejbutton)
deleteTab();
jtab.revalidate();
jtab.repaint();
}
public static void main(String[] args) {
tabbedPaneExam jframe = new tabbedPaneExam();
jframe.addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {
System.exit(0);
}
});
jframe.pack();
jframe.setVisible(true);
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -