Java graphical interface --- JSplitPane and JTabbedPane

Table of contents

1. JSplitPane

(1) Introduction to JSplitPane

(2) Steps to use JSplitPane

(3) Case

Two, JTabbedPane

(1) Introduction to JTabbedPane

(2) Steps to use JTabbedPane

(3) Case


1. JSplitPane

(1) Introduction to JSplitPane

JSplitPane is used to create a split panel, which can split a component (usually a container) into two parts and provide a split bar that the user can drag to adjust the size of the two parts.

(2) Steps to use JSplitPane

 Steps to use JSplitPane:
(1) Create an object
new JSpliyPane(int n,Component s1,Component s2)
n specifies the split direction of the JSplitPane container, the value can be vertical split JSplitPane.VERTICAL_SPLIT, or horizontal split: JSplitPane.HORIZONTAL_SPLIT.s1
: Components on the left or above
s2: components on the right or below

(2) Set whether to enable continuous layout support (optional)
setContinousLayout (boolean n)
   is off by default, and true is on, but since continuous layout support needs to support continuous redrawing of components, the efficiency will be lower.

(3) Set whether to support one-touch expansion
setOneTouchExpandable(boolean b)
is off by default, true is on

(4) Other settings
setDivderLocation (double p) Set the position of the divider to a certain percentage of JSplitPane 
setDivderLocation(int l) Set the position of the divider by pixel
setDiverSize(int n) Set the size of the divider by pixel value
setLeftComponent(Component c) Set the specified component

(3) Case

Case requirements: Create an interface as shown in the figure, click the list on the right, and the left will change to introduction information

 

public class book {
    private String name;//名字
    private Icon icon;//图片
    private String desc;//介绍信息
    public book(String name,Icon icon,String desc){
        this.name=name;
        this.desc=desc;
        this.icon=icon;
    }
    public String getName(){
        return name;
    }
    public void setName(String name){
        this.name=name;
    }
    public Icon getIcon(){
        return icon;
    }
    public void setIcon(Icon icon){
        this.icon=icon;
    }
    public String getDesc(){
        return desc;
    }
    public void setDesc(String desc){
        this.desc=desc;
    }

    @Override
    public String toString() {
        return name;
    }
}

public class exer{
    book[] books={new book("java自学宝典",new ImageIcon("D:\\idea\\project\\pro1\\exercise1\\image\\java.png"),"国内最全的Java教程\n,看得懂,学得会"),
            new book("轻量级的JAVAEE企业实战",new ImageIcon("D:\\idea\\project\\pro1\\exercise1\\image\\ee.png"),"开发经典图书,你值得拥有"),
            new book("Android基础教程",new ImageIcon("D:\\idea\\project\\pro1\\exercise1\\image\\android.png"),"教你手把手开发安卓")};
    JFrame s=new JFrame("程序练习");
    JList<book> bookJList=new JList<>(books);
    JLabel bookcover=new JLabel();
    JTextArea bookdesc=new JTextArea();
    public void init(){
        bookJList.setPreferredSize(new Dimension(150,400));
        bookcover.setPreferredSize(new Dimension(220,270));
        bookdesc.setPreferredSize(new Dimension(220,130));
        bookJList.addListSelectionListener(new ListSelectionListener() {
            @Override
            public void valueChanged(ListSelectionEvent e) {
                book b=bookJList.getSelectedValue();
                bookcover.setIcon(b.getIcon());
                bookdesc.setText(b.getDesc());
            }
        });
        //组装左边的
        JSplitPane left=new JSplitPane(JSplitPane.VERTICAL_SPLIT,bookcover,new JScrollPane(bookdesc));//出现JScrollPane是为了出现滚动条
        left.setOneTouchExpandable(true);//支持一触即展
        //组装整体
        JSplitPane all=new JSplitPane(JSplitPane.HORIZONTAL_SPLIT,left,bookJList);
        s.add(all);

        s.setDefaultCloseOperation(3);
        s.pack();
        s.setVisible(true);
    }
}

Two, JTabbedPane

(1) Introduction to JTabbedPane

JTabbedPane can conveniently place multiple tabs on the window, and each tab is equivalent to obtaining a component placement area with the same size as the external container. In this way, more components can be contained in one container.

(2) Steps to use JTabbedPane

Steps to use:
(1) Create an object
JTabbedPane(int tp,int tl)
tp: Specify the placement position of the tab title, you can choose four constants in SwingConstants: Top, LEFT, BOTTOM, RIGHT.
tl: Specify the layout strategy when the window cannot accommodate the title of the tab page, you can choose JTabbedPane.WRAP_TAB_LAYOUT and JTabbedPane.SCROLL_TAB_LAYOUT

(2) Add, delete, modify and check through the JTabbedPane object heap tag

addTab(String t,Icon i,Component c,String tip); add label
 t: the name of the label
 i: the icon of the label
 c: the component corresponding to the label
 tip: the prompt when the cursor is placed on the label

insertTab(String t, Icon i, Component c, String tip, int index) insert tab page t
 : the name of the label
 i: the icon of the label
 c: the component corresponding to the label
 tip: the prompt for placing the cursor on the label
 index: at that index insert tab at

setComponentAt(int i, Component c) Modify the component i corresponding to the label page
 : modify the label c at which index
 : the component corresponding to the label

removeTabAt(int i)
 i: delete the label at which index

(3) Set the currently displayed tab page
setSelectIndex(int ​​i) set the tab at which index is selected

(4) Set other attributes of JTabbedPane
setDisabledIconAt(int i,Icon d) to set the disabled icon at the specified position as icon, and the icon can also be null to indicate that the disabled icon is not used.


setEnabledAt(int i, boolean b) whether to enable the label at the specified position
setTitleAt(int i,String t) set the title of the tab page at the specified position t, if it is null, it means empty
setToolTipTextAt(int i,String t) set the specified Hint text for the location tab.

(5) Set the listener
addChangeListener() for JTabbedPane

(3) Case

Case requirements: make the interface as shown in the figure

public class exer{
    JFrame s=new JFrame("程序练习");
    JTabbedPane tabbedPane=new JTabbedPane(SwingConstants.LEFT,JTabbedPane.SCROLL_TAB_LAYOUT);

    public void init(){
        tabbedPane.addTab("用户管理",new ImageIcon("D:\\idea\\project\\pro1\\image\\弄玉.gif"),new JList<String>(new String[]{"用户1","用户2","用户3"}));
        tabbedPane.addTab("订单管理",new ImageIcon("D:\\idea\\project\\pro1\\image\\李清照.gif"),new JList<String>(new String[]{"订单1","订单2","订单3"}));
        tabbedPane.addTab("商品管理",new ImageIcon("D:\\idea\\project\\pro1\\image\\虎头.gif"),new JList<String>(new String[]{"商品1","商品2","商品3"}));

        tabbedPane.setEnabledAt(0,false);//将用户管理禁用,0是下标
        tabbedPane.setSelectedIndex(1);//设置为默认选中
        tabbedPane.addChangeListener(new ChangeListener() {
            @Override
            public void stateChanged(ChangeEvent e) {
                int selectindex=tabbedPane.getSelectedIndex();
                JOptionPane.showMessageDialog(s,"当前选中第"+(selectindex+1)+"个标签");

            }
        });
        s.add(tabbedPane);
        s.setBounds(400,400,400,400);

        s.setDefaultCloseOperation(3);
        s.setVisible(true);

    }
}

Guess you like

Origin blog.csdn.net/gaoqiandr/article/details/128737304
Recommended