使用了一个JSplitPane,左边是导航面板,有按钮,右边是空的面板,击左边的导航按钮,右边就显示响应的面板窗口。
效果实现的具体方法是:(1)点击左侧按监听事件启动
(2)监听方法调用写好的改变面板显示的方法
(3)简单方法实例如下:
public void first(){
JPanel first = new JPanel();
first.setBackground(Color.black);
jsp.remove(1);
repaint();
jsp.setRightComponent(first);
validate();
}
说明:首先要删除掉JSplitPane面板中右侧的面板,调用repaint()方法后,再将要展示的面板添加到JSplitPane的右侧,再调用validate()方法。
import
java.awt.Color
;
import
java.awt.event.ActionEvent
;
import
java.awt.event.ActionListener
;
import
javax.swing.ButtonGroup
;
import
javax.swing.JFrame
;
import
javax.swing.JPanel
;
import
javax.swing.JRadioButton
;
import
javax.swing.JSplitPane
;
/**
* @author wangsheng
*/
public
class
JSplitPaneTest
extends
JFrame
{
public
JRadioButton
button1
;
public
JRadioButton
button2
;
public
JRadioButton
button3
;
public
ButtonGroup
group
;
public
JSplitPane
jsp
;
public
JPanel
rightPanel
;
public
JSplitPaneTest
()
{
initUI
();
initAction
();
}
public
JPanel
leftPanel
(){
JPanel
leftPanel
=
new
JPanel
();
button1
=
new
JRadioButton
(
"修改"
,
true
);
button1
.
setBounds
(
1
,
0
,
50
,
20
);
button2
=
new
JRadioButton
(
"添加"
);
button2
.
setBounds
(
1
,
30
,
50
,
20
);
button3
=
new
JRadioButton
(
"删除"
);
button3
.
setBounds
(
1
,
60
,
50
,
20
);
group
=
new
ButtonGroup
();
group
.
add
(
button1
);
group
.
add
(
button2
);
group
.
add
(
button3
);
leftPanel
.
add
(
button1
);
leftPanel
.
add
(
button2
);
leftPanel
.
add
(
button3
);
return
leftPanel
;
}
public
void
first
(){
JPanel
first
=
new
JPanel
();
first
.
setBackground
(
Color
.
black
);
jsp
.
remove
(
1
);
repaint
();
jsp
.
setRightComponent
(
first
);
validate
();
}
public
void
second
(){
JPanel
second
=
new
JPanel
();
second
.
setBackground
(
Color
.
blue
);
jsp
.
remove
(
1
);
repaint
();
jsp
.
setRightComponent
(
second
);
validate
();
}
public
void
third
(){
JPanel
third
=
new
JPanel
();
third
.
setBackground
(
Color
.
green
);
jsp
.
remove
(
1
);
repaint
();
jsp
.
setRightComponent
(
third
);
validate
();
}
public
void
initUI
(){
rightPanel
=
new
JPanel
();
rightPanel
.
setBackground
(
Color
.
black
);
jsp
=
new
JSplitPane
(
JSplitPane
.
HORIZONTAL_SPLIT
,
leftPanel
(),
rightPanel
);
this
.
add
(
jsp
);
this
.
setTitle
(
"拆分窗口"
);
this
.
setBounds
(
300
,
200
,
800
,
500
);
this
.
setDefaultCloseOperation
(
EXIT_ON_CLOSE
);
this
.
setVisible
(
true
);
}
public
void
initAction
(){
button1
.
addActionListener
(
new
ActionListener
()
{
@Override
public
void
actionPerformed
(
ActionEvent
e
)
{
first
();
}
});
button2
.
addActionListener
(
new
ActionListener
()
{
@Override
public
void
actionPerformed
(
ActionEvent
e
)
{
second
();
}
});
button3
.
addActionListener
(
new
ActionListener
()
{
@Override
public
void
actionPerformed
(
ActionEvent
e
)
{
third
();
}
});
}
public
static
void
main
(
String
[]
args
)
{
new
JSplitPaneTest
();
}
}
|