|
本帖最后由 MKAIR 于 2013-7-2 18:27 编辑
- import java.awt.BorderLayout;
- import java.awt.GridLayout;
- import java.awt.event.ActionEvent;
- import java.awt.event.ActionListener;
- import java.io.DataInputStream;
- import java.io.DataOutputStream;
- import java.io.IOException;
- import javax.swing.JButton;
- import javax.swing.JLabel;
- import javax.swing.JPanel;
- import javax.swing.JSeparator;
- import javax.swing.JTabbedPane;
- import javax.swing.JTextArea;
- import javax.swing.SwingUtilities;
- import javax.swing.WindowConstants;
- import lejos.pc.comm.NXTConnector;
- import org.kociemba.twophase.Search;
- /**
- * This code was edited or generated using CloudGarden's Jigloo
- * SWT/Swing GUI Builder, which is free for non-commercial
- * use. If Jigloo is being used commercially (ie, by a corporation,
- * company or business for any purpose whatever) then you
- * should purchase a license for each developer using Jigloo.
- * Please visit www.cloudgarden.com for details.
- * Use of Jigloo implies acceptance of these licensing terms.
- * A COMMERCIAL LICENSE HAS NOT BEEN PURCHASED FOR
- * THIS MACHINE, SO JIGLOO OR THIS CODE CANNOT BE USED
- * LEGALLY FOR ANY CORPORATE OR COMMERCIAL PURPOSE.
- */
- public class NewJFrame extends javax.swing.JFrame implements ActionListener{
- {
- //Set Look & Feel
- try {
- javax.swing.UIManager.setLookAndFeel("javax.swing.plaf.nimbus.NimbusLookAndFeel");
- } catch(Exception e) {
- e.printStackTrace();
- }
- }
- /**
- * Simple JavaDoc
- * @author Michael Peng
- * Set the Version of the main program
- */
- private static final long serialVersionUID = 6338002822670303714L;
- // console string
- private static final String con = null;
- private JSeparator jSeparator1;
- private JLabel Note;
- private JTextArea Cube;
- private JPanel CubePanel;
- private JButton solve;
- private JPanel jPanel1;
- private JLabel debug;
- private JButton test;
- private JButton disconnect;
- private JPanel ConnectionDebug;
- private JPanel mainPanel;
- private JButton connect;
- private JTabbedPane ActionPane;
- private JTextArea Console;
- private static NXTConnector conn = new NXTConnector();
- private DataOutputStream dos = new DataOutputStream(conn.getOutputStream());
- private DataInputStream dis = new DataInputStream(conn.getInputStream());
- /**
- * Auto-generated main method to display this JFrame
- */
- public static void main(String[] args) {
- SwingUtilities.invokeLater(new Runnable() {
- public void run() {
- NewJFrame inst = new NewJFrame();
- inst.setLocationRelativeTo(null);
- inst.setVisible(true);
- }
- });
- }
-
- public NewJFrame() {
- super();
- initGUI();
- }
-
- private void initGUI() {
- try {
- setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
- this.setTitle("MindCuber Console Pro v1.00");
- {
- mainPanel = new JPanel();
- getContentPane().add(mainPanel, BorderLayout.CENTER);
- mainPanel.setPreferredSize(new java.awt.Dimension(487, 311));
- {
- connect = new JButton();
- mainPanel.add(connect);
- connect.setText("Connect!");
- connect.setPreferredSize(new java.awt.Dimension(224, 37));
- connect.addActionListener(this);
- }
- {
- ActionPane = new JTabbedPane();
- mainPanel.add(ActionPane);
- ActionPane.setPreferredSize(new java.awt.Dimension(329, 255));
- {
- Console = new JTextArea(con);
- ActionPane.addTab("Console", null, Console, null);
- Console.setPreferredSize(new java.awt.Dimension(429, 226));
- ActionPane.addTab("Connection Debug", null, getConnectionDebug(), null);
- ActionPane.addTab("Cube", null, getJPanel1(), null);
- }
- }
- }
- pack();
- this.setSize(432, 349);
- } catch (Exception e) {
- //add your error handling code here
- e.printStackTrace();
- }
- }
- private JPanel getConnectionDebug() {
- if(ConnectionDebug == null) {
- ConnectionDebug = new JPanel();
- ConnectionDebug.add(getDisconnect());
- ConnectionDebug.add(getTest());
- ConnectionDebug.add(getDebug());
- ConnectionDebug.add(getSolve());
- ConnectionDebug.add(getJSeparator1());
- }
- return ConnectionDebug;
- }
-
- private JButton getDisconnect() {
- if(disconnect == null) {
- disconnect = new JButton();
- disconnect.setText("Disconnect!");
- disconnect.setPreferredSize(new java.awt.Dimension(132, 71));
- disconnect.addActionListener(this);
- }
- return disconnect;
- }
-
- private JButton getTest() {
- if(test == null) {
- test = new JButton();
- test.setPreferredSize(new java.awt.Dimension(129, 71));
- test.setText("Test Connection!");
- test.addActionListener(this);
- }
- return test;
- }
-
- private JLabel getDebug() {
- if(debug == null) {
- debug = new JLabel();
- debug.setText("waiting...");
- debug.setPreferredSize(new java.awt.Dimension(234, 15));
- }
- return debug;
- }
- public void actionPerformed(ActionEvent e){
- Console.setLineWrap(true);
- if (e.getSource() == connect)
- {
- try {
- conn.connectTo();
- } catch (Exception e1) {
- e1.printStackTrace();
- }
- Console.setText("connected");
- }
- else if (e.getSource() == disconnect)
- {
- try {
- conn.close();
- } catch (IOException e1) {
- e1.printStackTrace();
- }
- Console.setText("Disconnected");
- }
- else if (e.getSource() == solve)
- {
- String result = Search.solution(Cube.getText(), 20,20,false);
- System.out.println(result);
- }
- else if (e.getSource() == test)
- {
- try {
- dos.writeInt(100);
- } catch (IOException e1) {
- Console.setText("Error while sending Int");
- System.err.println("Problems occured while testing connection. Please try again.");
- System.exit(0);
- }
- try {
- dis.readInt();
- } catch (IOException e1) {
- Console.setText("Error while receiving Int");
- System.err.println("Problems occured while testing connection. Please try again.");
- System.exit(0);
- }
- }
- }
-
- private JPanel getJPanel1() {
- if(jPanel1 == null) {
- jPanel1 = new JPanel();
- GridLayout jPanel1Layout = new GridLayout(1, 1);
- jPanel1Layout.setHgap(5);
- jPanel1Layout.setVgap(5);
- jPanel1Layout.setColumns(1);
- jPanel1.setLayout(jPanel1Layout);
- jPanel1.add(getCubePanel());
- }
- return jPanel1;
- }
- private JButton getSolve() {
- if(solve == null) {
- solve = new JButton();
- solve.setText("Solve that Cube!");
- solve.setPreferredSize(new java.awt.Dimension(183, 56));
- solve.addActionListener(this);
- }
- return solve;
-
- }
-
- private JPanel getCubePanel() {
- if(CubePanel == null) {
- CubePanel = new JPanel();
- CubePanel.add(getCube());
- CubePanel.add(getNote());
- }
- return CubePanel;
- }
-
- private JTextArea getCube() {
- if(Cube == null) {
- Cube = new JTextArea();
- Cube.setPreferredSize(new java.awt.Dimension(305, 180));
- }
- return Cube;
- }
-
- private JLabel getNote() {
- if(Note == null) {
- Note = new JLabel();
- Note.setText("Remember, the cube must be white face and blue up!");
- }
- return Note;
- }
-
- private JSeparator getJSeparator1() {
- if(jSeparator1 == null) {
- jSeparator1 = new JSeparator();
- jSeparator1.setPreferredSize(new java.awt.Dimension(212, 2));
- }
- return jSeparator1;
- }
- }
复制代码 按下test按钮,显示:
Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
at java.io.DataOutputStream.writeInt(DataOutputStream.java:197)
at NewJFrame.actionPerformed(NewJFrame.java:194)
at javax.swing.AbstractButton.fireActionPerformed(AbstractButton.java:2018)
at javax.swing.AbstractButton$Handler.actionPerformed(AbstractButton.java:2341)
at javax.swing.DefaultButtonModel.fireActionPerformed(DefaultButtonModel.java:402)
at javax.swing.DefaultButtonModel.setPressed(DefaultButtonModel.java:259)
at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(BasicButtonListener.java:252)
at java.awt.Component.processMouseEvent(Component.java:6505)
at javax.swing.JComponent.processMouseEvent(JComponent.java:3321)
at java.awt.Component.processEvent(Component.java:6270)
at java.awt.Container.processEvent(Container.java:2229)
at java.awt.Component.dispatchEventImpl(Component.java:4861)
at java.awt.Container.dispatchEventImpl(Container.java:2287)
at java.awt.Component.dispatchEvent(Component.java:4687)
at java.awt.LightweightDispatcher.retargetMouseEvent(Container.java:4832)
at java.awt.LightweightDispatcher.processMouseEvent(Container.java:4492)
at java.awt.LightweightDispatcher.dispatchEvent(Container.java:4422)
at java.awt.Container.dispatchEventImpl(Container.java:2273)
at java.awt.Window.dispatchEventImpl(Window.java:2719)
at java.awt.Component.dispatchEvent(Component.java:4687)
at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:729)
at java.awt.EventQueue.access$200(EventQueue.java:103)
at java.awt.EventQueue$3.run(EventQueue.java:688)
at java.awt.EventQueue$3.run(EventQueue.java:686)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:76)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:87)
at java.awt.EventQueue$4.run(EventQueue.java:702)
at java.awt.EventQueue$4.run(EventQueue.java:700)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:76)
at java.awt.EventQueue.dispatchEvent(EventQueue.java:699)
at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:242)
at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:161)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:150)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:146)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:138)
at java.awt.EventDispatchThread.run(EventDispatchThread.java:91)
(稍作更改)
|
|