JAVA 如何通过 mousePosition 从 JTabbedPane 获取 Tab
JAVA How to get Tab from JTabbedPane by mousePosition
刚遇到这个问题。将 SSCE 带在身边! :-)
我想通过 double-clicking 在其 "Title" 获取选项卡内容(组件)。
我给你这个 SSCE,在这种情况下,我想在 tab-menu-title 之前 double-clicking 收到 JLabel。 (绿色选项卡)我想接收 JLabel "I am label 2!"。
有代码:
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class SSCE extends JFrame {
private JTabbedPane tab;
public SSCE() {
tab = new JTabbedPane();
this.add(tab);
JLabel p = new JLabel("I am label 1!");
tab.addTab("Red tab",p );
JLabel p2 = new JLabel("I am label 2!");
tab.addTab("Green tab",p2 );
JLabel p3 = new JLabel("I am label 3!");
tab.addTab("Blue tab",p3 );
JLabel p4 = new JLabel("I am label 4!");
tab.addTab("Cyan tab", p4 );
tab.addMouseListener(new MouseAdapter(){
@Override
public void mousePressed(MouseEvent e) {
if ( e.getClickCount() > 1) {
Component c = tab.getComponentAt(new Point(e.getX(), e.getY()));
//TODO Find the right label and print it! :-)
JLabel innerComponent = (JLabel) c;
System.out.println("Found:" + innerComponent.getText());
}
}
});
}
public static void main(final String[] args) throws Exception {
SSCE start = new SSCE();
start.setDefaultCloseOperation(EXIT_ON_CLOSE);
start.setVisible(true);
start.setSize(new Dimension(450,300));
}
}
有办法做到吗?我尝试了很多东西。但没有运气:-(
非常感谢您的帮助!
我想做的是为 JTabbedPane 实现功能。所以当您 double-click 在 "Title" 时,它将打开一个对话框,其中包含您 [=33] 的选项卡内容=]编于。
我知道如何创建对话框等等。但是我不知道如何从标题上的 mouse-clicking 获取组件。
Component c = tab.getComponentAt(new Point(e.getX(), e.getY()));
您不想在您点击的地方获取组件。您想从所选选项卡中获取组件。
代码应该是:
//int index = tab.getSelectedTab(); // oops, this was a typo
int index = tab.getSelectedIndex();
Component c = tab.getComponentAt( index );
how to get the component just from the mouse-clicking on the title.
我猜你在找 JTabbedPane#indexAtLocation(int, int)?
Returns the tab index corresponding to the tab whose bounds intersect the specified location. Returns -1 if no tab intersects the location.
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class SSCE2 extends JFrame {
private JTabbedPane tab;
public SSCE2() {
tab = new JTabbedPane();
this.add(tab);
JLabel p = new JLabel("I am label 1!");
tab.addTab("Red tab", p);
JLabel p2 = new JLabel("I am label 2!");
tab.addTab("Green tab", p2);
JLabel p3 = new JLabel("I am label 3!");
tab.addTab("Blue tab", p3);
JLabel p4 = new JLabel("I am label 4!");
tab.addTab("Cyan tab", p4);
tab.addMouseListener(new MouseAdapter() {
@Override
public void mousePressed(MouseEvent e) {
if (e.getClickCount() > 1) {
//Component c = tab.getComponentAt(new Point(e.getX(), e.getY()));
//TODO Find the right label and print it! :-)
int index = tab.indexAtLocation(e.getX(), e.getY());
if (index >= 0) {
Component c = tab.getComponentAt(index);
if (c instanceof JLabel) {
JLabel innerComponent = (JLabel) c;
System.out.println("Found:" + innerComponent.getText());
}
}
}
}
});
}
public static void main(final String[] args) throws Exception {
JFrame start = new SSCE2();
start.setDefaultCloseOperation(EXIT_ON_CLOSE);
start.setVisible(true);
start.setSize(new Dimension(450, 300));
}
}
刚遇到这个问题。将 SSCE 带在身边! :-)
我想通过 double-clicking 在其 "Title" 获取选项卡内容(组件)。
我给你这个 SSCE,在这种情况下,我想在 tab-menu-title 之前 double-clicking 收到 JLabel。 (绿色选项卡)我想接收 JLabel "I am label 2!"。
有代码:
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class SSCE extends JFrame {
private JTabbedPane tab;
public SSCE() {
tab = new JTabbedPane();
this.add(tab);
JLabel p = new JLabel("I am label 1!");
tab.addTab("Red tab",p );
JLabel p2 = new JLabel("I am label 2!");
tab.addTab("Green tab",p2 );
JLabel p3 = new JLabel("I am label 3!");
tab.addTab("Blue tab",p3 );
JLabel p4 = new JLabel("I am label 4!");
tab.addTab("Cyan tab", p4 );
tab.addMouseListener(new MouseAdapter(){
@Override
public void mousePressed(MouseEvent e) {
if ( e.getClickCount() > 1) {
Component c = tab.getComponentAt(new Point(e.getX(), e.getY()));
//TODO Find the right label and print it! :-)
JLabel innerComponent = (JLabel) c;
System.out.println("Found:" + innerComponent.getText());
}
}
});
}
public static void main(final String[] args) throws Exception {
SSCE start = new SSCE();
start.setDefaultCloseOperation(EXIT_ON_CLOSE);
start.setVisible(true);
start.setSize(new Dimension(450,300));
}
}
有办法做到吗?我尝试了很多东西。但没有运气:-( 非常感谢您的帮助!
我想做的是为 JTabbedPane 实现功能。所以当您 double-click 在 "Title" 时,它将打开一个对话框,其中包含您 [=33] 的选项卡内容=]编于。
我知道如何创建对话框等等。但是我不知道如何从标题上的 mouse-clicking 获取组件。
Component c = tab.getComponentAt(new Point(e.getX(), e.getY()));
您不想在您点击的地方获取组件。您想从所选选项卡中获取组件。
代码应该是:
//int index = tab.getSelectedTab(); // oops, this was a typo
int index = tab.getSelectedIndex();
Component c = tab.getComponentAt( index );
how to get the component just from the mouse-clicking on the title.
我猜你在找 JTabbedPane#indexAtLocation(int, int)?
Returns the tab index corresponding to the tab whose bounds intersect the specified location. Returns -1 if no tab intersects the location.
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class SSCE2 extends JFrame {
private JTabbedPane tab;
public SSCE2() {
tab = new JTabbedPane();
this.add(tab);
JLabel p = new JLabel("I am label 1!");
tab.addTab("Red tab", p);
JLabel p2 = new JLabel("I am label 2!");
tab.addTab("Green tab", p2);
JLabel p3 = new JLabel("I am label 3!");
tab.addTab("Blue tab", p3);
JLabel p4 = new JLabel("I am label 4!");
tab.addTab("Cyan tab", p4);
tab.addMouseListener(new MouseAdapter() {
@Override
public void mousePressed(MouseEvent e) {
if (e.getClickCount() > 1) {
//Component c = tab.getComponentAt(new Point(e.getX(), e.getY()));
//TODO Find the right label and print it! :-)
int index = tab.indexAtLocation(e.getX(), e.getY());
if (index >= 0) {
Component c = tab.getComponentAt(index);
if (c instanceof JLabel) {
JLabel innerComponent = (JLabel) c;
System.out.println("Found:" + innerComponent.getText());
}
}
}
}
});
}
public static void main(final String[] args) throws Exception {
JFrame start = new SSCE2();
start.setDefaultCloseOperation(EXIT_ON_CLOSE);
start.setVisible(true);
start.setSize(new Dimension(450, 300));
}
}