jntabbedpane.java
来自「纯java操作系统jnode,安装简单和操作简单的个人使用的Java操作系统」· Java 代码 · 共 364 行
JAVA
364 行
package org.jnode.wt.components;
import org.jnode.wt.events.JNActionEvent;
import org.jnode.wt.events.JNodeKeyEvent;
import org.jnode.wt.events.JNodeMouseEvent;
import org.jnode.wt.layouts.JNBorderLayout;
import org.jnode.wt.layouts.JNFlowLayout;
import org.jnode.wt.layouts.JNLayoutManager;
import org.jnode.wt.events.JNActionListener;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Insets;
import java.util.Iterator;
/**
* @author Kishore
*/
public class JNTabbedPane extends JNPanel implements JNActionListener {
private JNPanel tabsPanel = new JNPanel();
// This is to hold all other components.
/* Tabbed pane contains (added) all panels, and displays,
corresponding panel basedon the one clicked or selected */
private JNPanel motherPanel = new JNPanel();
private JNPanel currentSelectedPanel = null;
/* This variable is used till ButtonGroup is implemented */
//private JNToggleButton currentSelectedTButton = null;
private int tabPlacement = JNDefaultLookAndFeel.TOP;
private ButtonGroup aButtonGroup = new ButtonGroup();
int yy = 0;
/**
* JNTabbedPane constructor comment.
*/
public JNTabbedPane() {
super();
init();
}
public void actionPerformed(JNActionEvent ae) {
JNAbstractButton source = (JNAbstractButton) ae.getSource();
if (source.isSelected()) {
int index = tabsPanel.getIndexOf(source);
JNPanel selectedPanel = (JNPanel) motherPanel.getComponent(index);
if (selectedPanel == currentSelectedPanel) {
// System.out.println("return Action perfromed, not selected *** "+ selectedPanel.getName());
return;
}
// make previous one invisible
/*
if(null != currentSelectedPanel)
{
currentSelectedPanel.setVisible(false);
}*/
// This to temp.
org.jnode.wt.components.JNComponent[] arr = motherPanel.getComponents();
for (int i = 0; i < arr.length; i++) {
arr[i].setVisible(false);
}
selectedPanel.setVisible(true);
currentSelectedPanel = selectedPanel;
tabsPanel.repaint();
// currentSelectedPanel.repaint();
motherPanel.repaint();
// this.repaint();
/* tabsPanel.layout();
motherPanel.layout();
this.layout();*/
// System.out.println("selected in Action performed %%% "+ selectedPanel.getName());
}
}
public org.jnode.wt.components.JNComponent add(org.jnode.wt.components.JNComponent comp, String tabTitle) {
if ((comp == tabsPanel) || (comp == motherPanel)) {
super.add(comp);
return comp;
}
motherPanel.add(comp);
// comp.setVisible(true);
comp.setName(tabTitle);
JNToggleButton tabButton = new JNToggleButton(tabTitle);
tabButton.setStyle(JNToggleButton.TABBEDPANE_TOGGLEBUTTON);
tabButton.setSelected(true);
tabButton.addActionListener(this);
aButtonGroup.add(tabButton);
tabsPanel.add(tabButton);
recalcPanelSizes();
// set it to the size of its container.
comp.setSize(motherPanel.getSize());
comp.setLocation(0, 0);
tabsPanel.layout();
motherPanel.layout();
this.layout();
return comp;
}
private JNPanel getPanelAt(int i) {
return (JNPanel) motherPanel.getComponent(i);
}
public int getTabPlacement() {
return tabPlacement;
}
private JNToggleButton getToggleButton(int i) {
return (JNToggleButton) tabsPanel.getComponent(i);
}
private void init() {
// super.setSize(480,303);
// tabsPanel.setBackground(java.awt.Color.red);
tabsPanel.setBackground(new Color(227, 220, 210));
motherPanel.setBackground(java.awt.Color.green);
//---
this.setLayout(new JNBorderLayout());
tabsPanel.setLayout(new JNFlowLayout());
motherPanel.setLayout(null);
tabsPanel.setInsets(new Insets(5, 5, 0, 5));
//tabsPanel.add(new JNButton("AAA"));
super.add(tabsPanel, JNBorderLayout.NORTH);
super.add(motherPanel, JNBorderLayout.CENTER);
//temp2();
}
public void processKeyEvent(JNodeKeyEvent event) {
for (Iterator it = getComponentList().iterator(); it.hasNext();) {
org.jnode.wt.components.JNComponent c = (org.jnode.wt.components.JNComponent) it.next();
if (!c.isVisible()) {
continue;
}
c.processKeyEvent(event);
}
}
public void processMouseEvent(JNodeMouseEvent event) {
// System.out.println("* ** JNPanel event at= "+event.getX()+","+ event.getY());
super.processMouseEvent(event);
event.consume();
}
public void processMouseMotionEvent(JNodeMouseEvent event) {
// System.out.println("* ** JNPanel event at= "+event.getX()+","+ event.getY());
super.processMouseMotionEvent(event);
event.consume();
}
private void recalcPanelSizes() {
int tabsHeight = (int) tabsPanel.getPreferredSize().getHeight();
//int mw = 0;
//int mh = 0;
if (tabPlacement == JNDefaultLookAndFeel.TOP) {
tabsPanel.setSize(this.getWidth(), tabsHeight);
// motherPanel.setSize( this.getWidth(), this.getHeight() - tabsHeight);
setMotherPanelSize(this.getWidth(), this.getHeight() - tabsHeight);
// - * -
// tabsPanel.setLocation(0,0);
// motherPanel.setLocation(0,this.getHeight() - tabsHeight);
// - * -
} else if (tabPlacement == JNDefaultLookAndFeel.BOTTOM) {
tabsPanel.setSize(this.getWidth(), tabsHeight);
// motherPanel.setSize( this.getWidth(), this.getHeight() - tabsHeight);
setMotherPanelSize(this.getWidth(), this.getHeight() - tabsHeight);
// - * -
// motherPanel.setLocation(0,0);
// tabsPanel.setLocation(0,this.getHeight() - motherPanel.getHeight());
// - * -
} else if (tabPlacement == JNDefaultLookAndFeel.LEFT) {
int tabswidth = (int) tabsPanel.getMinimumSize().getWidth();
//tabsPanel.setSize(tabswidth, this.getHeight());
setTabbsPanelSize_for_Left_Right(tabswidth, this.getHeight());
// motherPanel.setSize( this.getWidth()- tabsPanel.getWidth(), this.getHeight());
setMotherPanelSize(this.getWidth() - tabsPanel.getWidth(), this.getHeight());
} else if (tabPlacement == JNDefaultLookAndFeel.RIGHT) {
int tabswidth = (int) tabsPanel.getMinimumSize().getWidth();
//tabsPanel.setSize(tabswidth, this.getHeight());
setTabbsPanelSize_for_Left_Right(tabswidth, this.getHeight());
// motherPanel.setSize( this.getWidth()- tabsPanel.getWidth(), this.getHeight());
setMotherPanelSize(this.getWidth() - tabsPanel.getWidth(), this.getHeight());
}
}
// Tabbed pane layout cant be changed.
public void setLayout(JNLayoutManager lay) {
super.setLayout(new JNBorderLayout());
}
private void setMotherPanelSize(int mw, int mh) {
Dimension d = new Dimension(mw, mh);
motherPanel.setSize(d);
// set this size to mother panel child components.
org.jnode.wt.components.JNComponent arr[] = motherPanel.getComponents();
for (int i = 0; i < arr.length; i++) {
arr[i].setSize(d);
}
}
public void setSelected(int index) {
JNToggleButton tb = getToggleButton(index);
if (tb != null) {
// if(currentSelectedTButton != null)
// currentSelectedTButton.setSelected(false);
if (currentSelectedPanel != null)
currentSelectedPanel.setVisible(false);
//-- this should be removed later.
org.jnode.wt.components.JNComponent[] arr = motherPanel.getComponents();
for (int i = 0; i < arr.length; i++) {
arr[i].setVisible(false);
}
//--
tb.setSelected(true);
getPanelAt(index).setVisible(true);
getPanelAt(index).repaint();
tabsPanel.repaint();
tabsPanel.repaint();
motherPanel.repaint();
tabsPanel.layout();
motherPanel.layout();
this.layout();
// System.out.println("+++setSelected() is "+ tb.getText());
}
}
private void setTabbsPanelSize_for_Left_Right(int mw, int mh) {
Dimension d = new Dimension(mw, mh);
tabsPanel.setSize(d);
// set this size to mother panel child components.
org.jnode.wt.components.JNComponent arr[] = tabsPanel.getComponents();
Insets ti = tabsPanel.getInsets();
int x = ti.left;
int y = ti.top;
int cw = mw - (ti.left + ti.right);
for (int i = 0; i < arr.length; i++) {
arr[i].setLocation(x, y);
/* int ch = (int)(arr[i].getSize().getHeight());
arr[i].setSize( cw, ch );*/
int ch = (int) (arr[i].getPreferredSize().getHeight());
arr[i].setPreferredSize(cw, ch);
}
}
public void setTabPlacement(int tabplace) {
if (tabPlacement == tabplace) {
return;
}
tabPlacement = tabplace;
if (tabPlacement == JNDefaultLookAndFeel.TOP) {
tabsPanel.setInsets(new Insets(5, 5, 0, 5));
} else if (tabPlacement == JNDefaultLookAndFeel.BOTTOM) {
tabsPanel.setInsets(new Insets(0, 5, 5, 5));
} else if (tabPlacement == JNDefaultLookAndFeel.LEFT) {
tabsPanel.setInsets(new Insets(5, 5, 5, 0));
} else if (tabPlacement == JNDefaultLookAndFeel.RIGHT) {
tabsPanel.setInsets(new Insets(5, 0, 5, 5));
}
recalcPanelSizes();
super.remove(tabsPanel);
super.remove(motherPanel);
/*
super.add(null, JNBorderLayout.NORTH);
super.add(null, JNBorderLayout.SOUTH);
super.add(null, JNBorderLayout.WEST);
super.add(null, JNBorderLayout.EAST);
*/
//
if (tabPlacement == JNDefaultLookAndFeel.TOP) {
super.add(tabsPanel, JNBorderLayout.NORTH);
} else if (tabPlacement == JNDefaultLookAndFeel.BOTTOM) {
super.add(tabsPanel, JNBorderLayout.SOUTH);
} else if (tabPlacement == JNDefaultLookAndFeel.LEFT) {
super.add(tabsPanel, JNBorderLayout.WEST);
} else if (tabPlacement == JNDefaultLookAndFeel.RIGHT) {
super.add(tabsPanel, JNBorderLayout.EAST);
}
super.add(motherPanel, JNBorderLayout.CENTER);
super.layout();
tabsPanel.layout();
motherPanel.layout();
}
public void update(java.awt.Graphics g) {
super.update(g);
g.setColor(Color.black);
// g.drawRect(0,0,this.getWidth(),this.getHeight());
int x = (int) this.getAbsLocation().getX();
int y = (int) this.getAbsLocation().getY();
g.translate(x, y);
g.drawRect(0, 0, this.getWidth(), this.getHeight());
g.translate(-x, -y);
}
}
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?