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();
}
}