0

I have a general question about java. Because I want to create StronaGlowna.java (class) where I have place all buttons, check box and other GUI component which I want to display in main class. The first question is this right way, it's correct ? or maybe is better way to do this thing. My code look this:

import java.awt.CardLayout;
import java.awt.Container;
import java.awt.Dimension;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.io.FileNotFoundException;
import java.io.IOException;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JPanel;

public class Main extends JFrame {

    private static final long serialVersionUID = -4575271483481196192L;

    Container pane;
    CardLayout layout;

    public Main() throws FileNotFoundException, IOException {

        layout = new CardLayout();
        setLayout(layout);

        pane = this.getContentPane();

        /*Page: Strona główna */
        JPanel newPanel = new JPanel();
        pane.add("New", newPanel);      
        JButton przycisk = new JButton("Przycisk");
        newPanel.add(przycisk);

...

In "pane.add("New", newPanel);" I want to display elements from:

package aplikacja.glowna;

import javax.swing.JButton;
import javax.swing.JPanel;

public class StronaGlowna {

    public void StronaGlownaDisplay() {
        JPanel newPanel = new JPanel();
        JButton przycisk2 = new JButton("Przycisk");
        newPanel.add(przycisk2);
    }

}

Can I import/display all class StronaGlowna in main() something like a include in PHP ? What do You thing about my idea, it's correct or I'm wrong ? Thanks for help and discussion.

Przemysław Suszek
  • 90
  • 1
  • 1
  • 11

2 Answers2

0

It sounds like the way Netbeans handling GUI. You may view the article in http://netbeans.org/kb/docs/java/quickstart-gui.html, it may help you understand how the GUI works since Netbeans can generate code for you. You can always import class and create object to access methods (often public methods) . I think it is not like a include in PHP. PHP include is like to directly include the source code, but jave is not.

Robin
  • 134
  • 9
0

First - Never, never, never, code in Main class. Call a method from it and then start your staff in another class. And, of course, don't extend it. And the constructor is neither a good idea. All of these are bad practices. Now, going into your problem, my suggestion is that you make StronaGlowna extend JPanel, and then obtain an instance of it through a public constructor, and use that instance as the parameter for the constructor of JScrollPane. That will make the scrollPane act as a 'screen' inside which you can see the contents of StronaGlowna, which is what I understand you're after.