1

I'm trying to make a GUI based on BorderLayout and GridLayout by hand-writing not with a form builder. Because of that, I'm most likely skipping a part in the code. It is loaded but not seem.

import java.awt.*;
import java.awt.event.*;
import java.io.IOException;
import java.net.InetAddress;
import javax.swing.*;
import javax.swing.border.EmptyBorder;


class PanelDemo {

    JButton jbtnUpArrowFirst;
    JButton jbtnDownArrowFirst;
    JButton jbtnUpArrowSec;
    JButton jbtnDownArrowSec;
    JButton jbtnUpArrowThird;
    JButton jbtnDownArrowThird;

    JLabel jlblLedUpFirst;
    JLabel jlblLedDownFirst;
    JLabel jlblLedUpSec;
    JLabel jlblLedDownSec;
    JLabel jlblLedUpThird;
    JLabel jlblLedDownThird;

    private JComponent ui = null;
    private Insets buttonMargin = new Insets(10,10,10,10);

    PanelDemo() throws IOException {

        InitComponents();
    }

    private void InitComponents() {

        if (ui != null) return;

        ui = new JPanel(new BorderLayout(4,4));
        ui.setBorder(new EmptyBorder(4,4,4,4));

        int gap = 5;


        JPanel jpnl = new JPanel(new GridLayout(2, 4, gap, gap));
        jpnl.setBorder(new EmptyBorder(0, 0, 0, 50));
        jpnl.setOpaque(true);
        jpnl.setBorder(
                BorderFactory.createLineBorder(Color.BLUE));


        JPanel jpnl2 = new JPanel(new GridLayout(2, 2, gap, gap));
        jpnl2.setOpaque(true);
        jpnl2.setBorder(
                BorderFactory.createLineBorder(Color.RED));





        jbtnUpArrowFirst = new JButton("upfirst" ,new ImageIcon("/Users/Jack/Desktop/up.png"));
        jbtnUpArrowFirst.setVerticalTextPosition(SwingConstants.BOTTOM);
        jbtnUpArrowFirst.setHorizontalTextPosition(SwingConstants.CENTER);

        jbtnDownArrowFirst = new JButton("downfirst" ,new ImageIcon("/Users/Jack/Desktop/down.png"));
        jbtnDownArrowFirst.setVerticalTextPosition(SwingConstants.BOTTOM);
        jbtnDownArrowFirst.setHorizontalTextPosition(SwingConstants.CENTER);


        jlblLedUpFirst = new JLabel("upledfirst", new ImageIcon("/Users/Jack/Desktop/ledOff.png"), SwingConstants.CENTER);
        jlblLedUpFirst.setVerticalTextPosition(SwingConstants.BOTTOM);
        jlblLedUpFirst.setHorizontalTextPosition(SwingConstants.CENTER);


        jlblLedDownFirst = new JLabel("downledfirst", new ImageIcon("/Users/Jack/Desktop/ledOff.png"), SwingConstants.CENTER);
        jlblLedDownFirst.setVerticalTextPosition(SwingConstants.BOTTOM);
        jlblLedDownFirst.setHorizontalTextPosition(SwingConstants.CENTER);


        ////////////////////////////////////////////////////////////////////////
        jbtnUpArrowSec = new JButton("upsec", new ImageIcon("/Users/Jack/Desktop/up.png"));
        jbtnUpArrowSec.setVerticalTextPosition(SwingConstants.BOTTOM);
        jbtnUpArrowSec.setHorizontalTextPosition(SwingConstants.CENTER);

        jbtnDownArrowSec = new JButton("downsec" ,new ImageIcon("/Users/Jack/Desktop/down.png"));
        jbtnDownArrowSec.setVerticalTextPosition(SwingConstants.BOTTOM);
        jbtnDownArrowSec.setHorizontalTextPosition(SwingConstants.CENTER);


        jlblLedUpSec = new JLabel("upledsecond", new ImageIcon("/Users/Jack/Desktop/ledOff.png"), SwingConstants.CENTER);
        jlblLedUpSec.setVerticalTextPosition(SwingConstants.BOTTOM);
        jlblLedUpSec.setHorizontalTextPosition(SwingConstants.CENTER);

        jlblLedDownSec = new JLabel("downledsec", new ImageIcon("/Users/Jack/Desktop/ledOff.png"), SwingConstants.CENTER);
        jlblLedDownSec.setVerticalTextPosition(SwingConstants.BOTTOM);
        jlblLedDownSec.setHorizontalTextPosition(SwingConstants.CENTER);
        ////////////////////////////////////////////////////////////////////////
        jbtnUpArrowThird = new JButton("upthirt", new ImageIcon("/Users/Jack/Desktop/up.png"));
        jbtnUpArrowThird.setVerticalTextPosition(SwingConstants.BOTTOM);
        jbtnUpArrowThird.setHorizontalTextPosition(SwingConstants.CENTER);

        jbtnDownArrowThird = new JButton("downthird" ,new ImageIcon("/Users/Jack/Desktop/down.png"));
        jbtnDownArrowThird.setVerticalTextPosition(SwingConstants.BOTTOM);
        jbtnDownArrowThird.setHorizontalTextPosition(SwingConstants.CENTER);


        jlblLedUpThird = new JLabel("upledthird", new ImageIcon("/Users/Jack/Desktop/ledOff.png"), SwingConstants.CENTER);
        jlblLedUpThird.setVerticalTextPosition(SwingConstants.BOTTOM);
        jlblLedUpThird.setHorizontalTextPosition(SwingConstants.CENTER);

        jlblLedDownThird = new JLabel("downledthird", new ImageIcon("/Users/Jack/Desktop/ledOff.png"), SwingConstants.CENTER);
        jlblLedDownThird.setVerticalTextPosition(SwingConstants.BOTTOM);
        jlblLedDownThird.setHorizontalTextPosition(SwingConstants.CENTER);


        // Add the buttons and label to the panel.
        jpnl.add(jbtnUpArrowFirst);
        jpnl.add(jlblLedUpFirst);
        jpnl.add(jbtnUpArrowSec);
        jpnl.add(jlblLedUpSec);
        jpnl.add(jbtnDownArrowFirst);
        jpnl.add(jlblLedDownFirst);
        jpnl.add(jbtnDownArrowSec);
        jpnl.add(jlblLedDownSec);

        jpnl2.add(jbtnUpArrowThird);
        jpnl2.add(jlblLedUpThird);
        jpnl2.add(jbtnDownArrowThird);
        jpnl2.add(jlblLedDownThird);


        ui.add(jpnl, BorderLayout.CENTER);
        ui.add(jpnl2, BorderLayout.LINE_END);

        JFrame jfrm = new JFrame("Use Two JPanels");
        jfrm.setLocationByPlatform(true);
        jfrm.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
        jfrm.setContentPane(getUI());
        jfrm.setMinimumSize(jfrm.getSize());
        jfrm.pack();
        jfrm.setVisible(true);

        /*jbtnUpArrowFirst.addActionListener(e -> {
            try {
                test.send("ANTSW=VERT\r\n");

                //if (test.recv().equalsIgnoreCase(""))

            } catch (IOException e1) {
                e1.printStackTrace();
            }
        });

        jbtnDownArrowFirst.addActionListener(e -> {
            try {
                test.send("ANTSW=HORZ\r\n");
            } catch (IOException e1) {
                e1.printStackTrace();
            }
        });*/

    }

    public JComponent getUI() {
        return ui;
    }

    public static void main(String args[]) {
        // Create the frame on the event dispatching thread.
        SwingUtilities.invokeLater(() -> {
                try {
                    UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
                    new PanelDemo();
                } catch (IOException e) {
                    e.printStackTrace();
                } catch (IllegalAccessException e) {
                    e.printStackTrace();
                } catch (InstantiationException e) {
                    e.printStackTrace();
                } catch (UnsupportedLookAndFeelException e) {
                    e.printStackTrace();
                } catch (ClassNotFoundException e) {
                    e.printStackTrace();
                }
        });
    }
}
swinger
  • 63
  • 1
  • 5
  • Shouldnt you call "setVisible()" or something like that on your panel at some point? – GhostCat Aug 03 '16 at 08:53
  • but I have already set the visibility for JFrame why do I need in addition? @GhostCat – swinger Aug 03 '16 at 08:59
  • sorry, i overlooked the JFrame part. – GhostCat Aug 03 '16 at 09:04
  • Then the answer from @arcy is correct. But a side note: do not do all your work in your constructor! – GhostCat Aug 03 '16 at 09:05
  • Isn't already in a function for the components? Please let me inform, which point I'm wrong. @GhostCat – swinger Aug 03 '16 at 09:10
  • 1
    One way to get image(s) for an example is to hot link to images seen in [this Q&A](http://stackoverflow.com/q/19209650/418556). – Andrew Thompson Aug 03 '16 at 09:13
  • @AndrewThompson as you say I have completed the code by taking advantage of your before answer thanks but EmptyBorder seems to not work. Is there any mistake with it? – swinger Aug 03 '16 at 11:43
  • Link to a screenshot of the GUI. Note that I wrote `50` to reduce the size of the screenshot whereas you wanted a larger gap.. – Andrew Thompson Aug 03 '16 at 20:40
  • Actually I see the problem. Start a new question, but please remove the cruft from that example. An MCVE of a layout problem does not need images to be loaded, it does not require listener code.. – Andrew Thompson Aug 03 '16 at 20:42

1 Answers1

4

You are calling pack() and setVisible() before you add all the components to the JFrame. Move the block that does that to the end of the constructor.

In addition, my compiler cannot find test anywhere in the code that is setting up action listeners. I haven't worked with Java 8, so I don't know what's wrong there, but Eclipse should be able to compile this without errors.

arcy
  • 12,845
  • 12
  • 58
  • 103
  • @swinger I see you're a new user; it is customary in SO to click the checkmark on an answer if it answers your question, in addition to upvoting it if it's helpful. That way people know the question is answered, and the person who answered it gets credit. – arcy Aug 03 '16 at 09:07
  • This code displays a frame on my system. I pasted it into a new class in the default package in eclipse, changed the classname so it wouldn't conflict with the first one I got from this question, corrected the constructor, and hit "debug". It displays a window with 6 buttons. If you don't get this, it is because of something other than your code. If you have errors, tell us what they are. It would also help if you state what you expect and what you see that's different; since the first example wouldn't display anything, once this displayed something I thought we were done. – arcy Aug 03 '16 at 10:55
  • I don't understand why you've added icons to the question. What is it that you want to happen that is not happening? – arcy Aug 03 '16 at 10:59
  • _If you don't get this, it is because of something other than your code._ I have accepted because of the answer. – swinger Aug 03 '16 at 11:10
  • Do you have any idea about Emptyborder? It should put some space between two jpanels? Can you help? – swinger Aug 03 '16 at 11:38