Results 1 to 9 of 9
Like Tree1Likes
  • 1 Post By PhHein

Thread: How to write code smaller

  1. #1
    mr.suchy is offline Member
    Join Date
    Feb 2013
    Posts
    17
    Rep Power
    0

    Default How to write code smaller

    Hi

    I have strange question so maybe topic should be different. Sorry for that. When I do something in php I create index.php or page.php. In Java I have class ex. main.java and so on. I want create a GUI in ex. gui.java and the import everything to main.java. How can I do something like this. Right now I write everything in main class and I dont know what to read, know or search to the internet to find the solution.

    Best regards

  2. #2
    kalata is offline Member
    Join Date
    Aug 2011
    Location
    Bulgaria
    Posts
    29
    Rep Power
    0

    Default Re: How to write code smaller

    Hi,
    I think this might answer you question or at least be useful to some point -> Lesson: Packages (The Java™ Tutorials > Learning the Java Language)

    BR,
    Kalin
    "One of the greatest ways to learn something is trying to teach it to someone else."

  3. #3
    allaudin's Avatar
    allaudin is offline Senior Member
    Join Date
    Apr 2013
    Posts
    271
    Rep Power
    0

    Default Re: How to write code smaller

    Have you ever used classes ? :)

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

    Default Re: How to write code smaller

    @Qazi (allaudin): the avatar you're using now is also used by PhHein; did you copy it from him?

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  5. #5
    mr.suchy is offline Member
    Join Date
    Feb 2013
    Posts
    17
    Rep Power
    0

    Default Re: How to write code smaller

    Maybe I say something wrong. How do you code, do program beacause I want create JCheckBox or another filed in checkbox.java and then display it in another class. Right now I have this:

    Read properties from file.

    Java Code:
    package aplikacja.glowna;
    
    import java.io.FileInputStream;
    import java.io.FileNotFoundException;
    import java.io.IOException;
    import java.util.Properties;
    
    public class Konfiguracja {
    
    	private String password;
    	private String pozycja1;
    	private String pozycja2;
    
    	public void testPassword() throws FileNotFoundException, IOException {
    
    		Properties props = new Properties();
    		FileInputStream plik = new FileInputStream("aplikacja.properties");
    		props.load(plik);
    		password = props.getProperty("jdbc.password");
    		pozycja1 = props.getProperty("pizza.pozycja1");
    		pozycja2 = props.getProperty("pizza.pozycja2");
    	}
    
    	public String getPassword() {
    		return password;
    	}
    	
    	public String getPozycja1() {
    		return pozycja1;
    	}
    	
    	public String getPozycja2() {
    		return pozycja2;
    	}
    }

    Get values from file and display
    Java Code:
    package aplikacja.glowna;
    
    import java.io.FileNotFoundException;
    import java.io.IOException;
    
    import javax.swing.JLabel;
    
    public class OProgramie {
    
    	public Konfiguracja getKonfiguracja;
    	public JLabel autorLabel;
    
    	public OProgramie() throws FileNotFoundException, IOException {
    		getKonfiguracja = new Konfiguracja();
    		getKonfiguracja.testPassword();
    		
    		setAutorLabel(new JLabel(getKonfiguracja.getPassword()));
    	}
    
    	/**
    	 * @return the autorLabel
    	 */
    	public JLabel getAutorLabel() {
    		return autorLabel;
    	}
    
    	/**
    	 * @param autorLabel
    	 *            the autorLabel to set
    	 */
    	public void setAutorLabel(JLabel autorLabel) {
    		this.autorLabel = autorLabel;
    	}
    
    }
    Display frame, GUI.
    Java Code:
    package aplikacja.glowna;
    
    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.JCheckBox;
    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();
    
    		JPanel newPanel = new JPanel();
    		pane.add("New", newPanel);
    
    		JPanel savePanel = new JPanel();
    		pane.add("Load", savePanel);
    
    		// Menu
    		JMenuBar menuBar = new JMenuBar();
    
    		// Menu - elementy główne
    		JMenu menuPlik = new JMenu();
    		menuPlik.setText("Plik");
    		JMenu menuPomoc = new JMenu();
    		menuPomoc.setText("Pomoc");
    
    		// Menu - Poszczególne elementy menu
    		JMenuItem menuPlikNew = new JMenuItem();
    		menuPlikNew.setText("Opcja 1");
    		JMenuItem menuPlikLoad = new JMenuItem();
    		menuPlikLoad.setText("Opcja 2");
    		JMenuItem menuPlikExit = new JMenuItem();
    		menuPlikExit.setText("Zakończ");
    		JMenuItem menuPomocOProgramie = new JMenuItem();
    		menuPomocOProgramie.setText("O programie");
    
    		// Dodanie poszczeglnych elementów do GUI
    		menuPlik.add(menuPlikNew);
    		menuPlik.add(menuPlikLoad);
    		menuPlik.add(menuPlikExit);
    		menuBar.add(menuPlik);
    		menuBar.add(menuPomoc);
    		menuPomoc.add(menuPomocOProgramie);
    
    		// GUI
    
    		setTitle("Aplikacja v0.1");
    		setJMenuBar(menuBar);
    		setSize(new Dimension(640, 480));
    
    		// Akcje - wywoływanie
    		menuPlikExit.addActionListener(new ActionListener() {
    			public void actionPerformed(ActionEvent e) {
    				Main.this.windowClosed();
    			}
    		});
    
    		menuPlikNew.addActionListener(new ActionListener() {
    			public void actionPerformed(ActionEvent e) {
    				layout.show(pane, "New");
    			}
    		});
    		menuPlikLoad.addActionListener(new ActionListener() {
    			public void actionPerformed(ActionEvent e) {
    				layout.show(pane, "Load");
    			}
    		});
    		menuPomocOProgramie.addActionListener(new ActionListener() {
    			public void actionPerformed(ActionEvent e) {
    				layout.show(pane, "OProgramie");
    			}
    		});
    
    		// Strona główna
    		JButton przycisk = new JButton("Zamówienie");
    		newPanel.add(przycisk);
    		
    		JCheckBox check = new JCheckBox("Pole");
    		newPanel.add(check);
    		
    		
    
    		// -------------
    
    		OProgramie metodaOProgramie = new OProgramie();
    		JPanel panelOProgramieloadPanel = new JPanel();
    		pane.add("OProgramie", panelOProgramieloadPanel);
    
    		panelOProgramieloadPanel.add(metodaOProgramie.getAutorLabel());
    
    		this.addWindowListener(new WindowAdapter() {
    			public void windowClosing(WindowEvent e) {
    				Main.this.windowClosed();
    			}
    		});
    	}
    
    	protected void windowClosed() {
    		System.exit(0);
    	}
    
    	public static void main(String[] arg) throws FileNotFoundException,
    			IOException {
    		Main f = new Main();
    		f.setVisible(true);
    	}
    }
    I think that I write to many lines, code and do the same in this class.

  6. #6
    PhHein's Avatar
    PhHein is offline Senior Member
    Join Date
    Apr 2009
    Location
    Germany
    Posts
    1,430
    Rep Power
    7

    Default Re: How to write code smaller

    Quote Originally Posted by JosAH View Post
    @Qazi (allaudin): the avatar you're using now is also used by PhHein; did you copy it from him?

    Jos
    Hi Jos, it's OK. Qazi has asked for permission.
    allaudin likes this.
    Math problems? Call 1-800-[(10x)(13i)^2]-[sin(xy)/2.362x]
    The Ubiquitous Newbie Tips

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

    Default Re: How to write code smaller

    Quote Originally Posted by PhHein View Post
    Hi Jos, it's OK. Qazi has asked for permission.
    *click* (<-- the sound of the rotating knives machine being switched off ;-)'

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  8. #8
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    3,524
    Rep Power
    5

    Default Re: How to write code smaller

    Re: subject: I had to laugh! I thought you were going to tell him to use a smaller font.

    Regards,
    Jim
    The Java™ Tutorial | SSCCE | Java Naming Conventions
    Poor planning our your part does not constitute an emergency on my part.

  9. #9
    allaudin's Avatar
    allaudin is offline Senior Member
    Join Date
    Apr 2013
    Posts
    271
    Rep Power
    0

    Default Re: How to write code smaller

    Quote Originally Posted by JosAH View Post
    @Qazi (allaudin): the avatar you're using now is also used by PhHein; did you copy it from him?

    Jos
    yup .. copied

Similar Threads

  1. Was: Re: What are you using to write your code?
    By rajashripawar11@gmail.com in forum New To Java
    Replies: 2
    Last Post: 11-27-2012, 01:46 PM
  2. Auto-completion of code when when I write code
    By nutzu2011 in forum Eclipse
    Replies: 0
    Last Post: 04-06-2012, 07:01 PM
  3. Need help to write the code
    By Galichka in forum New To Java
    Replies: 24
    Last Post: 03-27-2012, 04:39 PM
  4. how do i write this code?
    By Libertyman in forum New To Java
    Replies: 23
    Last Post: 06-22-2010, 12:43 PM

Posting Permissions

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