Olhe para este código: package aulasLT51N.gui; import javax.swing.; import…
- User
Olhe para este código: package aulasLT51N.gui;
import javax.swing.; import javax.swing.border.TitledBorder; import java.awt.; import java.awt.event.ActionEvent; import java.awt.event.ActionListener;
/* Otimização do código implementado na aula, usando métodos auxiliares para instanciar os componentes. Colocar o TitledBorder nos componentes JTextField e JTextArea. Adicionar ActionListener nos botões. */ public class FirstFrameCopy extends JFrame {
// Métodos auxiliares que instanciam JTextField e JTextArea com TitledBorder private static JTextField getJTextField(int columns, String title) { JTextField tf = new JTextField(columns); tf.setBorder(new TitledBorder(title)); return tf; } private static JTextArea getJTextArea(int row, int columns, String title) { JTextArea ta = new JTextArea(row, columns); ta.setBorder(new TitledBorder(title)); ta.setLineWrap(true); return ta; } // Métodos auxiliar que instanciam JButton e adicionam o Listener private static JButton getJButton(String face, ActionListener listener) { JButton b = new JButton(face); b.addActionListener( listener ); return b; } //private final JTextField tf = new JTextField(20); //private final JTextArea ta = new JTextArea(10, 30); private final JTextField tf = getJTextField(20, "caixa de texto"); private final JTextArea ta = getJTextArea(10, 30, "area de texto"); public FirstFrameCopy() { super( "OLA"); setDefaultCloseOperation(EXIT_ON_CLOSE); Container cp = this.getContentPane(); // Numa JTextField quando é dado um Enter, é gerado // um ActionEvent. Quando se pressionar Enter no // JTextField,o texto é adicionado na JTextArea. tf.addActionListener( this::appendAction ); cp.add( tf, BorderLayout.NORTH ); cp.add( ta, BorderLayout.CENTER); JPanel south = new JPanel(new GridLayout(1, 2)); /* Instanciar os botões e adicionar o ActionListener */ JButton b1 = getJButton("B1", this::appendAction); south.add( b1 ); /* Como a referencia para o botão só é utilizada para adicionar*/ south.add( getJButton("B2", this::clearAction)); cp.add(south, BorderLayout.SOUTH); pack(); } private void clearAction(ActionEvent e ) { ta.setText(""); } private void appendAction(ActionEvent e){ ta.append( tf.getText()); }}
Agora olhe para este:
package aulasLT51N.gui;
import javax.swing.; import javax.swing.border.TitledBorder; import java.awt.; import java.awt.event.ActionEvent; import java.awt.event.ActionListener;
public class FirstFrame extends JFrame { private final JTextField tf = new JTextField(20); private final JTextArea ta = new JTextArea(10, 30);
public FirstFrame() { super( "OLA"); setDefaultCloseOperation(EXIT_ON_CLOSE); Container cp = this.getContentPane(); tf.setBorder(new TitledBorder("caixa de texto")); cp.add( tf, BorderLayout.NORTH ); ta.setBorder(new TitledBorder("area de texto")); cp.add( ta, BorderLayout.CENTER); ta.setLineWrap( true); JPanel south = new JPanel(new GridLayout(1, 2)); /* ActionListener l = new ActionListener() { @Override public void actionPerformed(ActionEvent e) { if ( e.getActionCommand().equals("1") ) ta.append( tf.getText() + '\n'); else ta.setText(""); } };*/ JButton b1 = new JButton("B1"); b1.setActionCommand("1"); b1.addActionListener(this::appendAction); //e -> ta.append( tf.getText() + '\n')); south.add( b1 ); JButton b2 = new JButton("B2"); south.add( b2); b2.addActionListener(this::clearAction); // e -> ta.setText("") ); cp.add(south, BorderLayout.SOUTH); pack(); } private void clearAction(ActionEvent e ) { ta.setText(""); } private void appendAction(ActionEvent e){ ta.append( tf.getText()); } - Assistant