jsplitpane里面不是有个onetouchexpandable属性吗,比方说加了个水平方向的jsplitpane,那么就有向上以及向下的按钮,我现在只想要一个向上的按钮该怎么写啊,请指教,多谢啦~
解决方案 »
- 关于java applet的问题
- reflection in action适合刚入行的java程序员么
- 【求助】为什么宽带连接后多播的recieve就阻塞呢
- 关于一个简单的算法,如何用Java实现
- 新年来了,来个具体点的,什么叫强转,记得是什么叫强转,而不是强转的用法!!!!!
- 各位大哥,请教一个多线程的问题!!!!!!!!!
- 弱弱的问下Frame能象Applet一样绘图吗
- 怎样更新jar包中的某个class文件??
- 想學習SWT,但找不到資料.請大家給我一些.要電子書的.
- 请问谁有JAVA调用MS FLEX GRID的例子,我自己看了半天还是芸芸的,或者类似的例子.
- 系统属性
- 提问一个java语句的问题!!散分
在UI的方法里重写下面的方法:
public BasicSplitPaneDivider createDefaultDivider() {
return new MetalSplitPaneDivider(this);//这里返回
}
protected void oneTouchExpandableChanged() {
if (!DefaultLookup.getBoolean(splitPane, splitPaneUI,
"SplitPane.supportsOneTouchButtons", true)) {
// Look and feel doesn't want to support one touch buttons, bail.
return;
}
if (splitPane.isOneTouchExpandable() &&
leftButton == null &&
rightButton == null) {
/* Create the left button and add an action listener to
expand/collapse it. */
leftButton = createLeftOneTouchButton();
if (leftButton != null)
leftButton.addActionListener(new OneTouchActionHandler(true));
/* Create the right button and add an action listener to
expand/collapse it. */
rightButton = createRightOneTouchButton();
if (rightButton != null)
rightButton.addActionListener(new OneTouchActionHandler
(false)); if (leftButton != null && rightButton != null) {
add(leftButton);
add(rightButton);//把这个注掉就好了
}
}
revalidate();
}
这里不把rightbutton给add进去就好了,现在我的界面上的确只剩一个向上的按钮了,
但响应不知道怎么加,
原来有响应的代码,但leftButton.addActionListener(new OneTouchActionHandler(true));
这一句里面的OneTouchActionHandler这东西是个private的,
我没办法访问,照抄一把以后里面又有private的东西,这样总是把代码都copy进去也不是个事啊,像这种情况有没有啥比较好的办法啊,多谢啦~
private JButton leftBt = null;private JButton rightBt = null;protected JButton createLeftOneTouchButton(){
leftBt = new JButton(){;
.....//这里是你的处理逻辑。
};
return leftBt;
}protected JButton createRightOneTouchButton(){
rightBt = new JButton(){;
.....//这里是你的处理逻辑。
};
return rightBt;
}private void removeRightBt(){
if(rightBt == null){
return;
}
remove(rightBt);//删除按钮
}protected void oneTouchExpandableChanged() {
super.oneTouchExpandableChanged();
this.removeRightBt();
revalidate();
}
void setKeepHidden(boolean keepHidden) {
this.keepHidden = keepHidden;
}
注意访问控制,你自己写的divider无法调用SplitPaneUI的这个方法,
所以divider和SplitPaneUI要一起修改
就用7楼的那个办法就行,8楼的办法也是可以的,
还要多谢9楼的提醒,的确要把SplitPaneUI也重写一下,否则事件的响应是加不进去的,
不过有个初始化的问题不知道怎么解决,是这样的://这是我写的SplitPaneUI,因为有几个private的东西访问不到,只好copy一下了
public class NewSplitPaneUI extends BasicSplitPaneUI{
private boolean keepHidden = false;
void setKeepHidden(boolean keepHidden) {
this.keepHidden = keepHidden;
}
}
//这是我写的divider
public class NewSplitPaneDivider extends BasicSplitPaneDivider { private static final long serialVersionUID = 3963216987946515073L;
protected NewSplitPaneUI splitPaneUI;
//********************************************************
//如9楼的兄弟所说,还是得写一个,我猜这里应该要初始化一下吧,该怎么初始化好呢
//这东西在BasicSplitPaneDivider里面声明的时候就一句话
//protected BasicSplitPaneUI splitPaneUI;
//******************************************************** public NewSplitPaneDivider(BasicSplitPaneUI ui) {
super(ui);
}
protected void oneTouchExpandableChanged() {
if (!DefaultLookup.getBoolean(splitPane, splitPaneUI,
"SplitPane.supportsOneTouchButtons", true)) {
return;
}
if (splitPane.isOneTouchExpandable() &&
leftButton == null &&
rightButton == null) {
leftButton = createLeftOneTouchButton();
if (leftButton != null)
leftButton.addActionListener(new OneTouchActionHandler(true)); rightButton = createRightOneTouchButton(); if (leftButton != null && rightButton != null) {
add(leftButton);
//add(rightButton);注掉这一句就能只显示一个向上的按钮了
}
}
revalidate();
}
private void revalidate() {
invalidate();
if (splitPane != null) {
splitPane.revalidate();
}
}
//**************************************************************
//OneTouchActionHandler 这个东西是个private的,只好copy过来
//**************************************************************
private class OneTouchActionHandler implements ActionListener { private boolean toMinimum; OneTouchActionHandler(boolean toMinimum) {
this.toMinimum = toMinimum;
} public void actionPerformed(ActionEvent e) {
Insets insets = splitPane.getInsets();
int lastLoc = splitPane.getLastDividerLocation();
int currentLoc = splitPaneUI.getDividerLocation(splitPane);//*************************************************************************************
//上面这一句会报错,空指针,原因应该是splitPaneUI初始化不对
//*************************************************************************************
int newLoc;
if (toMinimum) {
if (orientation == JSplitPane.VERTICAL_SPLIT) {
if (currentLoc >= (splitPane.getHeight() -
insets.bottom - getHeight())) {
int maxLoc = splitPane.getMaximumDividerLocation();
newLoc = Math.min(lastLoc, maxLoc);
splitPaneUI.setKeepHidden(false);
add(leftButton);
remove(rightButton);
}
else {
newLoc = insets.top;
splitPaneUI.setKeepHidden(true);
add(rightButton);
remove(leftButton);
}
}
else {
if (currentLoc >= (splitPane.getWidth() -
insets.right - getWidth())) {
int maxLoc = splitPane.getMaximumDividerLocation();
newLoc = Math.min(lastLoc, maxLoc);
splitPaneUI.setKeepHidden(false);
}
else {
newLoc = insets.left;
splitPaneUI.setKeepHidden(true);
}
}
}
else {
if (orientation == JSplitPane.VERTICAL_SPLIT) {
if (currentLoc == insets.top) {
int maxLoc = splitPane.getMaximumDividerLocation();
newLoc = Math.min(lastLoc, maxLoc);
splitPaneUI.setKeepHidden(false);
}
else {
newLoc = splitPane.getHeight() - getHeight() -
insets.top;
splitPaneUI.setKeepHidden(true);
}
}
else {
if (currentLoc == insets.left) {
int maxLoc = splitPane.getMaximumDividerLocation();
newLoc = Math.min(lastLoc, maxLoc);
splitPaneUI.setKeepHidden(false);
}
else {
newLoc = splitPane.getWidth() - getWidth() -
insets.left;
splitPaneUI.setKeepHidden(true);
}
}
}
if (currentLoc != newLoc) {
splitPane.setDividerLocation(newLoc);
splitPane.setLastDividerLocation(currentLoc);
}
}
}
}
super(ui);
this.splitPaneUI = (NewSplitPaneUI) ui;
}
应有人要求,顺便把代码贴出来~
//主函数中的声明和调用
JSplitPane splitPane = new JSplitPane();
splitPane.setUI(new NewSplitPaneUI());
splitPane.disable();
//NewSplitPaneUI.java
import javax.swing.plaf.basic.BasicSplitPaneDivider;
import javax.swing.plaf.basic.BasicSplitPaneUI;public class NewSplitPaneUI extends BasicSplitPaneUI{
private boolean keepHidden = false; public NewSplitPaneUI() {
super();
} void setKeepHidden(boolean keepHidden) {
this.keepHidden = keepHidden;
}
public BasicSplitPaneDivider createDefaultDivider()
{
return new NewSplitPaneDivider( this );
}
}
//NewSplitPaneDivider.java
import java.awt.Insets;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;import javax.swing.JSplitPane;
import javax.swing.plaf.basic.BasicSplitPaneDivider;
import javax.swing.plaf.basic.BasicSplitPaneUI;import sun.swing.DefaultLookup;public class NewSplitPaneDivider extends BasicSplitPaneDivider { private static final long serialVersionUID = 3963216987946515073L;
protected NewSplitPaneUI splitPaneUI = new NewSplitPaneUI(); public NewSplitPaneDivider(BasicSplitPaneUI ui) {
super(ui);
}
protected void oneTouchExpandableChanged() {
if (!DefaultLookup.getBoolean(splitPane, splitPaneUI,
"SplitPane.supportsOneTouchButtons", true)) {
// Look and feel doesn't want to support one touch buttons, bail.
return;
}
if (splitPane.isOneTouchExpandable() &&
leftButton == null &&
rightButton == null) {
/* Create the left button and add an action listener to
expand/collapse it. */
leftButton = createLeftOneTouchButton();
if (leftButton != null)
leftButton.addActionListener(new OneTouchActionHandler(true));
/* Create the right button and add an action listener to
expand/collapse it. */
rightButton = createRightOneTouchButton();
if (rightButton != null)
rightButton.addActionListener(new OneTouchActionHandler
(false)); if (leftButton != null && rightButton != null) {
add(leftButton);
//add(rightButton);
}
}
revalidate();
}
private void revalidate() {
invalidate();
if (splitPane != null) {
splitPane.revalidate();
}
}
/**
* Listeners installed on the one touch expandable buttons.
*/
private class OneTouchActionHandler implements ActionListener {
/** True indicates the resize should go the minimum (top or left)
* vs false which indicates the resize should go to the maximum.
*/
private boolean toMinimum; OneTouchActionHandler(boolean toMinimum) {
this.toMinimum = toMinimum;
} public void actionPerformed(ActionEvent e) {
Insets insets = splitPane.getInsets();
int lastLoc = splitPane.getLastDividerLocation();
int currentLoc = splitPane.getDividerLocation();//splitPaneUI.getDividerLocation(splitPane);
int newLoc; // We use the location from the UI directly, as the location the
// JSplitPane itself maintains is not necessarly correct.
if (toMinimum) {
if (orientation == JSplitPane.VERTICAL_SPLIT) {
if (currentLoc >= (splitPane.getHeight() -
insets.bottom - getHeight())) {
int maxLoc = splitPane.getMaximumDividerLocation();
newLoc = Math.min(lastLoc, maxLoc);
splitPaneUI.setKeepHidden(false);
add(leftButton);
remove(rightButton);
}
else {
newLoc = insets.top;
splitPaneUI.setKeepHidden(true);
add(rightButton);
remove(leftButton);
}
}
else {
if (currentLoc >= (splitPane.getWidth() -
insets.right - getWidth())) {
int maxLoc = splitPane.getMaximumDividerLocation();
newLoc = Math.min(lastLoc, maxLoc);
splitPaneUI.setKeepHidden(false);
}
else {
newLoc = insets.left;
splitPaneUI.setKeepHidden(true);
}
}
}
else {
if (orientation == JSplitPane.VERTICAL_SPLIT) {
if (currentLoc == insets.top) {
int maxLoc = splitPane.getMaximumDividerLocation();
newLoc = Math.min(lastLoc, maxLoc);
splitPaneUI.setKeepHidden(false);
add(leftButton);
remove(rightButton);
}
else {
newLoc = splitPane.getHeight() - getHeight() -
insets.top;
splitPaneUI.setKeepHidden(true);
add(rightButton);
remove(leftButton);
}
}
else {
if (currentLoc == insets.left) {
int maxLoc = splitPane.getMaximumDividerLocation();
newLoc = Math.min(lastLoc, maxLoc);
splitPaneUI.setKeepHidden(false);
}
else {
newLoc = splitPane.getWidth() - getWidth() -
insets.left;
splitPaneUI.setKeepHidden(true);
}
}
}
if (currentLoc != newLoc) {
splitPane.setDividerLocation(newLoc);
// We do this in case the dividers notion of the location
// differs from the real location.
splitPane.setLastDividerLocation(currentLoc);
}
}
}
}