Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 2 of 2

Thread: Merge java files into one and how to use keylistener or key bindings?

  1. #1
    Junior Member
    Join Date
    Feb 2011
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Merge java files into one and how to use keylistener or key bindings?

    I have two questions. A. How do I merge my two .java files and B. how do I use keylistener or keybindings in my code ? I am open to either keylisteners or keybindings and have tried to do this, but I have not been sucessful.
    I am using two files, which compile fine and run exactly as I have them coded. I am very new to Java, but have worked with HTML and Autohotkey extensivly, Autohotkey more than HTML. Any help would be appreciated.
    RiskApp.java:
    import javax.swing.*;
    import java.text.NumberFormat;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    import javax.swing.SwingUtilities;
    import java.awt.*;
    import java.awt.event.*;
     
    public class RiskApp extends JFrame {
        private static void createAndShowGUI() {
            JFrame frame = new JFrame("Risk");
    JPanel panel = new JPanel(new BorderLayout());
    Container contentPane = frame.getContentPane();
    contentPane.setLayout(new GridLayout(1,2));
            frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            Risk riskPanel = new Risk();
            frame.getContentPane().add(riskPanel);
            frame.setSize(305,90);
            frame.setResizable(false);
            frame.setVisible(true);
        }
     
        public static void main(String[] args) {
            javax.swing.SwingUtilities.invokeLater(new Runnable() {
                public void run() {
    try {
    UIManager.setLookAndFeel("com.sun.java.swing.plaf.windows.WindowsLookAndFeel");
    } catch (UnsupportedLookAndFeelException e) {
    } catch (ClassNotFoundException e) {
    } catch (InstantiationException e) {
    } catch (IllegalAccessException e) {
    }
                    createAndShowGUI();
                }
            });
        }
    }
    Risk.java
    import javax.swing.*;
    import java.awt.*;
    import java.text.NumberFormat;
    import java.awt.event.ActionListener;
    import java.awt.event.ActionEvent;
    import java.awt.event.*;
     
       public class Risk extends JPanel {
       public int leftNumber, rightNumber, setbad;
       public JButton Enter;
       public JFormattedTextField stupid, yummy;
       public JLabel smart, dumb, nook, shook;
       public String nos, yos, setnice;
       public NumberFormat yucker = NumberFormat.getNumberInstance();
       public NumberFormat nucker = NumberFormat.getNumberInstance();
     
       public Risk() {
          setbad = 1;
          smart = new JLabel("First Number:");
          add(smart);
    	      stupid = new JFormattedTextField(yucker);
          stupid.setColumns(3);
          stupid.setText("0");
          add(stupid);
          boolean isDigit;
          dumb = new JLabel("Second Number:");
          add(dumb);
          yummy = new JFormattedTextField(nucker);
          yummy.setColumns(3);
          yummy.setText("0");
          add(yummy);
          Enter = new JButton("Enter");
          add(Enter);
          Enter.addActionListener(new ButtonListener());	
     
     
       }
       class ButtonListener implements ActionListener {
          ButtonListener() {
          }
     
          public final void actionPerformed(ActionEvent e) {
             if (e.getActionCommand().equals("Enter")) {
    		if (setbad == 1) {
    			shook = new JLabel("0");
    			nook = new JLabel("0");
    			add(shook);
    			add(nook);
     
              	String nos = stupid.getText();
    	  	String yos = yummy.getText();
    	   	shook.setText("First Editable Number: " + nos);
    	   	nook.setText("Second Editable Number: " + yos);
    		setbad ++;
    	revalidate();
    }
    else {
    	  String nos = stupid.getText();
    	  String yos = yummy.getText();
    	   shook.setText("First Editable Number: " + nos);
    	   nook.setText("Second Editable Number: " + yos);
    	revalidate();
    	}
     
     
     
             }
          }
       }
    }
    Thanks,
    cc11rocks
    Last edited by cc11rocks; February 9th, 2011 at 09:44 PM.


  2. #2
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,424
    My Mood
    Hungover
    Thanks
    144
    Thanked 636 Times in 540 Posts

    Default Re: Merge java files into one and how to use keylistener or key bindings?

    A- Why do you think you should?

    B- What did google tell you?
    How to Write a Key Listener (The Java™ Tutorials > Creating a GUI With JFC/Swing > Writing Event Listeners)
    How to Use Key Bindings (The Java™ Tutorials > Creating a GUI With JFC/Swing > Using Other Swing Features)
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

Similar Threads

  1. Key Bindings
    By nemo in forum What's Wrong With My Code?
    Replies: 2
    Last Post: December 28th, 2010, 08:50 PM
  2. Replies: 4
    Last Post: November 14th, 2010, 11:44 AM
  3. how to merge my OO with GUI
    By zyspt in forum AWT / Java Swing
    Replies: 1
    Last Post: May 18th, 2010, 03:28 PM
  4. Merge 2 Sorted Text Files into a Third
    By Epyllion in forum What's Wrong With My Code?
    Replies: 0
    Last Post: February 10th, 2010, 08:24 PM
  5. Question...Key bindings
    By TheEnd in forum What's Wrong With My Code?
    Replies: 1
    Last Post: January 19th, 2010, 09:10 PM