Results 1 to 2 of 2
Like Tree1Likes
  • 1 Post By JosAH

Thread: Classification

  1. #1
    Jimmy1983 is offline Member
    Join Date
    Nov 2014
    Posts
    1
    Rep Power
    0

    Default Classification

    Hi dudes.
    I'm new in java, suppose thise 2 seperate class code.
    ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    1:
    import javax.swing.SwingUtilities;

    public class App {
    public static void main(String[] args) {

    SwingUtilities.invokeLater(new Runnable() {
    public void run() {

    new BasicFrame();
    }
    });
    }

    }
    ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    2.

    import java.awt.BorderLayout;
    import java.awt.Color;
    import java.awt.FlowLayout;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;

    import javax.swing.JButton;
    import javax.swing.JComboBox;
    import javax.swing.JFrame;
    import javax.swing.JLabel;
    import javax.swing.JPanel;
    import javax.swing.JScrollPane;
    import javax.swing.JTextArea;
    import javax.swing.JTextField;
    import javax.swing.JToolBar;

    public class BasicFrame extends JFrame {

    private static final long serialVersionUID = 1L;
    private JPanel p = new JPanel();
    private JButton bt = new JButton("Submit");
    private JTextField t = new JTextField("Hi", 10);
    private JTextArea ta = new JTextArea("Hello World!\nThis is next line", 10,30);
    private JLabel l = new JLabel("Write here:");
    private String arr[] = { "Pland", "Canada", "Germany","Japan" };
    private JComboBox<Object> cb = new JComboBox<Object>(arr);
    private JToolBar tlb =new JToolBar();
    private JButton tbt1=new JButton("Hello!");
    private JButton tbt2=new JButton("Welcome!");
    private JButton tbt3 =new JButton("Bye!");

    public BasicFrame() {
    super("This is basic frame 4.5");
    setSize(512, 384);
    setVisible(true);
    setResizable(true);
    setDefaultCloseOperation(DISPOSE_ON_CLOSE);
    add(p);
    tlb.add(tbt1);
    tlb.add(tbt2);
    tlb.add(tbt3);
    p.add(tlb);
    p.add(l);
    p.add(t);
    p.add(bt);
    bt.addActionListener(new ActionListener() {

    public void actionPerformed(ActionEvent arg0) {
    ta.append("Hello\n");
    }
    });
    p.add(cb);

    /* This line differs from 4.3 */p.add(new JScrollPane(ta));
    p.setBackground(Color.YELLOW);

    }
    }




    I want to seperate appropriates codes in the second class to many classes with my codes, not more other codes.
    May I use getter and setter for (p) JPanel if I make a class for toolbar? (Actually I have a problem to seperate toolbar in to its class).

  2. #2
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,785
    Blog Entries
    7
    Rep Power
    21

    Default Re: Classification

    Sure you can use setters and/or getters, aamof, it is preferred over direct member access, but be aware of the LoD (Law of Demeter); read more about it here: Law of Demeter - Wikipedia, the free encyclopedia.

    kind regards,

    Jos
    Jimmy1983 likes this.
    cenosillicaphobia: the fear for an empty beer glass

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •