Swing�̃X���b�h�����𗝉������FSwing��Java�ɋ����Ȃ�i6�j�i1/2 �y�[�W�j

��5��ł́AToDo���X�g�̕\����ToDo�̒lj����������܂����B��6��ł͂���Ȃ�@�\�̎����ƑO��G��Ă��Ȃ��_�ɂ‚��ĉ�����s���܂��B

» 2006�N09��23�� 00��00�� ���J
[���_���C������ЃV���v���N�X�E�e�N�m���W�[]

����쐬����GUI�A�v���P�[�V�����̎d�l

�@����͑�5��ō쐬�����A�v���P�[�V�����ɑ΂��Ĉȉ��̋@�\���������܂��B

  • �ꗗ��ToDo��ҏW����
  • �ꗗ��ToDo���폜����
}1@ToDoXg̃C[W �}1�@ToDo���X�g�̃C���[�W

ToDo���X�g�̎���

�v���W�F�N�g�̍쐬

�@����́uToDoList2�v�Ƃ������O�̃v���W�F�N�g���쐬���܂��i��1��́g�v���W�F�N�g�̍쐬�h���Q�l�ɂ��Ă��������j�B

SwingAppMain�N���X�̍쐬

�@�A�v���P�[�V�����̋N������ѓy��ƂȂ�t���[���̐�����S������N���X�uSwingAppMain�v���쐬���܂��B

package swing.sample;
import java.awt.BorderLayout;
import java.awt.Container;
import javax.swing.JComponent;
import javax.swing.JFrame;
import javax.swing.SwingUtilities;
public class SwingAppMain {
        /**
         * �A�v���P�[�V�����̋N��
         */
        public static void main(String[] args) {
                System.out.println("main : "
                        + SwingUtilities.isEventDispatchThread());
                SwingUtilities.invokeLater(new Runnable() {
                        public void run() {
                                createAndShowTodoList();
                        }
                });
        }
        /**
         * ToDo���X�g�̐����ƕ\�����s���܂��B
         */
        private static void createAndShowTodoList() {
                System.out.println("createAndShowTodoList :"
                                + SwingUtilities.
                                isEventDispatchThread());
                JFrame mainFrame = new JFrame("ToDo���X�g");
                mainFrame.setDefaultCloseOperation
                (JFrame.EXIT_ON_CLOSE);
                Container contentPane =
                mainFrame.getContentPane();
                // ToDo���X�g�𐶐�
                JComponent newContentPane = new ToDoListPane();
                contentPane.add(newContentPane,
                BorderLayout.CENTER);
                // Window�T�C�Y�𒲐�
                mainFrame.pack();
                // �\��
                mainFrame.setVisible(true);
          }
}

TodoListPane�N���X�̐���

�@����ToDoListPane�N���X���쐬���܂��B���̃N���X�́AToDo���X�g�̂��ׂĂ̋@�\��񋟂���N���X�ł��B

package swing.sample;
import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.DefaultListModel;
import javax.swing.JButton;
import javax.swing.JList;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextField;
import javax.swing.SwingUtilities;
import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener;
/**
 * ToDo���X�g
 */
public class ToDoListPane extends JPanel {
        private JList toDoList;
        private DefaultListModel toDoListModel;
        private JTextField toDoInputField;
        private JButton addButton;
        private JButton modifyButton;
        private JButton removeButton;
public ToDoListPane() {
            super(new BorderLayout());
            // �ꗗ�𐶐�
            toDoListModel = new DefaultListModel();
            toDoList = new JList(toDoListModel);
            JScrollPane listScrollPane = new JScrollPane(toDoList);
            // TODO���X�g�Ƀ��X�i��ݒ�
            toDoList.addListSelectionListener
            (new ToDoListSelectionHandler());
            // ToDo�lj��p�e�L�X�g�t�B�[���h�̐���
            toDoInputField = new JTextField();
            // �e�{�^���̐���
            JPanel buttonPanel = new JPanel();
            addButton = new JButton("�lj�");
            modifyButton = new JButton("�ҏW");
            removeButton = new JButton("�폜");
            // �{�^���Ƀ��X�i��ݒ�
            addButton.addActionListener
            (new AddActionHandler());
            modifyButton.addActionListener
            (new ModifyActionHandler());
            removeButton.addActionListener
            (new RemoveActionHandler());
            buttonPanel.add(addButton);
            buttonPanel.add(modifyButton);
            buttonPanel.add(removeButton);
            add(listScrollPane, BorderLayout.NORTH);
            add(toDoInputField, BorderLayout.CENTER);
            add(buttonPanel, BorderLayout.SOUTH);
        }
        /**
         * TODO���X�g�I���A�N�V�����̃n���h��
         */
        private class ToDoListSelectionHandler
        implements ListSelectionListener {
                public void valueChanged(ListSelectionEvent arg0) {
                        // TODO���X�g�̂ǂ̍s���I�������
                        ���Ȃ��ꍇ��A
                        // �����s���I������Ă���ꍇ�́A
                        // �������܂���
                        if (toDoList.getSelectedIndices().
                        length!=1){
                                    return;
                        }
                        toDoInputField.setText((String)toDoList.
                        getSelectedValue());
                }
        }
        /**
         * �lj��{�^���A�N�V�����̃n���h��
         */
        private class AddActionHandler implements ActionListener {
                public void actionPerformed(ActionEvent e) {
                        // �e�L�X�g�t�B�[���h�̓��e��
                         ���X�g���f���ɒlj�
                        toDoListModel.addElement
                        (toDoInputField.getText());
                }
        }
        /**
         * �ҏW�{�^���A�N�V�����̃n���h��
         */
        private class ModifyActionHandler
        implements ActionListener {
                public void actionPerformed(ActionEvent e) {
                        // �e�L�X�g�t�B�[���h�̓��e��
                        ���X�g���f����ҏW
                        // TODO���X�g�̂ǂ̍s��
                        �I������Ă��Ȃ��ꍇ��A
                        // �����s���I������Ă���ꍇ�́A
                        // �������܂���
                        if (toDoList.getSelectedIndices().
                        length != 1) {
                                return;
                        }
                        toDoListModel.set(toDoList.
                        getSelectedIndex(),
                        toDoInputField.getText());
                }
        }
        /**
         * �폜�{�^���A�N�V�����̃n���h��
         */
        private class RemoveActionHandler
        implements ActionListener {
                public void actionPerformed(ActionEvent e) {
                        // �e�L�X�g�t�B�[���h�̓��e��
                        ���X�g���f���ɒlj�
                        // TODO���X�g�̂ǂ̍s��
                        �I������Ă��Ȃ��ꍇ��A
                        // �����s���I������Ă���ꍇ�́A
                        // �������܂���
                        if (toDoList.getSelectedIndices().
                        length != 1) {
                                return;
                        }
                        setButtonsEnabled(false);
                        Thread removeThread = new
                        RemoveThread(toDoList.getSelectedIndex());
                        removeThread.start();
                }
        }
        /**
         * �{�^���̏�Ԃ��X�V���܂�
         */
        private void setButtonsEnabled(boolean enabled) {
                addButton.setEnabled(enabled);
                modifyButton.setEnabled(enabled);
                removeButton.setEnabled(enabled);
        }
        /**
         * �폜�������s���N���X
         */
        class RemoveThread extends Thread {
                int index;
                RemoveThread(int index) {
                        this.index = index;
                }
                public void run() {
                        // ���Ԃ̂����鏈�������s
                        doLongTask();
                        // �C�x���g�f�B�X�p�b�`�X���b�h��
                        �{�^����ύX
                        SwingUtilities.invokeLater(new Runnable() {
                                public void run() {
                                        toDoListModel.
                                        remove(index);
                                        setButtonsEnabled(true);
                                }
                        });
                }
        }
        /**
         *�@���Ԃ̂����鏈��
         */
        private void doLongTask() {
                try {
                        Thread.sleep(10000);
                } catch (InterruptedException ex) {
                }
        }

�A�v���P�[�V�����̎��s

�@�uSwingAppMain�v�N���X�����s���AToDo��lj����Ă݂܂��i�}2�ł́u20:00�ގЁv�̒lj������݂悤�Ƃ��Ă���j�B

}2 AvP[V̎s �}2 �A�v���P�[�V�����̎��s����

�@�Ȃ��A����͈ȉ��̂悤�ȋ@�\���������܂����B

(1)�m�ҏW�n�{�^�����N���b�N����ƁA�ꗗ�̑I������Ă���1�s�̕����񂪃e�L�X�g�t�B�[���h�ɕ\������Ă��镶����ɕύX�����

}3@}2̏ԂA(1)s �}3�@�}2�̏�Ԃ���A(1)�����s��������

(2)�m�폜�n�{�^�����N���b�N����ƁA�ꗗ�̑I������Ă���1�s���폜�����

}4@}2̏Ԃ(2)s �}4�@�}2�̏�Ԃ���(2)�����s��������
�@�@�@�@�@�@ 1|2 ���̃y�[�W��

Copyright © ITmedia, Inc. All Rights Reserved.

'; this.insertTarget = document.querySelector('#cmsBody .subscription') || document.querySelector('#cmsBody .inner'); }; BodyAdIMSWithCCE.prototype = Object.create(BodyAdContent.prototype); BodyAdIMSWithCCE.prototype.activate = function () { refreshGam('InArtSpecialLink'); } // global reference window.itm = itm; //entry point BodyAdEventBase.polyfill(); const bodyAdManager = BodyAdManager.getInstance(); bodyAdManager.addEventListener(BodyAdManager.EVENTS.READY, function (ev) { bodyAdManager.loadAdvertise(); }); bodyAdManager.init(); })();
�X�|���T�[����̂��m�点PR

���ڂ̃e�[�}

Microsoft  WindowsőO2025
AI for GWjAO
[R[h^m[R[h Zg by IT - ITGWjArWlX̒SŊ􂷂gD
Cloud Native Central by IT - XP[uȔ\͂gD
�V�X�e���J���m�E�n�E �y�����i�r�zPR
���Ȃ��ɂ������߂̋L��PR

RSS�ɂ‚���

�A�C�e�B���f�B�AID�ɂ‚���

���[���}�K�W���o�^

��IT�̃��[���}�K�W���́A �������A���ׂĖ����ł��B���Ѓ��[���}�K�W�������w�ǂ��������B