我想要这个窗体的代码
import java.awt.BorderLayout;
import java.awt.Font;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.ButtonGroup;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JRadioButton;
import javax.swing.JTabbedPane;
import javax.swing.SwingConstants;
import javax.swing.border.EmptyBorder;
public class JTabbedPaneTest extends JFrame {
private JTabbedPane tabbedPane;// 选项卡面板
private JRadioButton rdbtnTop; // “选项卡在顶部”单选按钮
private JRadioButton rdbtnDown; // “选项卡在底部”单选按钮
private JRadioButton rdbtnLeft; // “选项卡在左侧”单选按钮
private JRadioButton rdbtnRight; // “选项卡在右侧”单选按钮
private JRadioButton rdbtnWrap;// “限制布局”单选按钮
private JRadioButton rdbtnScroll;// “滚动布局”单选按钮
private JPanel contentPane;// 窗体容器面板
/**
* 演示选项卡面板类的构造方法
*/
public JTabbedPaneTest() {
setTitle("演示选项卡面板");// 窗体的标题
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);// 窗体的关闭方式
setBounds(100, 100, 475, 325);// 窗体的边界
// 创建内容面板,内容面板的布局为边界布局,把选项卡面板置于内容面板的中间
contentPane = new JPanel();
tabbedPane = new JTabbedPane();// 实例化选项卡面板
contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));// 无边框
contentPane.setLayout(new BorderLayout(0, 0));// 使用边界布局
contentPane.add(tabbedPane, BorderLayout.CENTER);// 选项卡面板在中部
setContentPane(contentPane);// 重新设置窗口容器
buttonsInit();// 按钮初始化
tabbedPaneInit();// 选项卡面板初始化
addListener();// 添加监听
}
private void buttonsInit() {
GridLayout gl = new GridLayout(6, 1);
JPanel buttonPanel = new JPanel(gl);
rdbtnTop = new JRadioButton("选项卡在顶部"); // “选项卡在顶部”单选按钮
rdbtnDown = new JRadioButton("选项卡在底部"); // “选项卡在底部”单选按钮
rdbtnLeft = new JRadioButton("选项卡在左侧"); // “选项卡在左侧”单选按钮
rdbtnRight = new JRadioButton("选项卡在右侧"); // “选项卡在右侧”单选按钮
ButtonGroup group1=new ButtonGroup();
group1.add(rdbtnTop);
group1.add(rdbtnDown);
group1.add(rdbtnLeft);
group1.add(rdbtnRight);
rdbtnWrap = new JRadioButton("限制布局");// “限制布局”单选按钮
rdbtnScroll = new JRadioButton("滚动布局");// “滚动布局”单选按钮
ButtonGroup group2=new ButtonGroup();
group2.add(rdbtnWrap);
group2.add(rdbtnScroll);
buttonPanel.add(rdbtnTop);
buttonPanel.add(rdbtnDown);
buttonPanel.add(rdbtnLeft);
buttonPanel.add(rdbtnRight);
buttonPanel.add(rdbtnWrap);
buttonPanel.add(rdbtnScroll);
contentPane.add(buttonPanel,BorderLayout.WEST);
}
private void tabbedPaneInit() {// 选项卡面板初始化
// 选项卡中的窗格内容(“罗永浩”)与标签内容(“我不是为了输赢,我就是认真”)
JLabel lbLYH = new JLabel("我不是为了输赢,我就是认真");
lbLYH.setFont(new Font("微软雅黑", Font.PLAIN, 14));
lbLYH.setHorizontalAlignment(SwingConstants.CENTER);
tabbedPane.addTab("罗永浩", lbLYH);// 添加选项卡标题和组件
// 选项卡中的窗格内容(“乔帮主”)与标签内容(“Stay Hungry, Stay Foolish”)
JLabel lblJobs = new JLabel("Stay Hungry, Stay Foolish");
lblJobs.setFont(new Font("微软雅黑", Font.PLAIN, 14));
lblJobs.setHorizontalAlignment(SwingConstants.CENTER);
tabbedPane.addTab("乔帮主", lblJobs);// 添加选项卡标题和组件
// 选项卡中的窗格内容(“罗振宇”)与标签内容(“死磕自己,愉悦大家”)
JLabel lblLZY = new JLabel("死磕自己,愉悦大家");
lblLZY.setFont(new Font("微软雅黑", Font.PLAIN, 14));
lblLZY.setHorizontalAlignment(SwingConstants.CENTER);
tabbedPane.addTab("罗振宇", lblLZY);// 添加选项卡标题和组件
}
private void addListener() {// 添加监听
rdbtnTop.addActionListener(new ActionListener() {// “选项卡在顶部”单选按钮添加监听
public void actionPerformed(ActionEvent e) {
tabbedPane.setTabPlacement(JTabbedPane.TOP);// 设置选项卡窗格在顶部
}
});
rdbtnLeft.addActionListener(new ActionListener() {// “选项卡在左侧”单选按钮添加监听
public void actionPerformed(ActionEvent e) {
tabbedPane.setTabPlacement(JTabbedPane.LEFT);// 设置选项卡窗格在左侧
}
});
rdbtnRight.addActionListener(new ActionListener() {// “选项卡在右侧”单选按钮添加监听
public void actionPerformed(ActionEvent e) {
tabbedPane.setTabPlacement(JTabbedPane.RIGHT);// 设置选项卡窗格在右侧
}
});
rdbtnDown.addActionListener(new ActionListener() {// “选项卡在底部”单选按钮添加监听
public void actionPerformed(ActionEvent e) {
tabbedPane.setTabPlacement(JTabbedPane.BOTTOM);// 设置选项卡窗格在底部
}
});
rdbtnWrap.addActionListener(new ActionListener() {// “限制布局”单选按钮添加监听
public void actionPerformed(ActionEvent e) {
tabbedPane.setTabLayoutPolicy(JTabbedPane.WRAP_TAB_LAYOUT);// 设置选项卡布局为限制布局
}
});
rdbtnScroll.addActionListener(new ActionListener() {// “滚动布局”单选按钮添加监听
public void actionPerformed(ActionEvent e) {
tabbedPane.setTabLayoutPolicy(JTabbedPane.SCROLL_TAB_LAYOUT);// 设置选项卡布局为滚动布局
}
});
}
public static void main(String[] args) {
new JTabbedPaneTest().setVisible(true);;
}
}
根号申 发表于2019-04-10 09:15
import java.awt.BorderLayout;
import java.awt.Font;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.ButtonGroup;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JRadioButton;
import javax.swing.JTabbedPane;
import javax.swing.SwingConstants;
import javax.swing.border.EmptyBorder;
public class JTabbedPaneTest extends JFrame {
private JTabbedPane tabbedPane;// 选项卡面板
private JRadioButton rdbtnTop; // “选项卡在顶部”单选按钮
private JRadioButton rdbtnDown; // “选项卡在底部”单选按钮
private JRadioButton rdbtnLeft; // “选项卡在左侧”单选按钮
private JRadioButton rdbtnRight; // “选项卡在右侧”单选按钮
private JRadioButton rdbtnWrap;// “限制布局”单选按钮
private JRadioButton rdbtnScroll;// “滚动布局”单选按钮
private JPanel contentPane;// 窗体容器面板
/**
* 演示选项卡面板类的构造方法
*/
public JTabbedPaneTest() {
setTitle("演示选项卡面板");// 窗体的标题
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);// 窗体的关闭方式
setBounds(100, 100, 475, 325);// 窗体的边界
// 创建内容面板,内容面板的布局为边界布局,把选项卡面板置于内容面板的中间
contentPane = new JPanel();
tabbedPane = new JTabbedPane();// 实例化选项卡面板
contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));// 无边框
contentPane.setLayout(new BorderLayout(0, 0));// 使用边界布局
contentPane.add(tabbedPane, BorderLayout.CENTER);// 选项卡面板在中部
setContentPane(contentPane);// 重新设置窗口容器
buttonsInit();// 按钮初始化
tabbedPaneInit();// 选项卡面板初始化
addListener();// 添加监听
}
private void buttonsInit() {
GridLayout gl = new GridLayout(6, 1);
JPanel buttonPanel = new JPanel(gl);
rdbtnTop = new JRadioButton("选项卡在顶部"); // “选项卡在顶部”单选按钮
rdbtnDown = new JRadioButton("选项卡在底部"); // “选项卡在底部”单选按钮
rdbtnLeft = new JRadioButton("选项卡在左侧"); // “选项卡在左侧”单选按钮
rdbtnRight = new JRadioButton("选项卡在右侧"); // “选项卡在右侧”单选按钮
ButtonGroup group1=new ButtonGroup();
group1.add(rdbtnTop);
group1.add(rdbtnDown);
group1.add(rdbtnLeft);
group1.add(rdbtnRight);
rdbtnWrap = new JRadioButton("限制布局");// “限制布局”单选按钮
rdbtnScroll = new JRadioButton("滚动布局");// “滚动布局”单选按钮
ButtonGroup group2=new ButtonGroup();
group2.add(rdbtnWrap);
group2.add(rdbtnScroll);
buttonPanel.add(rdbtnTop);
buttonPanel.add(rdbtnDown);
buttonPanel.add(rdbtnLeft);
buttonPanel.add(rdbtnRight);
buttonPanel.add(rdbtnWrap);
buttonPanel.add(rdbtnScroll);
contentPane.add(buttonPanel,BorderLayout.WEST);
}
private void tabbedPaneInit() {// 选项卡面板初始化
// 选项卡中的窗格内容(“罗永浩”)与标签内容(“我不是为了输赢,我就是认真”)
JLabel lbLYH = new JLabel("我不是为了输赢,我就是认真");
lbLYH.setFont(new Font("微软雅黑", Font.PLAIN, 14));
lbLYH.setHorizontalAlignment(SwingConstants.CENTER);
tabbedPane.addTab("罗永浩", lbLYH);// 添加选项卡标题和组件
// 选项卡中的窗格内容(“乔帮主”)与标签内容(“Stay Hungry, Stay Foolish”)
JLabel lblJobs = new JLabel("Stay Hungry, Stay Foolish");
lblJobs.setFont(new Font("微软雅黑", Font.PLAIN, 14));
lblJobs.setHorizontalAlignment(SwingConstants.CENTER);
tabbedPane.addTab("乔帮主", lblJobs);// 添加选项卡标题和组件
// 选项卡中的窗格内容(“罗振宇”)与标签内容(“死磕自己,愉悦大家”)
JLabel lblLZY = new JLabel("死磕自己,愉悦大家");
lblLZY.setFont(new Font("微软雅黑", Font.PLAIN, 14));
lblLZY.setHorizontalAlignment(SwingConstants.CENTER);
tabbedPane.addTab("罗振宇", lblLZY);// 添加选项卡标题和组件
}
private void addListener() {// 添加监听
rdbtnTop.addActionListener(new ActionListener() {// “选项卡在顶部”单选按钮添加监听
public void actionPerformed(ActionEvent e) {
tabbedPane.setTabPlacement(JTabbedPane.TOP);// 设置选项卡窗格在顶部
}
});
rdbtnLeft.addActionListener(new ActionListener() {// “选项卡在左侧”单选按钮添加监听
public void actionPerformed(ActionEvent e) {
tabbedPane.setTabPlacement(JTabbedPane.LEFT);// 设置选项卡窗格在左侧
}
});
rdbtnRight.addActionListener(new ActionListener() {// “选项卡在右侧”单选按钮添加监听
public void actionPerformed(ActionEvent e) {
tabbedPane.setTabPlacement(JTabbedPane.RIGHT);// 设置选项卡窗格在右侧
}
});
rdbtnDown.addActionListener(new ActionListener() {// “选项卡在底部”单选按钮添加监听
public void actionPerformed(ActionEvent e) {
tabbedPane.setTabPlacement(JTabbedPane.BOTTOM);// 设置选项卡窗格在底部
}
});
rdbtnWrap.addActionListener(new ActionListener() {// “限制布局”单选按钮添加监听
public void actionPerformed(ActionEvent e) {
tabbedPane.setTabLayoutPolicy(JTabbedPane.WRAP_TAB_LAYOUT);// 设置选项卡布局为限制布局
}
});
rdbtnScroll.addActionListener(new ActionListener() {// “滚动布局”单选按钮添加监听
public void actionPerformed(ActionEvent e) {
tabbedPane.setTabLayoutPolicy(JTabbedPane.SCROLL_TAB_LAYOUT);// 设置选项卡布局为滚动布局
}
});
}
public static void main(String[] args) {
new JTabbedPaneTest().setVisible(true);;
}
}
谢谢,谢谢,太感谢你了