Using two graphical interfaces from one source

I am working on an existing project and want to simplify and use it. I have two GUIs that I want them to feed on only one resource, as well as some GUI codes. I mean, that gui1.javathey gui2.javaconsist of their GUI codes. And one for their common parts. Let me call him common.java. With the gui selection part, I can satisfy the selected gui (gui1 or gui2). I think to expand common.javaon JFrame, then continue gui1.javaand gui2.javawith help common.java. Moreover, if there is an external part of one of these graphical interfaces, I can add the external part using the if condition (as I said, I can determine which gui is selected.) For example:

protected void MovementStateControl() {
        try {
            URL url = new URL(NameofMyproject.GetWepIp() + "<MESSAGE><Command_No>4</Command_No></MESSAGE>");
            URLConnection connection = url.openConnection();
            Document doc = parseXML(connection.getInputStream());
            NodeList Settings = doc.getElementsByTagName("SYSTEM_SETTINGS");
            Node MovementSystem = Settings.item(0);
            Element MovementElem = (Element) MovementSystem;
            jLabel7.setText(MovementElem.getElementsByTagName("device_name").item(0).getTextContent());
            SystemGuiSelect.DeviceName = MovementElem.getElementsByTagName("device_name").item(0).getTextContent();
            NameofMyproject.signal_max_level = Integer.parseInt(MovementElem.getElementsByTagName("signal_max_val").item(0).getTextContent());

            /* If gui1 is selected, the part should be done as well. Otherwise, just above part okay. */
            if (gui1) {
                NameofMyproject.signal_min_level = Integer.parseInt(MovementElem.getElementsByTagName("signal_min_val").item(0).getTextContent());
                if (!"EXISTS".equals(MovementElem.getElementsByTagName("polarization_system").item(0).getTextContent())) {
                    jLabel24.setVisible(false);
                    LblPolAngle.setVisible(false);
                    lblPolTarget.setVisible(false);
                    jLabel13.setVisible(false);
                    jTextField3.setVisible(false);
                    jButton16.setVisible(false);
                    jButton8.setText("Tx-Xy");
                    jButton3.setVisible(false);
                    jButton4.setVisible(false);
                    jProgressBar3.setVisible(false);
                    jLabel36.setVisible(false);
                    jLabel37.setVisible(false);
                    jLabel5.setVisible(false);
                    jButton18.setVisible(false);
                } else {
                    jLabel24.setVisible(true);
                    LblPolAngle.setVisible(true);
                    lblPolTarget.setVisible(true);
                    jLabel13.setVisible(true);
                    jTextField3.setVisible(true);
                    jButton16.setVisible(true);
                    jButton8.setText("Tx-Xy-Zu");
                    jButton3.setVisible(true);
                    jButton4.setVisible(true);
                    jProgressBar3.setVisible(true);
                    jLabel36.setVisible(true);
                    jLabel37.setVisible(true);
                    jLabel5.setVisible(true);
                    jButton18.setVisible(true);
                }
            }

        } catch (Exception e) {  }
    }

. , GUI common.java, - GUI. , common.java - , . (JLabels, JButtons .. ) thread , . NetBeans. , , , ? ( , vargs), , , .

+4
3

, .
-, , ( , ..) , , ( ). , gui . , , .

-,

 if (gui1) { then...

, ( , -) ( ), . , .
, . , Common . , .

guis :

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;

public abstract class CommonFrame extends JFrame implements MyFrameListener {

    protected JLabel myLabelForControlMove;
    private JButton btnControlMove;
    private JPanel panel;

    public CommonFrame() {
        panel = new JPanel();
        add(panel);
        myLabelForControlMove = new JLabel("waiting for information...");
        panel.add(myLabelForControlMove);
        btnControlMove = new JButton("click to control move");
        panel.add(btnControlMove);
        setVisible(true);
        pack();


        // call concrete class with no aware which one is used
        onMovementStateCreation();

        btnControlMove.addActionListener(new ActionListener() {

            @Override
            public void actionPerformed(ActionEvent e) {
            // call concrete class with no aware which one is used
            onMovementStateControl();
            }
        });
    }
}



public interface MyFrameListener {

  void onMovementStateCreation();

  void onMovementStateControl();

}


public class Gui1 extends CommonFrame {

 @Override
 public void onMovementStateCreation() {
   myLabelForControlMove.setText("control starts");
 }

 @Override
 public void onMovementStateControl() {
   myLabelForControlMove.setText("control with state Gui1");
 }

}


public class Gui2 extends CommonFrame {

  @Override
  public void onMovementStateCreation() {
    myLabelForControlMove.setText("control with state Gui2");
  }

  @Override
  public void onMovementStateControl() {
    // does nothing
  }

}
+1

, , . -, , - Swing. In Process Event Bus View Control , . Swing code.

, , . , device_name . Event Bus. , EventDispatchThread.

, gui2.java , , , gui1.java, . , , - .

Ectract Swing

, , .., (, ). , JPanel , .

EventBus , , , . JFrame, .

, , .

+3

, . .

You common.java should consist of specialized containers. This will separate common.java from gui1.java and gui2.java, in the future, if you need to add a new gui3.java, you will not break any APIs and just create a new SpecializedGUI.java

enter image description here

+2
source

Source: https://habr.com/ru/post/1648641/


All Articles