JTabbedPane中的组件焦点

时间:2014-04-19 10:07:17

标签: java swing user-interface jtabbedpane

我对此完全陌生.....

嗯,实际上我有一个JPanel有两个面板(比如上面板和下面板) 上面板是JPanel和 下面板是JTabbedPane ..

在JtabbedPane里面我有两个标签。 每个选项卡包含一个JPanel(具有空布局),其中包含一个或多个组件,如jtextfield,jbutton,jscrollpane(使用jtable)等.............

问题是当我切换到任何标签时,我无法获得正常的jpanel效果。 不能用鼠标点击按钮(可以通过按Tab键选择按钮n然后按空格键点击),鼠标点击时无法选择文本字段(只能通过按Tab键选择)等。

那么有什么解决办法吗?我希望jtabbedpane里面的面板像普通的jpanel一样工作:) 谢谢你:))

1 个答案:

答案 0 :(得分:0)

没有任何MCVE,很难提供帮助,所以我制作了一个简单的示例应用程序,我希望它适用于您的应用程序。

import java.awt.BorderLayout;
import java.awt.Dimension;

import javax.swing.JFrame;
import javax.swing.JPanel;


public class MainFrame extends JFrame {

    /**
     * 
     */
    private static final long serialVersionUID = 1L;
    private static MainFrame frame;

    private MainFrame() {
        this.setTitle("Focus on JTabbedPane");
        this.setSize(new Dimension(800, 600));

        this.getContentPane().add(initComponents());
        this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

    }

    private JPanel initComponents() {
        JPanel panel = new JPanel(new BorderLayout(10, 10));
        panel.add(new MyInfoPanel(), BorderLayout.NORTH);
        panel.add(new MyTabPanel(), BorderLayout.CENTER);

        return panel;
    }

    public static void createAndShowGui() {
        frame = new MainFrame();
        frame.setVisible(true);
    }



}

第一个只有Infotext的JPanel

import javax.swing.JLabel;
import javax.swing.JPanel;


public class MyInfoPanel extends JPanel {

    /**
     * 
     */
    private static final long serialVersionUID = 1L;

    public MyInfoPanel() {
        this.add(new JLabel("Some info text"));
    }

}

MyTabbedPanel

import javax.swing.JTabbedPane;


public class MyTabPanel extends JTabbedPane {

    /**
     * 
     */
    private static final long serialVersionUID = 1L;

    public MyTabPanel() {
        this.addTab("Tab 1", new FirstTabPanel());
        this.addTab("Tab 2", new SecondTabPanel());
    }

}

两个TabPanels

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JPanel;
import javax.swing.JTextField;


public class FirstTabPanel extends JPanel implements ActionListener {

    /**
     * 
     */
    private static final long serialVersionUID = 1L;
    JButton button;
    JTextField tf;
    public FirstTabPanel() {
        tf = new JTextField("Textfield");
        this.add(tf);
        button = new JButton("Button");
        button.addActionListener(this);
        this.add(button);
    }

    @Override
    public void actionPerformed(ActionEvent e) {
        if(e.getSource().equals(button)) {
            tf.setText("Button on was clicked!");
        }
    }
}

SecondTabPanel

import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;

import javax.swing.JPanel;
import javax.swing.JTextArea;
import javax.swing.JTextField;


public class SecondTabPanel extends JPanel {

    /**
     * 
     */
    private static final long serialVersionUID = 1L;

    JTextArea area;
    JTextField field;

    public SecondTabPanel() {

        this.setLayout(new BorderLayout(10, 10));

        field = new JTextField();
        this.add(field, BorderLayout.NORTH);
        area = new JTextArea();
        this.add(area, BorderLayout.CENTER);
        field.addKeyListener(new KeyListener() {

            @Override
            public void keyTyped(KeyEvent e) {
            }

            @Override
            public void keyReleased(KeyEvent e) {
                // TODO Auto-generated method stub

            }

            @Override
            public void keyPressed(KeyEvent e) {
                area.setText(area.getText() + e.getKeyChar());
            }
        });
    }


}

然后开始

import javax.swing.SwingUtilities;


public class App {

    public static void main(String[] args) {
        SwingUtilities.invokeLater(new Runnable() {

            @Override
            public void run() {
                MainFrame.createAndShowGui();
            }
        });
    }
}

希望这款迷你应用能为您提供帮助。