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:
Risk.javaimport 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(); } }); } }
Thanks,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(); } } } } }
cc11rocks