如何通过JButton,JTable或其他组件设置焦点?

时间:2017-06-22 06:41:14

标签: java swing jtable jpanel jbutton

我正在尝试使用多个Jpanel,其中包含我的JPanel上的按钮和JTable,每个包含一个JTable,一些JButton和一个JLabel,我使用一个单击并移动一个JTable但是当我的鼠标在另一个上时面板,我点击冻结的面板,直到我离开任何桌子。

我的听众:

import java.awt.*;
import java.awt.event.*;

import javax.swing.*;
import javax.swing.border.*;

public class ComponentMove extends MouseAdapter {

    private boolean move;
    private int relx;
    private JComponent component;
    private int rely;
    private Container container;

    public ComponentMove(Container container) {
        this.container=container;
    }

    @Override
    public void mousePressed(MouseEvent e) {
        if ( move ) {
            move=false; // arrêt du mouvement
            component.setBorder(null); // on  supprime la bordure noire
            component=null;
        }
        else {
            component = getComponent(e.getX(),e.getY()); // on mémorise le composant en déplacement
            if ( component!=null ) {
                container.setComponentZOrder(component,0); // place le composant le plus haut possible
                relx = e.getX()-component.getX(); // on mémorise la position relative
                rely = e.getY()-component.getY(); // on mémorise la position relative
                move=true; // démarrage du mouvement
                component.setBorder(BorderFactory.createLineBorder(Color.BLACK)); // on indique le composant sélectionné par une bordure noire
            }
        }
    }

    private JComponent getComponent(int x, int y) {
        // on recherche le premier composant qui correspond aux coordonnées de la souris
        for(Component component : container.getComponents()) {
            if ( component instanceof JComponent && component.getBounds().contains(x, y) ) {
                return (JComponent)component;
            }
        }
        return null;
    }

    @Override
    public void mouseMoved(MouseEvent e) {
        if ( move ) {
            // si on déplace
            component.setLocation(e.getX()-relx, e.getY()-rely);
        }
    }
}

这是包含表格和按钮的表格:

import javax.swing.*;
import java.awt.*;
import java.util.*;
import java.text.*;
import java.awt.Color;

public class PanneauPrincipal extends javax.swing.JPanel{

private Tables tables1;
private Tables tables2;
private Tables tables3;
private Tables tables4;
private Tables tables5;
private Tables tables6;
private ArrayList<JPanel> contient;
public Color background = new Color(171,214,224);
int c; //Set the placement of the panel
double percentage; //number of table
int posX;
int posY;

public PanneauPrincipal(int x, int y, String nomTable){
    c = 0;
    percentage = 0.5;
    Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
    posX = screenSize.width;
    posY = screenSize.height;
    contient = new ArrayList<JPanel>();



    this.setLayout(null);
    tables1 = new Tables("Table1");
    tables2 = new Tables("Table2");
    tables3 = new Tables("Table3");
    tables4 = new Tables("Table4");
    tables5 = new Tables("Table5");
    tables6 = new Tables("Table6");

    displayPanel(tables1);
    displayPanel(tables2);
    displayPanel(tables3);
    displayPanel(tables4);
    displayPanel(tables5);
    displayPanel(tables6);

    this.setBackground(background);
}

public void displayPanel(Tables tableToAdd){

    contient.add(new JPanel());
    contient.get((contient.size()-1)).setLayout(new BorderLayout());
    contient.get((contient.size()-1)).setBounds((int)(percentage * posX),(int) (percentage * posY),700,300);
    contient.get((contient.size()-1)).add(tableToAdd);
    contient.get((contient.size()-1)).setEnabled(false);
    this.add(contient.get((contient.size()-1)));

    if(percentage > 0.2){
        percentage -= 0.1;
    }

我把听众放在主要的应用程序:

import javax.swing.*;
import java.awt.*;
import javax.swing.border.*;

public class Application extends javax.swing.JFrame{
private TableauDeBord barreHaut;
private PanneauPrincipal milieu1;
private InfoBDD info;
public Color background = new Color(171,214,224);

public Application(String titre){
    super(titre); 
    Color infoColor = new Color(255,255,255);
    barreHaut = new TableauDeBord();
    milieu1 = new PanneauPrincipal(200,200, "Table 1");
    info = new InfoBDD();
    Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();

    info.setBackground(infoColor);

    ComponentMove listener1 = new ComponentMove(milieu1);
    milieu1.addMouseListener(listener1);
    milieu1.addMouseMotionListener(listener1);

    this.setLayout(new BorderLayout());
    this.add(barreHaut, BorderLayout.NORTH);
    this.add(milieu1);
    this.add(info, BorderLayout.WEST);
    this.setVisible(true);
    this.pack();
    this.setSize(screenSize.width,screenSize.height);
    this.setDefaultCloseOperation(EXIT_ON_CLOSE);
    }

}

0 个答案:

没有答案