我是靠谱客的博主 爱撒娇大雁,最近开发中收集的这篇文章主要介绍分割面板、选项卡、桌面面板和内部窗体(JSplitPane 、JTabbedPane 和JDesktopPane )(简单代码例子实现),觉得挺不错的,现在分享给大家,希望可以做个参考。
概述
分割面板代码实例展示:
import java.awt.BorderLayout;
import java.awt.Container;
import java.sql.Connection;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JSplitPane;
public class Main extends JFrame{
public Main() {
setTitle("分割面板");
setBounds(100, 200, 300, 300);
setDefaultCloseOperation(EXIT_ON_CLOSE);
setVisible(true);
Container c = getContentPane();
JSplitPane jsp = new JSplitPane();//默认水平分割
// JSplitPane jsp = new JSplitPane(JSplitPane.HORIZONTAL_SPLIT);
c.add(jsp,BorderLayout.CENTER);
jsp.setLeftComponent(new JLabel("左边"));
JSplitPane jsp2 = new JSplitPane(JSplitPane.VERTICAL_SPLIT);//垂直分割
jsp2.setLeftComponent(new JLabel("上面 "));
jsp2.setRightComponent(new JLabel("下面 "));
jsp.setRightComponent(jsp2);
jsp2.setDividerSize(20);//分隔条宽度的像素
jsp2.setContinuousLayout(true);//重绘分隔条
jsp2.setOneTouchExpandable(true);//设置UI小部件
jsp2.setDividerLocation(60);//设置上方高度为30像素
}
public static void main(String args[]) {
new Main();
}
}
运行结果:
选项卡代码实例展示(图片我放src下了):
import java.awt.*;
import java.net.*;
import javax.swing.*;
import javax.swing.event.*;
public class Main extends JFrame {
public static void main(String args[]) {
Main frame = new Main();
frame.setVisible(true);
}
public Main() {
super();
getContentPane().setFocusCycleRoot(true);
setTitle("选项卡面板");
setBounds(100, 100, 500, 375);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
//选项卡的构造方式
final JTabbedPane tabbedPane = new JTabbedPane();
// 设置选项卡标签的布局方式
tabbedPane.setTabLayoutPolicy(JTabbedPane.SCROLL_TAB_LAYOUT);
tabbedPane.addChangeListener(new ChangeListener() {//设置监听事件
public void stateChanged(ChangeEvent e) {
// 获得被选中选项卡的索引
int selectedIndex = tabbedPane.getSelectedIndex();
// 获得指定索引的选项卡标签
String title = tabbedPane.getTitleAt(selectedIndex);
System.out.println(title);
}
});
//图片的插入
getContentPane().add(tabbedPane, BorderLayout.CENTER);
URL resource = Main.class.getResource("/tab.JPG");
ImageIcon imageIcon = new ImageIcon(resource);
final JLabel tabLabelA = new JLabel();
tabLabelA.setText("桃花潭水深千尺,不及汪伦送我情");//内容
//第一个参数是:标签的标题;第二个参数是:图片;第三个人参数是:文本框中的内容;;第四个参数是:鼠标悬停提示
tabbedPane.addTab("李白", imageIcon, tabLabelA, "诗词查看");
// tabbedPane.setTabPlacement(JTabbedPane.LEFT);//选项卡放在右边
// tabbedPane.setTabLayoutPolicy(JTabbedPane.WRAP_TAB_LAYOUT);//限制布局
// tabbedPane.setTabLayoutPolicy(JTabbedPane.SCROLL_TAB_LAYOUT);//滚动布局
final JLabel tabLabelB = new JLabel();
tabLabelB.setText("人生自古谁无死?留取丹心照汗青");
tabbedPane.addTab("文天祥", imageIcon, tabLabelB,"概略");
final JLabel tabLabelC = new JLabel();
tabLabelC.setText("两个黄鹂鸣翠柳 ,一行白鹭上青天。");
tabbedPane.addTab("杜甫", imageIcon, tabLabelC, "杜甫诗词");
final JLabel tabLabelD = new JLabel();
tabLabelD.setText("但愿人长久千里共婵娟");
tabbedPane.addTab("苏轼", imageIcon, tabLabelD, "苏轼诗词");
tabbedPane.setSelectedIndex(2); // 设置索引为2的选项卡被选中
tabbedPane.setEnabledAt(2, false); // 设置索引为0的选项卡不可用
}
}
运行结果
以下截图是对注释的部分代码功能的演示:
单独运行这段代码:
tabbedPane.setTabPlacement(JTabbedPane.LEFT);//选项卡放在右边
结果为:
单独运行这段代码: tabbedPane.setTabLayoutPolicy(JTabbedPane.WRAP_TAB_LAYOUT);//限制布局
结果为:
单独运行这段代码:
tabbedPane.setTabLayoutPolicy(JTabbedPane.SCROLL_TAB_LAYOUT);//滚动布局
运行结果为
桌面面板和内部窗体代码实例展示:
import java.awt.*;
import java.net.*;
import javax.swing.*;
import javax.swing.event.*;
public class Main extends JFrame{
public Main() {
setTitle("桌面");
setBounds(100, 200, 500, 500);
setDefaultCloseOperation(EXIT_ON_CLOSE);
setVisible(true);
Container c = getContentPane();
JDesktopPane desk = new JDesktopPane();
c.add(desk,BorderLayout.CENTER);
JInternalFrame inFrame = new JInternalFrame();
inFrame.setVisible(true);//显示窗体
inFrame.setBounds(10, 10, 250, 180);//设置位置和大小
inFrame.setTitle("内部窗体");
inFrame.setResizable(true);
inFrame.setIconifiable(true);//允许最小化
inFrame.setMaximizable(true);//允许最大化
inFrame.setClosable(true);//允许关闭
ImageIcon icon = new ImageIcon("src/tab.JPG");
inFrame.setFrameIcon(icon);//设置图标
JLabel j = new JLabel("我是内部窗体");
inFrame.add(j);
desk.add(inFrame);
}
public static void main(String args[]) {
new Main();
}
}
运行结果:
最后
以上就是爱撒娇大雁为你收集整理的分割面板、选项卡、桌面面板和内部窗体(JSplitPane 、JTabbedPane 和JDesktopPane )(简单代码例子实现)的全部内容,希望文章能够帮你解决分割面板、选项卡、桌面面板和内部窗体(JSplitPane 、JTabbedPane 和JDesktopPane )(简单代码例子实现)所遇到的程序开发问题。
如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。
本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
发表评论 取消回复