JTabbedPane内部组件的焦点



我对此完全陌生。。。。。

事实上,我有一个JPanel,有两个面板(比如上面板和下面板)上部面板为JPanel下面的面板是JTabbedPane。。

在JtabbedPane中,我有两个选项卡。每个选项卡都包含一个JPanel(具有空布局),它有一个或多个组件,如jtextfield、jbutton、jscrollpane(带jtable)等…………

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

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

如果没有任何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());
    }
}

两个选项卡

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

希望这个迷你应用程序能帮助你。

相关内容

  • 没有找到相关文章

最新更新