Disappearing panel border

From: Bob H (bherbst65_at_hotmail.com)
Date: 07/30/04

  • Next message: Roedy Green: "Re: Negation of boolean, sample Jet compiled code."
    Date: 29 Jul 2004 23:22:39 -0700
    
    

    Hi All,
    The following program has 3 panels centrally located on which are
    drawn lines . However in MyPanelA the outline of the frame is missing,
    while in the other MyPanelB and MyPanelC the outline is clearly shown.

    I need to restore the outline of the frame in MyPanelA.

    Help is appreciated.

    Bob H

    //package hellowordusingdesigner;

    import javax.swing.UIManager;
    import java.awt.*;

    public class HelloWorld {
      boolean packFrame = false;

      //Construct the application
      public HelloWorld() {
        HelloWorldFrame frame = new HelloWorldFrame();
        //Validate frames that have preset sizes
        //Pack frames that have useful preferred size info, e.g. from
    their layout
        if (packFrame) {
          frame.pack();
        }
        else {
          frame.validate();
        }
        //Center the window
        Dimension screenSize =
    Toolkit.getDefaultToolkit().getScreenSize();
        Dimension frameSize = frame.getSize();
        if (frameSize.height > screenSize.height) {
          frameSize.height = screenSize.height;
        }
        if (frameSize.width > screenSize.width) {
          frameSize.width = screenSize.width;
        }
        frame.setLocation((screenSize.width - frameSize.width) / 2,
    (screenSize.height - frameSize.height) / 2);
        frame.setVisible(true);
      }
      //Main method
      public static void main(String[] args) {
        try {
          UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
        }
        catch(Exception e) {
          e.printStackTrace();
        }
        new HelloWorld();
      }
    }

    //package hellowordusingdesigner;

    import java.awt.*;
    import java.awt.event.*;
    import javax.swing.*;
    import javax.swing.border.*;

    public class HelloWorldFrame
      extends JFrame {
      JPanel jpnlDisplay;
      BorderLayout borderLayout1 = new BorderLayout();
      JPanel jPanel1 = new JPanel();
      JLabel jlblTitle = new JLabel();
      JLabel jlblHelloWorld = new JLabel();
      JButton jbtnSayHello = new JButton();
      JButton jbtnExit = new JButton();
      
      private TitledBorder titledBorder1;
      private BorderLayout borderLayout2 = new BorderLayout();
      //private JPanel jPnlOnTop = new JPanel();
      //private JPanel jPnlMiddle = new JPanel();
      //private JPanel jPnlBack = new JPanel();
      private GridLayout gridLayout1 = new GridLayout();
      private MyPanelA jPnlMiddle = new MyPanelA();
      private MyPanelB jPnlOnTop = new MyPanelB();
      private MyPanelC jPnlBack = new MyPanelC();
      
      
      //Construct the frame
      public HelloWorldFrame() {
        enableEvents(AWTEvent.WINDOW_EVENT_MASK);
        try {
          jbInit();
        }
        catch (Exception e) {
          e.printStackTrace();
        }
      }
      
      //Component initialization
      private void jbInit() throws Exception {
        jpnlDisplay = (JPanel)this.getContentPane();
        titledBorder1 = new TitledBorder("");
        jpnlDisplay.setLayout(borderLayout1);
        this.setSize(new Dimension(400, 300));
        this.setTitle("HeloWld");
        jPanel1.setLayout(null);
        jlblTitle.setFont(new java.awt.Font("SansSerif", 1, 16));
        jlblTitle.setForeground(Color.blue);
        jlblTitle.setAlignmentY( (float) 0.5);
        jlblTitle.setText("My First Java Application");
        jlblTitle.setBounds(new Rectangle(113, 25, 235, 18));
        jlblHelloWorld.setFont(new java.awt.Font("SansSerif", 0, 24));
        jlblHelloWorld.setForeground(Color.red);
        jlblHelloWorld.setText("Hello World from Java");
        jlblHelloWorld.setBounds(new Rectangle(93, 148, 250, 18));
        jbtnSayHello.setBounds(new Rectangle(71, 234, 130, 27));
        jbtnSayHello.setText("Say Hello Java");
        jbtnSayHello.addActionListener(new
                                        
    HelloWorldFrame_jbtnSayHello_actionAdapter(this));
        jbtnExit.setBounds(new Rectangle(220, 234, 130, 27));
        jlblHelloWorld.setVisible(false);
        jbtnExit.setText("Exit");
        jbtnExit.addActionListener(new
    HelloWorldFrame_jbtnExit_actionAdapter(this));
        jPnlMiddle.setBorder(BorderFactory.createEtchedBorder());
        jPnlMiddle.setBounds(new Rectangle(12, 63, 152, 47));
        jPnlMiddle.setLayout(borderLayout2);
        jPnlOnTop.setBorder(BorderFactory.createLoweredBevelBorder());
        jPnlOnTop.setPreferredSize(new Dimension(100, 100));
        jPnlOnTop.setBounds(new Rectangle(87, 90, 100, 43));
        jPnlBack.setBorder(BorderFactory.createRaisedBevelBorder());
        jPnlBack.setBounds(new Rectangle(138, 56, 113, 68));
        jPnlBack.setLayout(gridLayout1);
        jpnlDisplay.add(jPanel1, BorderLayout.CENTER);
        jPanel1.add(jPnlOnTop, null);
        jPanel1.add(jPnlMiddle, null);
        jPanel1.add(jbtnSayHello, null);
        jPanel1.add(jbtnExit, null);
        jPanel1.add(jlblHelloWorld, null);
        jPanel1.add(jlblTitle, null);
        jPanel1.add(jPnlBack, null);
        
      }
      
      //Overridden so we can exit when window is closed
      protected void processWindowEvent(WindowEvent e) {
        super.processWindowEvent(e);
        if (e.getID() == WindowEvent.WINDOW_CLOSING) {
          System.exit(0);
        }
      }
      
      void jbtnExit_actionPerformed(ActionEvent e) {
        System.exit(0);
      }
      
      void jbtnSayHello_actionPerformed(ActionEvent e) {
        if (jlblHelloWorld.isVisible() == false) {
          jlblHelloWorld.setVisible(true);
          
        }
      }
    }

    class HelloWorldFrame_jbtnExit_actionAdapter
      implements java.awt.event.ActionListener {
      HelloWorldFrame adaptee;
      
      HelloWorldFrame_jbtnExit_actionAdapter(HelloWorldFrame adaptee) {
        this.adaptee = adaptee;
      }
      
      public void actionPerformed(ActionEvent e) {
        adaptee.jbtnExit_actionPerformed(e);
      }
    }

    class HelloWorldFrame_jbtnSayHello_actionAdapter
      implements java.awt.event.ActionListener {
      HelloWorldFrame adaptee;
      
      HelloWorldFrame_jbtnSayHello_actionAdapter(HelloWorldFrame adaptee)
    {
        this.adaptee = adaptee;
      }
      
      public void actionPerformed(ActionEvent e) {
        adaptee.jbtnSayHello_actionPerformed(e);
      }
    }

    import javax.swing.*;
    import java.awt.*;
    /**
     *
     * @author zzynx
     */
    public class MyPanelA extends JPanel {
       /** Creates a new instance of MyPanel */
      public MyPanelA() {
      }
       public void paint(Graphics g) {
        super.paintComponent(g);
        // a hexagon
        // since it is a circular motion here calling an extra (6 + 1) to
    close up the figure
        int HexaPts = 7;
        int HexaPtsX[] = new int[HexaPts];
        int HexaPtsY[] = new int[HexaPts];
        g.setColor(Color.magenta);
        int rad = 15; // radius of circle containing hexagon
        int x1, y1;
        for (int i = 0; i < HexaPts; i++) {
          x1 = (int) (rad * Math.cos((2 * Math.PI / 6)*(0.5+i)));
          y1 = (int) (rad * Math.sin((2 * Math.PI / 6)*(0.5+i)));
          HexaPtsX[i]=x1;
          HexaPtsY[i]=y1;
        }
        int a = 24;// horizontal position on this row.
        int id = 24;
        for (int ib = 1; ib < 3; ib++) {
          for (int ic = 0; ic < HexaPts - 1; ic++) { // six sides
            g.drawLine(0+a*ib + HexaPtsX[ic], 0+1*id + HexaPtsY[ic],
                       0+a*ib + HexaPtsX[ic + 1], 0+1*id + HexaPtsY[ic +
    1]);
          }
        }
      }
    }

    /*
     // import javax.swing.*;
     // import java.awt.*;
    /**
     *
     * @author zzynx
     */
    //public class MyPanelA extends JPanel {
        /** Creates a new instance of MyPanel */
      // public MyPanelA() {
      // }
      // public void paintComponent(Graphics g) {
      // super.paintComponent(g);
      // g.setColor(Color.green);
       // g.drawLine(5,5,30,5);
      // }
    //}

    import javax.swing.*;
    import java.awt.*;
    /**
     *
     * @author zzynx
     */
    public class MyPanelB extends JPanel {
        /** Creates a new instance of MyPanel */
        public MyPanelB() {
        }
        public void paintComponent(Graphics g) {
             super.paintComponent(g);
             g.setColor(Color.green);
             g.drawLine(5,5,30,5);
        }
    }

    import javax.swing.*;
    import java.awt.*;
    /**
     *
     * @author zzynx
     */
    public class MyPanelC extends JPanel {
        /** Creates a new instance of MyPanel */
        public MyPanelC() {
        }
        public void paintComponent(Graphics g) {
             super.paintComponent(g);
             g.setColor(Color.blue);
             g.drawLine(5,5,30,5);
        }
    }


  • Next message: Roedy Green: "Re: Negation of boolean, sample Jet compiled code."
  • Quantcast