第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號(hào)安全,請(qǐng)及時(shí)綁定郵箱和手機(jī)立即綁定
已解決430363個(gè)問(wèn)題,去搜搜看,總會(huì)有你想問(wèn)的

訪(fǎng)問(wèn) JFrame 或 JPanel 中的子元素

訪(fǎng)問(wèn) JFrame 或 JPanel 中的子元素

慕無(wú)忌1623718 2022-05-25 16:28:26
我目前正在編寫(xiě)一個(gè)小程序。在程序中,我有一個(gè) JPanel,其中包含 400 個(gè)文本框,設(shè)置在 20 x 20 網(wǎng)格中。他程序的一部分致力于為變量分配顏色。然后當(dāng)用戶(hù)單擊其中一個(gè)文本框時(shí),背景顏色會(huì)發(fā)生變化。這是在 Netbeans 中編寫(xiě)的,所有可視項(xiàng)目都使用設(shè)計(jì)管理器設(shè)置(加上更改布局管理器以適應(yīng))。我對(duì)設(shè)計(jì)、將顏色分配給變量甚至編寫(xiě)使用鼠標(biāo)單擊事件處理程序?qū)⒈尘邦伾O(shè)置為顏色變量的單獨(dú)代碼都沒(méi)有問(wèn)題。問(wèn)題的原因是目前,我需要為所有 400 個(gè)文本框編寫(xiě)代碼才能使其工作。有沒(méi)有辦法知道單擊了哪個(gè)文本框并分配顏色,而無(wú)需為所有 400 個(gè)文本框編寫(xiě)代碼,可能通過(guò)父級(jí)(JPanel)?
查看完整描述

2 回答

?
慕森卡

TA貢獻(xiàn)1806條經(jīng)驗(yàn) 獲得超8個(gè)贊

很簡(jiǎn)單:使用一個(gè) FocusListener,一個(gè)添加到每個(gè) JTextField。例如,如果您有這樣的 JTextFields:


private JTextField[][] fields = new JTextField[ROW_COUNT][ROW_COUNT];

假設(shè)你有一個(gè)像這樣的 FocusListener:


private class MyFocus implements FocusListener {

    @Override

    public void focusLost(FocusEvent e) {

        // get JTextField that lost focus

        JTextField textField = (JTextField) e.getSource();


        // set color back to white

        textField.setBackground(INACTIVE_COLOR);

    }


    @Override

    public void focusGained(FocusEvent e) {

        // get JTextField that is gaining focus

        JTextField textField = (JTextField) e.getSource();


        // set color to the active background

        textField.setBackground(ACTIVE_COLOR);

    }

}

您可以創(chuàng)建和添加您的偵聽(tīng)器


    FocusListener focusListener = new MyFocus();        

    setLayout(new GridLayout(ROW_COUNT, ROW_COUNT, 1, 1));

    for (int row = 0; row < fields.length; row++) {

        for (int col = 0; col < fields[row].length; col++) {

            JTextField field = new JTextField(COLS);

            field.addFocusListener(focusListener);

            add(field);

        }

    }

整個(gè)可測(cè)試的東西:


import java.awt.Color;

import java.awt.GridLayout;

import java.awt.event.FocusEvent;

import java.awt.event.FocusListener;

import javax.swing.*;


@SuppressWarnings("serial")

public class FocusExample extends JPanel {

    private static final int ROW_COUNT = 20;

    private static final int COLS = 5;

    protected static final Color ACTIVE_COLOR = Color.PINK;

    protected static final Color INACTIVE_COLOR = Color.WHITE;

    private JTextField[][] fields = new JTextField[ROW_COUNT][ROW_COUNT];


    public FocusExample() {

        FocusListener focusListener = new MyFocus();        

        setLayout(new GridLayout(ROW_COUNT, ROW_COUNT, 1, 1));

        for (int row = 0; row < fields.length; row++) {

            for (int col = 0; col < fields[row].length; col++) {

                JTextField field = new JTextField(COLS);

                field.addFocusListener(focusListener);

                add(field);

            }

        }

    }


    private class MyFocus implements FocusListener {

        @Override

        public void focusLost(FocusEvent e) {

            // get JTextField that lost focus

            JTextField textField = (JTextField) e.getSource();


            // set color back to white

            textField.setBackground(INACTIVE_COLOR);

        }


        @Override

        public void focusGained(FocusEvent e) {

            // get JTextField that is gaining focus

            JTextField textField = (JTextField) e.getSource();


            // set color to the active background

            textField.setBackground(ACTIVE_COLOR);

        }

    }


    private static void createAndShowGui() {

        FocusExample mainPanel = new FocusExample();


        JFrame frame = new JFrame("FocusExample");

        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        frame.getContentPane().add(mainPanel);

        frame.pack();

        frame.setLocationRelativeTo(null);

        frame.setVisible(true);

    }


    public static void main(String[] args) {

        SwingUtilities.invokeLater(() -> createAndShowGui());

    }

}


查看完整回答
反對(duì) 回復(fù) 2022-05-25
?
長(zhǎng)風(fēng)秋雁

TA貢獻(xiàn)1757條經(jīng)驗(yàn) 獲得超7個(gè)贊

你不需要面板。只需這樣做:


聲明這個(gè)鼠標(biāo)適配器:


MouseAdapter ma = new MouseAdapter() {

            @Override

            public void mouseClicked(MouseEvent e) {

                JTextField tf = (JTextField) e.getSource();

                tf.setBackground(Color.BLACK);

            }

        };

在創(chuàng)建文本字段的循環(huán)內(nèi)執(zhí)行此操作:


textField.addMouseListener(ma);

MouseEvent 知道單擊了哪個(gè) JTextField,因此您可以訪(fǎng)問(wèn)它并更改它的顏色。將此鼠標(biāo)偵聽(tīng)器添加到每個(gè)文本字段,這應(yīng)該可以工作。


查看完整回答
反對(duì) 回復(fù) 2022-05-25
  • 2 回答
  • 0 關(guān)注
  • 186 瀏覽
慕課專(zhuān)欄
更多

添加回答

舉報(bào)

0/150
提交
取消
微信客服

購(gòu)課補(bǔ)貼
聯(lián)系客服咨詢(xún)優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動(dòng)學(xué)習(xí)伙伴

公眾號(hào)

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號(hào)