Basic Concept of Swing
Basic Concept of Swing
Basic Concept of Swing
MainFrame
import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JTextArea;
public class MainFrame extends JFrame {
public MainFrame () {
setLayout(new BorderLayout());
toolbar.setStringListener(new StringListener(){
textPanel.appendText(text);
});
add(toolbar, BorderLayout.NORTH);
add(textPanel,BorderLayout.CENTER);
setVisible(true);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setSize(800, 600);
}
StringListener
Toolbar
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JPanel;
public Toolbar () {
helloButton.addActionListener(this);
goodbyeButton.addActionListener(this);
setLayout(new FlowLayout(FlowLayout.LEFT));
add(helloButton);
add(goodbyeButton);
this.textListener = listener;
@Override
if(clicked == helloButton) {
if(textListener != null){
textListener.textEmitted("Hello\n");
//textPanel.appendText("Hello\n");
if(textListener != null){
textListener.textEmitted("Goodby\n");
}
App
import javax.swing.JFrame;
import javax.swing.SwingUtilities;
SwingUtilities.invokeLater(new Runnable(){
@Override
new MainFrame();
//frame.setVisible(true);
//frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
//frame.setSize(800, 600);
});
}
TextPanel
import java.awt.BorderLayout;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
public TextPanel() {
textArea.append(text);