0

Как должно выглядеть Здравствуйте. Мне нужно в один слушатель ActionListener поместить работу сразу со всеми кнопками. Для каждой кнопки свой слушатель создать не подходит. Вот код:

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class P4_2 implements ActionListener 
{
  public static void main(String[] args) {
      SwingUtilities.invokeLater (new T2());}
  public void run() {  
    JPanel windowContent= new JPanel();
    windowContent.setLayout(null);
    JButton obr = new JButton("ШАБЛОН");
    JButton style = new JButton("СТИЛЬ");
    JButton p = new JButton("АБЗАЦ");
    JButton style1 = new JButton("ШРИФТ");
    JButton zyr = new JButton("ЖИРНЫЙ");
    JButton curs = new JButton("КУРСИВ");
    JButton cav = new JButton("<>");
    JButton cav1 = new JButton("<></>");
    JButton ink = new JButton("ССЫЛКА");
    JButton enter = new JButton("ПЕРЕНОС");
    JButton list = new JButton("СПИСОК N");
    JButton list1 = new JButton("СПИСОК *");
    JButton line = new JButton("ЛИНИЯ");     
    JTextArea derzy = new JTextArea(20,20);
    JTextArea text = new JTextArea(20,20);
    JScrollPane re = new JScrollPane(text);
    text.setLineWrap(true); 
    derzy.setLineWrap(true);
    windowContent.setLayout(null); 
    windowContent.add(obr);
    windowContent.add(style);
    windowContent.add(p);
    windowContent.add(style1);
    windowContent.add(derzy);
    windowContent.add(zyr);
    windowContent.add(curs);
    windowContent.add(text);
    windowContent.add(re);
    windowContent.add(cav);
    windowContent.add(cav1);
    windowContent.add(ink);
    windowContent.add(enter);
    windowContent.add(list);
    windowContent.add(list1);
    windowContent.add(line);
    obr.setBounds(5,5,100,40);
    style.setBounds(110,5,100,40);
    p.setBounds(215,5,100,40);
    style1.setBounds(320,5,100,40);
    zyr.setBounds(425,5,100,40);
    curs.setBounds(530,5,100,40);
    cav.setBounds(5,50,100,40);
    cav1.setBounds(110,50,100,40);
    ink.setBounds(215,50,100,40);
    enter.setBounds(320,50,100,40);
    list.setBounds(425,50,100,40);
    list1.setBounds(530,50,100,40);
    line.setBounds(5,95,100,40);
    derzy.setBounds(5,350,690,155);
    text.setBounds(700,5,500,500);
    re.setBounds(640,5,50,300);
    text.setLineWrap(true);
    derzy.setLineWrap(true);
    JFrame frame = new JFrame("Помощник по HTML");
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.setContentPane(windowContent);
    frame.setSize(1225,550);
    frame.setVisible(true);
    obr.addActionListener(this);
    style.addActionListener(this);
    p.addActionListener(this);
    style1.addActionListener(this);
    zyr.addActionListener(this);
    curs.addActionListener(this);
    cav.addActionListener(this);
    cav1.addActionListener(this);
    ink.addActionListener(this);
    enter.addActionListener(this);
    list.addActionListener(this);
    list1.addActionListener(this);
    line.addActionListener(this);   
  }
    public void actionPerformed(ActionEvent e) {    
if(e.getActionCommand().equals("ШАБЛОН")){ 
derzy.setText("<html>"+ "\n" + "<title>"+ "\n" + "</title>"+ "\n" + "<body>"+ "\n" + "</body>"+ "\n" + "</html>");}
if(e.getActionCommand().equals("СТИЛЬ")){
derzy.setText("<style type=\"text/css\">"+ "\n" + "p{font-size:16px; font-family:Comic Sans MS}"+ "\n" + "h1{font-size:20px; font-family:Comic Sans MS}"+ "\n" + "h2{font-size:18px; font-family:Comic Sans MS}"+ "\n" + "</style>");}
if(e.getActionCommand().equals("АБЗАЦ")){
derzy.setText("<p>  </p>");}
if(e.getActionCommand().equals("ШРИФТ")){
derzy.setText("<p style=\"font-size:16px; font-family:Comic Sans MS\">  </p>");}
if(e.getActionCommand().equals("ЖИРНЫЙ")){
derzy.setText("<b>  </b>");}
if(e.getActionCommand().equals("КУРСИВ")){
derzy.setText("<i>  </i>");}
if(e.getActionCommand().equals("<>")){
derzy.setText("< >");}
if(e.getActionCommand().equals("<></>")){
derzy.setText("< >  </ >");}
if(e.getActionCommand().equals("ССЫЛКА")){
derzy.setText("<a  title=\" \" href=\"https://www.google.com.ua/\">     </a>");}
if(e.getActionCommand().equals("ПЕРЕНОС")){
derzy.setText("<br/>");}
if(e.getActionCommand().equals("СПИСОК N")){
derzy.setText("<ol>"+ "\n" + "<li>  </li>"+ "\n" + "<li>    </li>"+ "\n" + "</ol>");}
if(e.getActionCommand().equals("СПИСОК *")){
derzy.setText("<ul>"+ "\n" + "<li>  </li>"+ "\n" + "<li>    </li>"+ "\n" + "</ul>");}
if(e.getActionCommand().equals("ЛИНИЯ")){
derzy.setText("<hr/>");}
    }   
}

Вот пример работающей программы, где на каждую кнопку свой слушатель:

import javax.swing.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
public class P3 implements ActionListener 
{
public static void main(String[] args) 
    {
    JPanel windowContent= new JPanel();
    windowContent.setLayout(null); 
    JTextArea derzy = new JTextArea(20,20);
    JTextArea text = new JTextArea(20,20);
    JScrollPane re = new JScrollPane(text);
    text.setLineWrap(true);
    derzy.setLineWrap(true);
    JButton obr = new JButton("ШАБЛОН");
    JButton style = new JButton("СТИЛЬ");
    JButton p = new JButton("АБЗАЦ");
    JButton style1 = new JButton("ШРИФТ");
    JButton zyr = new JButton("ЖИРНЫЙ");
    JButton curs = new JButton("КУРСИВ");
    JButton cav = new JButton("<>");
    JButton cav1 = new JButton("<></>");
    JButton ink = new JButton("ССЫЛКА");
    JButton enter = new JButton("ПЕРЕНОС");
    JButton list = new JButton("СПИСОК N");
    JButton list1 = new JButton("СПИСОК *");
    JButton line = new JButton("ЛИНИЯ");
// Добавляем компоненты на панель
    windowContent.add(obr);
    windowContent.add(style);
    windowContent.add(p);
    windowContent.add(style1);
    windowContent.add(derzy);
    windowContent.add(zyr);
    windowContent.add(curs);
    windowContent.add(text);
    windowContent.add(cav);
    windowContent.add(cav1);
    windowContent.add(ink);
    windowContent.add(enter);
    windowContent.add(list);
    windowContent.add(list1);
    windowContent.add(line);
    windowContent.add(re);
    re.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);
    re.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS);
    text.setCaretPosition(0);

    obr.setBounds(5,5,100,40);
    style.setBounds(110,5,100,40);
    p.setBounds(215,5,100,40);
    style1.setBounds(320,5,100,40);
    zyr.setBounds(425,5,100,40);
    curs.setBounds(530,5,100,40);
    cav.setBounds(5,50,100,40);
    cav1.setBounds(110,50,100,40);
    ink.setBounds(215,50,100,40);
    enter.setBounds(320,50,100,40);
    list.setBounds(425,50,100,40);
    list1.setBounds(530,50,100,40);
    line.setBounds(5,95,100,40);


    derzy.setBounds(5,350,690,155);
    text.setBounds(700,5,480,483);
    re.setBounds(700,5,497,500);
// Создаём фрейм и задаём для него панель 
    JFrame frame = new JFrame("Помощник по HTML"); 
    frame.setContentPane(windowContent);
// задаём и размер и делаем фрейм видимым
    frame.setSize(1240,550);
    frame.setVisible(true);
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    obr.addActionListener(new ActionListener() 
{
public void actionPerformed(ActionEvent e) 
    {
      derzy.setText("<html>"+ "\n" + "<title>"+ "\n" + "</title>"+ "\n" + "<body>"+ "\n" + "</body>"+ "\n" + "</html>");
    }
});
    style.addActionListener(new ActionListener() 
{
public void actionPerformed(ActionEvent e) 
    {
      derzy.setText("<style type=\"text/css\">"+ "\n" + "p{font-size:16px; font-family:Comic Sans MS}"+ "\n" + "h1{font-size:20px; font-family:Comic Sans MS}"+ "\n" + "h2{font-size:18px; font-family:Comic Sans MS}"+ "\n" + "</style>");
    }
});
    p.addActionListener(new ActionListener() 
{
public void actionPerformed(ActionEvent e) 
    {
      derzy.setText("<p>    </p>");
    }
}); 
    style1.addActionListener(new ActionListener() 
{
public void actionPerformed(ActionEvent e) 
    {
      derzy.setText("<p style=\"font-size:16px; font-family:Comic Sans MS\"> </p>");
    }
}); 
    zyr.addActionListener(new ActionListener() 
    {
    public void actionPerformed(ActionEvent e) 
        {
          derzy.setText("<b>    </b>");
        }
    }); 
    curs.addActionListener(new ActionListener() 
    {
    public void actionPerformed(ActionEvent e) 
        {
          derzy.setText("<i>    </i>");
        }
    });
    cav.addActionListener(new ActionListener() 
    {
    public void actionPerformed(ActionEvent e) 
        {
          derzy.setText("< >");
        }
    });
    cav1.addActionListener(new ActionListener() 
    {
    public void actionPerformed(ActionEvent e) 
        {
          derzy.setText("< >    </ >");
        }
    });
    ink.addActionListener(new ActionListener() 
    {
    public void actionPerformed(ActionEvent e) 
        {
          derzy.setText("<a  title=\"   \" href=\"https://www.google.com.ua/\">     </a>");
        }
    }); 
    enter.addActionListener(new ActionListener() 
    {
    public void actionPerformed(ActionEvent e) 
        {
          derzy.setText("<br/>");
        }
    });
    enter.addActionListener(new ActionListener() 
    {
    public void actionPerformed(ActionEvent e) 
        {
          derzy.setText("<br/>");
        }
    });
    list1.addActionListener(new ActionListener() 
    {
    public void actionPerformed(ActionEvent e) 
        {
          derzy.setText("<ul>"+ "\n" + "<li>    </li>"+ "\n" + "<li>    </li>"+ "\n" + "</ul>");
        }
    });
    list.addActionListener(new ActionListener() 
    {
    public void actionPerformed(ActionEvent e) 
        {
          derzy.setText("<ol>"+ "\n" + "<li>    </li>"+ "\n" + "<li>    </li>"+ "\n" + "</ol>");
        }
    });
    line.addActionListener(new ActionListener() 
    {
    public void actionPerformed(ActionEvent e) 
        {
          derzy.setText("<hr/>");
        }
    });





    }

public void actionPerformed(ActionEvent e) 
{        }

}
3
  • Не понятно, что Вы хотите? Что означает ` поместить работу сразу со всеми кнопками`? Что он должен делать? 5 янв 2018 в 21:07
  • То, что он должен делать, уже написано в коде. Я просто не понимаю, как создать слушатель, который будет получать результат e.getActionCommand().
    – Casy7
    5 янв 2018 в 21:44
  • почему не хотите создавать слушателя для каждой кнопки? Чем конкретно это не устраивает? 6 янв 2018 в 1:46

1 ответ 1

1

Используйте e.getSource.equals(переменная_JButton)

if(e.getSource().equals(obr)){ 
derzy.setText("<html>"+ "\n" + "<title>"+ "\n" + "</title>"+ "\n" + "<body>"+ "\n" + "</body>"+ "\n" + "</html>");}

Ваш ответ

By clicking “Отправить ответ”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Всё ещё ищете ответ? Посмотрите другие вопросы с метками или задайте свой вопрос.