选项卡问题,请教一下各位。
创建一个类A继承JFrame,在A的构造方法中添加了几个选项卡组件(a,b,c,d),我再选项卡a中加了一个button按钮,我想点击button按钮,选项卡会从当前a跳到选项卡b.请教一下代码应该怎么写?
补充:选项卡(a,b,c,d)在同一个界面中。
------解决方案--------------------
package jfrm;
import java.awt.*;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import javax.swing.*;
public class Jfrm extends JFrame implements MouseListener{
JButton[] jB1 = {new JButton("欢迎页"), new JButton("欢迎页")};
JButton[] jB2 = {new JButton("人员管理"), new JButton("人员管理")};
JButton[] jB4 = {new JButton("人员管理"), new JButton("人员管理")};
JTabbedPane jTP = new JTabbedPane();
JPanel jP4 = new JPanel();
public Jfrm()
{
setSize(500, 400);
setDefaultCloseOperation(EXIT_ON_CLOSE);
JPanel[] jP1 = {new JPanel(), new JPanel()};
jP1[0].setLayout(new FlowLayout());
jP1[1].setLayout(new FlowLayout());
jP1[0].add(jB4[0]);
jP1[1].add(jB4[1]);
jB4[0].addMouseListener(this);
jB4[1].addMouseListener(this);
JPanel[] jP2 = {new JPanel(), new JPanel()};
jP2[0].setLayout(new BorderLayout());
jP2[1].setLayout(new BorderLayout());
jP2[0].add(jB1[0]);
jP2[1].add(jB2[1]);
JPanel jP3 = new JPanel();
jP3.setLayout(new BorderLayout());
jP3.add("West", jP1[0]);
jP3.add(jP2[0]);
jP4.setLayout(new BorderLayout());
jP4.add("West", jP1[1]);
jP4.add(jP2[1]);
jTP.add("欢迎页", jP3);
jTP.add("人员管理", jP4);
add(jTP);
setVisible(true);
}
public static void main(String[] args) {
new Jfrm();
}
public void mouseClicked(MouseEvent e) {
// TODO Auto-generated method stub
}
public void mouseEntered(MouseEvent e) {
// TODO Auto-generated method stub
}
public void mouseExited(MouseEvent e) {
// TODO Auto-generated method stub
}
public void mousePressed(MouseEvent e) {
if (e.getSource() == jB4[0] || e.getSource() == jB4[1])
{
jTP.setSelectedComponent(jP4) ;
}
}
public void mouseReleased(MouseEvent e) {
// TODO Auto-generated method stub
}
}
这段代码对你应该有帮助你可以自己调试下