在Swing中创建类似元素时,如何避免代码重复?

时间:2018-06-02 21:09:04

标签: java swing

我为swing的小文本编辑器制作了代码。我有很多类似的组件做类似的操作。类似的代码重复多次。例如,对于不同的字体大小,下面的代码会重复多次:

pkt8 = new JMenuItem("8 pts");
pkt8.addActionListener(this);
fontsize.add(pkt8);
...
if (e.getSource() == pkt8) {
  textArea.setFont(new Font("monospaced", Font.PLAIN, 8))
...

如何更改代码以避免重复?

以下是完整代码:

package zad6;

import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.PrintWriter;
import java.io.StringReader;
import java.io.Writer;
import java.nio.file.AccessDeniedException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;
import java.util.StringTokenizer;

import javax.swing.*;
import javax.swing.event.MenuEvent;
import javax.swing.event.MenuKeyEvent;
import javax.swing.event.MenuKeyListener;
import javax.swing.event.MenuListener;

public class DrawSwing {
public static void main(String[] args) {
    EventQueue.invokeLater(new Runnable() {

        @Override
        public void run() {
            new Ramka();
        }
    });
}
}

class Ramka implements ActionListener {

// kolory 1-blue,2-yellow,3-orange,4-red,5-white,6-black,7-green
JMenuItem c1;
JMenuItem c2;
JMenuItem c3;
JMenuItem c4;
JMenuItem c5;
JMenuItem c6;
JMenuItem c7;
JMenuItem c1f;
JMenuItem c2f;
JMenuItem c3f;
JMenuItem c4f;
JMenuItem c5f;
JMenuItem c6f;
JMenuItem c7f;
JMenuItem pkt8;
JMenuItem pkt10;
JMenuItem pkt12;
JMenuItem pkt14;
JMenuItem pkt16;
JMenuItem pkt18;
JMenuItem pkt20;
JMenuItem pkt22;
JMenuItem pkt24;
JTextArea textArea;
JMenuItem szkola;
JMenuItem praca;
JMenuItem dom;
BufferedReader br;
String line;
FileDialog fd;
static String path;

public Ramka() {
    JFrame jf = new JFrame();

    jf.setTitle("Prosty edytor - bez tytulu");
    jf.pack();
    jf.setSize(500, 500);
    jf.setVisible(true);
    jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    final JMenuBar menuBar = new JMenuBar();
    jf.setJMenuBar(menuBar);
    textArea = new JTextArea(5, 20);
    textArea.setEditable(true);
    jf.add(textArea, BorderLayout.CENTER);
    JScrollPane scrollPane = new JScrollPane(textArea);
    jf.add(scrollPane, BorderLayout.CENTER);

    // Główne menu
    // File menu
    JMenu fileMenu = new JMenu("File");
    menuBar.add(fileMenu);
    // Open
    JMenuItem open = new JMenuItem("Open", new ImageIcon("src/src/if_simpline_4_2305586.png"));
    open.setMnemonic(KeyEvent.VK_CONTROL + KeyEvent.VK_O);
    open.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_O, ActionEvent.CTRL_MASK));
    fileMenu.add(open);
    open.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent ae) {
            fd = new FileDialog(jf, "Open", FileDialog.LOAD);
            textArea.setText("");
            fd.setVisible(true);
            String katalog = fd.getDirectory();
            String plik = fd.getFile();
            System.out.println(fd.getFile());
            path = katalog + plik;
            jf.setTitle("Prosty edytor - " + path);
            try {
                br = new BufferedReader(new FileReader(path));
            } catch (FileNotFoundException e1) {
                // TODO Auto-generated catch block
                e1.printStackTrace();
            }

            try {
                line = br.readLine();
            } catch (IOException e1) {
                // TODO Auto-generated catch block
                e1.printStackTrace();
            }
            while (line != null) {
                textArea.append(line + "\n");
                try {
                    line = br.readLine();
                } catch (IOException e1) {
                    // TODO Auto-generated catch block
                    e1.printStackTrace();
                }
            }
        }
    });
    fileMenu.addSeparator();
    // Save
    JMenuItem save = new JMenuItem("Save", new ImageIcon("src/src/if_simpline_53_2305609.png"));
    save.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_S, ActionEvent.CTRL_MASK));
    fileMenu.add(save);
    save.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent ae) {
            File f2 = new File(path);
            jf.setTitle("Prosty edytor - " + path);
            try {
                BufferedWriter out = new BufferedWriter(new FileWriter(f2));
                BufferedReader input = new BufferedReader(new StringReader(textArea.getText()));
                String line;

                while ((line = input.readLine()) != null)
                    out.write(line);
                out.newLine();
                out.close();

            } catch (AccessDeniedException e1) {
                e1.printStackTrace();
            } catch (IOException e1) {
                e1.printStackTrace();
            }
        }
    });
    fileMenu.addSeparator();
    // Saveas
    JMenuItem saveas = new JMenuItem("Save As...", new ImageIcon("src/src/if_simpline_53_2305609.png"));
    fileMenu.add(saveas);
    saveas.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_A, ActionEvent.CTRL_MASK));
    saveas.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent ae) {
            fd = new FileDialog(jf, "Save As..", FileDialog.SAVE);
            fd.setVisible(true);
            String katalog = fd.getDirectory();
            String plik = fd.getFile();
            path = katalog + plik;
            File f2 = new File(path);
            jf.setTitle("Prosty edytor - " + path);
            try {
                BufferedWriter out = new BufferedWriter(new FileWriter(f2));
                BufferedReader input = new BufferedReader(new StringReader(textArea.getText()));
                String line;

                while ((line = input.readLine()) != null)
                    out.write(line);
                out.newLine();
                out.close();

            } catch (AccessDeniedException e1) {
                e1.printStackTrace();
            } catch (IOException e1) {
                e1.printStackTrace();
            }
        }
    });
    JSeparator sep = new JSeparator(SwingConstants.HORIZONTAL);
    sep.setBackground(Color.RED);
    fileMenu.add(sep);
    // Exit
    JMenuItem exit = new JMenuItem("Exit", new ImageIcon("src/src/if_simpline_43_2305619.png"));
    exit.setMnemonic(KeyEvent.VK_CONTROL + KeyEvent.VK_X);
    exit.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_X, ActionEvent.CTRL_MASK));
    fileMenu.add(exit);
    exit.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent ae) {
            // Exit the program
            System.exit(0);
        }
    });
    fileMenu.addSeparator();
    // Edit menu
    JMenu editMenu = new JMenu("Edit");
    menuBar.add(editMenu);
    // Sub menu
    JMenu adresMenu = new JMenu("Adresy");
    editMenu.add(adresMenu);

    // Pod menu
    praca = new JMenuItem("Praca");
    praca.addActionListener(this);
    praca.setBorder(BorderFactory.createLineBorder(Color.black));
    adresMenu.add(praca);

    praca.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_P, ActionEvent.SHIFT_MASK + ActionEvent.CTRL_MASK));
    adresMenu.addSeparator();
    szkola = new JMenuItem("Szkoła");
    szkola.addActionListener(this);

    szkola.setBorder(BorderFactory.createLineBorder(Color.black));
    adresMenu.add(szkola);
    szkola.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_S, ActionEvent.SHIFT_MASK + ActionEvent.CTRL_MASK));
    adresMenu.addSeparator();
    dom = new JMenuItem("Dom");
    dom.addActionListener(this);
    dom.setBorder(BorderFactory.createLineBorder(Color.black));
    dom.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_D, ActionEvent.SHIFT_MASK + ActionEvent.CTRL_MASK));
    adresMenu.add(dom);

    // Options menu
    JMenu optionsMenu = new JMenu("Options");
    menuBar.add(optionsMenu);

    // Sub menu
    JMenu foreground = new JMenu("Foreground");
    optionsMenu.add(foreground);
    // Pod menu foreground
    c1f = new JMenuItem("Blue", new ImageIcon("src/src/blue.png"));
    foreground.add(c1f);
    c1f.addActionListener(this);
    c2f = new JMenuItem("Yellow", new ImageIcon("src/src/yellow.png"));
    foreground.add(c2f);
    c2f.addActionListener(this);
    c3f = new JMenuItem("Orange", new ImageIcon("src/src/orange.png"));
    foreground.add(c3f);
    c3f.addActionListener(this);
    c4f = new JMenuItem("Red", new ImageIcon("src/src/red.png"));
    foreground.add(c4f);
    c4f.addActionListener(this);
    c5f = new JMenuItem("White", new ImageIcon("src/src/white.png"));
    foreground.add(c5f);
    c5f.addActionListener(this);
    c6f = new JMenuItem("Black", new ImageIcon("src/src/black.png"));
    foreground.add(c6f);
    c6f.addActionListener(this);
    c7f = new JMenuItem("Green", new ImageIcon("src/src/green.png"));
    foreground.add(c7f);
    c7f.addActionListener(this);

    // Background
    JMenu background = new JMenu("Background");
    optionsMenu.add(background);
    JMenu fontsize = new JMenu("Font size");
    optionsMenu.add(fontsize);
    // Pod menu background
    c1 = new JMenuItem("Blue", new ImageIcon("src/src/blue.png"));
    background.add(c1);
    c1.addActionListener(this);
    c2 = new JMenuItem("Yellow", new ImageIcon("src/src/yellow.png"));
    background.add(c2);
    c2.addActionListener(this);
    c3 = new JMenuItem("Orange", new ImageIcon("src/src/orange.png"));
    background.add(c3);
    c3.addActionListener(this);
    c4 = new JMenuItem("Red", new ImageIcon("src/src/red.png"));
    background.add(c4);
    c4.addActionListener(this);
    c5 = new JMenuItem("White", new ImageIcon("src/src/white.png"));
    background.add(c5);
    c5.addActionListener(this);
    c6 = new JMenuItem("Black", new ImageIcon("src/src/black.png"));
    background.add(c6);
    c6.addActionListener(this);
    c7 = new JMenuItem("Green", new ImageIcon("src/src/green.png"));
    background.add(c7);
    c7.addActionListener(this);

    // Pod menu font size

    pkt8 = new JMenuItem("8 pts");
    pkt8.addActionListener(this);
    fontsize.add(pkt8);
    pkt10 = new JMenuItem("10 pts");
    fontsize.add(pkt10);
    pkt10.addActionListener(this);
    pkt12 = new JMenuItem("12 pts");
    fontsize.add(pkt12);
    pkt12.addActionListener(this);
    pkt14 = new JMenuItem("14 pts");
    fontsize.add(pkt14);
    pkt14.addActionListener(this);
    pkt16 = new JMenuItem("16 pts");
    fontsize.add(pkt16);
    pkt16.addActionListener(this);
    pkt18 = new JMenuItem("18 pts");
    fontsize.add(pkt18);
    pkt18.addActionListener(this);
    pkt20 = new JMenuItem("20 pts");
    fontsize.add(pkt20);
    pkt20.addActionListener(this);
    pkt22 = new JMenuItem("22 pts");
    fontsize.add(pkt22);
    pkt22.addActionListener(this);
    pkt24 = new JMenuItem("24 pts");
    fontsize.add(pkt24);
    pkt24.addActionListener(this);

}

String tab[] = { " adres praca", " adres szkola", " adres dom" };

@Override
public void actionPerformed(ActionEvent e) {

    if (e.getSource() == pkt8) {
        textArea.setFont(new Font("monospaced", Font.PLAIN, 8));
    } else if (e.getSource() == pkt10) {
        textArea.setFont(new Font("monospaced", Font.PLAIN, 10));
    } else if (e.getSource() == pkt12) {
        textArea.setFont(new Font("monospaced", Font.PLAIN, 12));
    } else if (e.getSource() == pkt14) {
        textArea.setFont(new Font("monospaced", Font.PLAIN, 14));
    } else if (e.getSource() == pkt16) {
        textArea.setFont(new Font("monospaced", Font.PLAIN, 16));
    } else if (e.getSource() == pkt18) {
        textArea.setFont(new Font("monospaced", Font.PLAIN, 18));
    } else if (e.getSource() == pkt20) {
        textArea.setFont(new Font("monospaced", Font.PLAIN, 20));
    } else if (e.getSource() == pkt22) {
        textArea.setFont(new Font("monospaced", Font.PLAIN, 22));
    } else if (e.getSource() == pkt24) {
        textArea.setFont(new Font("monospaced", Font.PLAIN, 24));
    } else if (e.getSource() == c1) {
        textArea.setBackground(Color.BLUE);
    } else if (e.getSource() == c2) {
        textArea.setBackground(Color.YELLOW);
    } else if (e.getSource() == c3) {
        textArea.setBackground(Color.ORANGE);
    } else if (e.getSource() == c4) {
        textArea.setBackground(Color.RED);
    } else if (e.getSource() == c5) {
        textArea.setBackground(Color.WHITE);
    } else if (e.getSource() == c6) {
        textArea.setBackground(Color.BLACK);
    } else if (e.getSource() == c7) {
        textArea.setForeground(Color.GREEN);
    } else if (e.getSource() == c1f) {
        textArea.setForeground(Color.BLUE);
    } else if (e.getSource() == c2f) {
        textArea.setForeground(Color.YELLOW);
    } else if (e.getSource() == c3f) {
        textArea.setForeground(Color.ORANGE);
    } else if (e.getSource() == c4f) {
        textArea.setForeground(Color.RED);
    } else if (e.getSource() == c5f) {
        textArea.setForeground(Color.WHITE);
    } else if (e.getSource() == c6f) {
        textArea.setForeground(Color.BLACK);
    } else if (e.getSource() == c7f) {
        textArea.setForeground(Color.GREEN);
    } else if (e.getSource() == praca) {
        textArea.append(tab[0]);
    } else if (e.getSource() == szkola) {
        textArea.append(tab[1]);
    } else if (e.getSource() == dom) {
        textArea.append(tab[2]);
    }
}
}

评论代码:

public ZmianaTla(Color color, String label, JComponent target, 
ImageIcon ico) {
        super(label);
        this.color=color;
        this.target=target;
        this.ico=ico;
        }


background.add(new JMenuItem(new ZmianaTla(Color.YELLOW, "Yellow", 
textArea),new ImageIcon("src/src/yellow.png")); 

1 个答案:

答案 0 :(得分:1)

很难使基本上更短。我想你的观点是如何减少锅炉板,重复类似的代码。解决方案之一可以遵循。

实施更改字体的操作:

public class ChangeFontAction extends AbstractAction {

    private int fontSize;

    private JTextComponent target;

    public ChangeFontAction(int fontSize, String label, JTextComponent target) {
        super(label);
        this.fontSize = fontSize;
        this.target = target;
    }

    public void actionPerformed(ActionEvent e) {
        target.setFont(new Font(Font.MONOSPACED, Font.PLAIN, fontSize));
    }

}

在此操作中更改字体:

target.setFont(new Font("monospaced", Font.PLAIN, fontSize))

按如下方式创建菜单项:

fontsize.add(new JMenuItem(new ChangeFontAction(8, "8 pts", textArea)));
fontsize.add(new JMenuItem(new ChangeFontAction(10, "10 pts", textArea)));

你现在不需要任何听众。

总而言之,代码可能并不少,但更容易理解。如果你想改变按钮/菜单点击发生的事情,你不需要多次重复,你将在一个地方进行。

以类似的方式,您可以创建一个动作类来改变颜色。