onyxgem 0 Report post Posted June 26, 2007 Java GUIMaking a Little Java ProgramSec. 1: Imports and starting it offSec. 2: VariablesSec. 3: Frame and StuffSec. 4: Declaring buttonsSec. 5: Adding buttonsSec. 6: Action ListeningSec. 7: Using this for a learning experienceSection 1Now, let's think. What imports do we need? We obviously need GUI imports. We also need the action Listener. So,let's declare this at the very top:Code: import java.awt.*;import java.awt.event.*;import javax.swing.*;That's all we need to get all our supplies. Now to start us off.Skip a couple lines and add:Code:public class Tutorial extends JFrame implements ActionListener {Section 2Let's declare our variables.Right below the class, addCode:private static Tutorial frame; private static JTextField text;I will tell you about this later.Section 3This is in all java programs. To start it and load everything, you must add:Code:public static main(String[] args){We must now add the frame, so skip a 2 lines and add:Code:frame = new Tutorial(); frame.setTitle("Tutorial of GUI"); frame.setSize(400, 100); frame.setDefaultCloseOperation(EXIT_ON_CLOSE); frame.setVisible(true);Ending the statement, add one last "}"Section 4Now add:Code:public Tutorial() {Next add: Code:setLayout(new BorderLayout()); JTextField text = new JTextField(10); JButton button = new JButton("JButton1"); JButton button1 = new JButton("JButton2"); JButton button2 = new JButton("JButton3"); JButton button3 = new JButton("JButton4"); JPanel panel = new JPanel();That adds the button vars.Section 5Let's add the buttons now. Add right below that, add:Code:panel.setLayout(new FlowLayout()); panel.add(button); panel.add(button1); panel.add(button2); panel.add(button3); add(panel, BorderLayout.NORTH); button.addActionListener(this); button1.addActionListener(this); button2.addActionListener(this); button3.addActionListener(this); }That adds the buttons on.Section 6Let's make the outcome of clicking the button. Add:Code:public void actionPerformed(ActionEvent e) { JOptionPane.showMessageDialog(frame, " was selected." ); } }That tells you what button was selected. Now save and compile. Now run the program and see what the outcome is.Section 7You can view other things like this at:http://www.oracle.com/technetwork/java/index.htmlThis will help you understand all the functions of java GUI.-Bill for Unknnote:i copied this from my word and pasted it here hope this helps ya out im sorry if i misspelled anything i type fast Share this post Link to post Share on other sites
galexcd 0 Report post Posted June 27, 2007 Great tutorial! I still hate java though. I remember learning gui in java about four years ago, and it was hell for me. This probably would have made it a bit easier but I much prefer c++ to java when making gui applications. Anyway great tut! Keep up the good work! Share this post Link to post Share on other sites
zak92 0 Report post Posted June 29, 2007 These languages confuse me but at the same time fascinate me as some gibberish can turn into such good creation such as a great and a profitable web site. Thanks man love you as I wanted to just learn this. Share this post Link to post Share on other sites
master_bacarra 0 Report post Posted June 29, 2007 the reason why i like java more is because i could just use the exception handling for the errors. but i have to admit i'm not a great programmer at all. i never really appreciated c/c++ because of the complicated pointers. programming with pointers was hell for me.anyways, great tutorial for starters. Share this post Link to post Share on other sites