Results 1 to 8 of 8
Thread: Newbi. What way is the best?
- 10-01-2009, 05:56 AM #1
Senior Member
- Join Date
- Sep 2009
- Location
- Sweden/Borås
- Posts
- 107
- Rep Power
- 0
Newbi. What way is the best?
God morning.
Learning diffrent ways to type my code as time go. Need an advice on witch exampel is the best. And why it is the best.
Exampel 1.
Exampel 2.Java Code:public JMenuBar createMenu() { // Create the menu system JMenuBar aMenuBar = new JMenuBar(); JMenu aMenu = new JMenu("Arkiv"); JMenuItem aMenuItem = new JMenuItem("Close"); aMenuBar.add(aMenu); aMenu.add(aMenuItem); return aMenuBar; }// end menu public static void gui() { // Create gui AND thing in it JTextField aTextField = new JTextField(); JLabel aLabel = new JLabel("Text1: "); JFrame aFrame = new JFrame("myGui 1"); aFrame.setSize(200, 200); aFrame.setVisible(true); aFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); JPanel aPanel = new JPanel(new BorderLayout()); aLabel.setLabelFor(aTextField); aPanel.add(aLabel, BorderLayout.WEST); aPanel.add(aTextField, BorderLayout.CENTER); aFrame.add(aPanel, BorderLayout.NORTH); mygui gui = new mygui(); aFrame.setJMenuBar(gui.createMenu()); aFrame.pack(); }// end gui public static void main(String[] args) { gui(); // runs gui. } }
Thankfull for any advice that can make me better. / Ocean.Java Code:private JMenuBar createMenu() { // Create the menu system JMenuBar aMenuBar = new JMenuBar(); JMenu aMenu = new JMenu("Arkiv"); JMenuItem aMenuItem = new JMenuItem("Close"); aMenuBar.add(aMenu); aMenu.add(aMenuItem); return aMenuBar; }// end menu private JPanel createThings() { // Things to be on the frame JTextField aTextField = new JTextField(); JLabel aLabel = new JLabel("Text2: "); JPanel aPanel = new JPanel(new BorderLayout()); aLabel.setLabelFor(aTextField); aPanel.add(aLabel, BorderLayout.WEST); aPanel.add(aTextField, BorderLayout.CENTER); return aPanel; } // end things private static void gui() { // create and show the gui JFrame aFrame = new JFrame("myGui 2"); aFrame.setSize(200, 200); aFrame.setVisible(true); aFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); mygui frame = new mygui(); aFrame.add(frame.createThings(), BorderLayout.NORTH); mygui gui = new mygui(); aFrame.setJMenuBar(gui.createMenu()); aFrame.pack(); } // end gui public static void main(String[] args) { gui(); } }Last edited by ocean; 10-01-2009 at 05:59 AM.
- 10-01-2009, 09:11 AM #2
Senior Member
- Join Date
- Aug 2009
- Posts
- 2,388
- Rep Power
- 6
The only differences I could see from your two snippets is that in the second one, the Panel is created in a separate method. That helps if you have to create that panel many times, in which case you don't have to type up all that code. Just call the method. Since the panels returned will always look exactly the same, it would be useful only if the panel returned is in fact reusable.
- 10-01-2009, 09:42 AM #3
Moderator
- Join Date
- Apr 2009
- Posts
- 10,481
- Rep Power
- 16
I dunno.
Personally I like to break things down like that.
It may not make any functional difference, but I find it easier to read a makeThisPanel(), than attempt to find the bits that go towards making thisPanel in a single method. Tends to be the way I write code as well, starting with something like:
and then actually do methods representing those comments.Java Code:public void buildPanel() { // make top search panel // make center result panel // make bottom button panel }
What I will say, though, is the gui() call should be in a SwingUtilities.invokeLater() call.
- 10-01-2009, 09:54 AM #4
Senior Member
- Join Date
- Sep 2009
- Location
- Sweden/Borås
- Posts
- 107
- Rep Power
- 0
Yes thas is the only diffrence, And i made them private. wich is a bad ide if i need to reuse them? made this exampel very simply just to show my point. But lets say im making a really big program. does it make any diffrence in memory use or speed?
- 10-01-2009, 10:02 AM #5
Moderator
- Join Date
- Apr 2009
- Posts
- 10,481
- Rep Power
- 16
Making them private's not a problem.
I wouldn't have a generic toolkit for panels anyway.
They tend to be local to whatever screen I'm building.
- 10-01-2009, 11:30 AM #6
Senior Member
- Join Date
- Sep 2009
- Location
- Sweden/Borås
- Posts
- 107
- Rep Power
- 0
To make SwingUtilities.invokeLater() work i had to make gui runnable and add return statement to gui(). Since invokeLater are new to me i looked at sun doc. i can´t figure out why i should use it. Any help explaining it would be greatful.
EDIT: 2009-10-01 kl 12:00
After some more reading i found out how you mean. and made
Is this the correct way to run gui()?Java Code:public static void main(String[] args) { SwingUtilities.invokeLater(new Runnable() { public void run() { gui();Last edited by ocean; 10-01-2009 at 11:43 AM.
- 10-01-2009, 11:51 AM #7
Moderator
- Join Date
- Apr 2009
- Posts
- 10,481
- Rep Power
- 16
Yep, that's the way you do it.
It makes sure the gui is built by the Event Dispatch Thread (the thread that deals with all the gui stuff, or should anyway). Good practice, basically.
- 10-01-2009, 12:06 PM #8
Senior Member
- Join Date
- Sep 2009
- Location
- Sweden/Borås
- Posts
- 107
- Rep Power
- 0
Similar Threads
-
newbi. ActionEvent. Close on click
By ocean in forum New To JavaReplies: 3Last Post: 09-18-2009, 09:41 AM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks