[Java] 보조창을 활용하여 글씨 추가하기

Jeini·2022년 12월 24일
0

📌 Code list

목록 보기
41/55
post-thumbnail

✏️ InputDialog_보조창


mport java.awt.BorderLayout;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTextField;

public class InputDialog extends JDialog {
    private JButton btnAdd;
    private JButton btnClose;
    private JTextField tfInput;

    private DataSet owner;
    public InputDialog(DataSet owner) {
        super(owner, "input", true);
        this.owner = owner;
        btnAdd = new JButton("Add");
        btnClose = new JButton("Close");
        tfInput = new JTextField(20);

        JPanel pnlSouth = new JPanel(new GridLayout(1, 2));
        pnlSouth.add(btnAdd);
        pnlSouth.add(btnClose);

        JPanel pnlCenter = new JPanel();
        pnlCenter.add(tfInput);

        add(pnlSouth, BorderLayout.SOUTH);
        add(pnlCenter, BorderLayout.CENTER);

        ActionListener aListener = new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent ae) {
                if(ae.getSource() == btnClose) {
                    dispose();
                } else {
                    add();
                }
            }
        };
        btnAdd.addActionListener(aListener);
        btnClose.addActionListener(aListener);

        setSize(250, 100);
        setLocationRelativeTo(owner);
        setDefaultCloseOperation(DISPOSE_ON_CLOSE);
        setVisible(true);
    }
    private void add() {
        String word = tfInput.getText();
        if(owner.add(word)) {
            dispose();
        } else {
            JOptionPane.showMessageDialog(
                    this,
                    "이미 존재하는 단어입니다.",
                    "경고",
                    JOptionPane.WARNING_MESSAGE
            );
            tfInput.requestFocus();
            tfInput.selectAll();
        }
    }
}

✏️ DataSet_본창

import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;

public class DataSet extends JFrame {
    private JTextArea taList;
    private JButton btnAdd;
    private ArrayList<String> words;

    public DataSet() {
        words = new ArrayList<String>();

        taList = new JTextArea();
        taList.setEditable(false);
        btnAdd = new JButton("Add");

        add(new JScrollPane(taList), BorderLayout.CENTER);
        add(btnAdd, BorderLayout.SOUTH);

        btnAdd.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent ae) {
                new InputDialog(DataSet.this);
            }
        });

        setTitle("DataSet");
        setSize(300, 400);
        setLocationRelativeTo(null);
        setDefaultCloseOperation(EXIT_ON_CLOSE);
        setVisible(true);
    }

    public boolean add(String word) {
        boolean flag = false;
        if(!words.contains(word)) {
            words.add(word);
            taList.append(word + "\n");
            flag = true;
        }
        return flag;
    }

    public static void main(String[] args) {
        new DataSet();
    }
}



profile
Fill in my own colorful colors🎨

0개의 댓글