Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 20 of 20

Thread: Layout in java

  1. #1
    Junior Member
    Join Date
    Feb 2022
    Posts
    24
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Layout in java

    Hi everyone I'm new to java and are making a big project for the first time therefor I want to creative some menu's. In this conetext I'm using borderlayout. In my SOUTH reagion, I have a gridlayout with some buttons and info boxes, this is working fine. In my CENTER region I have my "canvas" where I use Graphics to draw, this is where my game is drawed. In my EAST region, I'm trying to use BoxLayout to make another menu, so far I have added a headline, a gridLayout with a lot of bottun and another headline. This gridlayout keeps bugging for me and I dont know what to do. Here are my code:

    public class VectorTD {
     
        public static GameFrame frame;
     
        public static void main(String[] args) {
     
            // Creates the display
            frame = new GameFrame();
     
        }
    }



    import java.awt.*;
    import javax.swing.*;
    import java.awt.Graphics;
     
    public class GamePanel extends JPanel implements Runnable {
     
        static Graphics graphics;
        final static int FPS = 100;
        final static public long SKIP_TICKS = 1000000000 / FPS;
     
        static public int lives = 25;
        static public int money = 100;
        static public int round = 0;
        static public int interest = 3;
     
     
        static public boolean roundStart = false;
     
        GamePanel() {
            new Thread(this).start();
            Square.makeGrid();
            Vectoid.makeVectoids();
        }
     
     
        public void paint(Graphics g) {
            Image image = createImage(GameFrame.SCREEN_WIDTH, GameFrame.SCREEN_HEIGHT);
            graphics = image.getGraphics();
            draw(graphics);
            g.drawImage(image, 0, 0, this);
     
        }
     
        public void draw(Graphics g) {
            Square.drawGrid(g);
            Vectoid.drawVectoids(g);
            Vectoid.spawnVectoids();
            Toolkit.getDefaultToolkit().sync();
        }
     
     
     
     
        // Gameloop
        public void run() {
            // long then = System.nanoTime();
            // The gameloop runs until the program is closed
            while (true) {
                // Saves the current time
                long now = System.nanoTime();
     
                repaint();
     
                // After the game has been updated, the program needs to wait until next
                // gameupdate. This waiting time is determined by the FPS variable.
                long sleeptime = SKIP_TICKS - (System.nanoTime() - now);
                if (sleeptime >= 0) {
                    try {
                        Thread.sleep(sleeptime / 1000000);
                    } catch (Exception e) {
                        System.out.println("Error: Can't sleep.");
                    }
                }
                // If sleeptime < 0, the program is not updated fast enough
                else {
                    System.out.println("We are running behind");
                }
            }
        }
     
    }


    import java.awt.*;
    import java.awt.Color;
     
    public class Square extends Rectangle {
     
      static int rows = 15;
      static int collums = 15;
     
      public static Square grid[][] = new Square[rows][collums];
      public static int[][] makeMap = { { 0, 0 }, { 5, 0 }, { 5, 5 }, { 3, 5 }, { 3, 3 }, { 0, 3 }, { 0, 10 }, { 10, 10 },
          { 10, 5 }, { 14, 5 } };
     
      int x, y;
      static int width = 40;
      int strokeWeigth = 2;
     
      Color bodyColor = new Color(29, 69, 64);
      Color strokeColor = new Color(0, 0, 0);
      boolean isTowerPlacebel = true;
     
      public static int[][] vectoidRoute;
     
      Square(int x, int y) {
        this.x = x;
        this.y = y;
      }
     
      private void draw(Graphics g) {
        g.setColor(strokeColor);
        g.fillRect(x, y, width, width);
        g.setColor(bodyColor);
        g.fillRect(x + strokeWeigth, y + strokeWeigth, width - strokeWeigth * 2, width - strokeWeigth * 2);
      }
     
      public static void makeGrid() {
        for (int i = 0; i < collums; i++) {
          for (int j = 0; j < rows; j++) {
     
            grid[i][j] = new Square(i * width, j * width);
          }
        }
        makeMap();
        calculateRouteLength();
        makeVectoidRoute();
      }
     
      public static void drawGrid(Graphics g) {
        for (int i = 0; i < collums; i++) {
          for (int j = 0; j < rows; j++) {
            grid[i][j].draw(g);
          }
        }
      }
     
      private static void makeMap() {
     
        for (int i = 1; i < makeMap.length; i++) {
     
          if (makeMap[i - 1][0] < makeMap[i][0]) {
     
            for (int j = makeMap[i - 1][0]; j <= makeMap[i][0]; j++) {
     
              grid[j][makeMap[i][1]].isTowerPlacebel = false;
              grid[j][makeMap[i][1]].bodyColor = new Color(0, 41, 0);
            }
          } else if (makeMap[i - 1][1] < makeMap[i][1]) {
     
            for (int j = makeMap[i - 1][1]; j <= makeMap[i][1]; j++) {
     
              grid[makeMap[i][0]][j].isTowerPlacebel = false;
              grid[makeMap[i][0]][j].bodyColor = new Color(0, 41, 0);
            }
     
          } else if (makeMap[i - 1][0] > makeMap[i][0]) {
     
            for (int j = makeMap[i - 1][0]; j >= makeMap[i][0]; j--) {
     
              grid[j][makeMap[i][1]].isTowerPlacebel = false;
              grid[j][makeMap[i][1]].bodyColor = new Color(0, 41, 0);
            }
     
          } else if (makeMap[i - 1][1] > makeMap[i][1]) {
     
            for (int j = makeMap[i - 1][1]; j >= makeMap[i][1]; j--) {
     
              grid[makeMap[i][0]][j].isTowerPlacebel = false;
              grid[makeMap[i][0]][j].bodyColor = new Color(0, 41, 0);
            }
          }
        }
      }
     
      private static void calculateRouteLength() {
        int length = 0;
     
        for (int i = 1; i < makeMap.length; i++) {
     
          length += Math.abs(grid[makeMap[i][0]][makeMap[i][1]].x - grid[makeMap[i - 1][0]][makeMap[i - 1][1]].x);
          length += Math.abs(grid[makeMap[i][0]][makeMap[i][1]].y - grid[makeMap[i - 1][0]][makeMap[i - 1][1]].y);
     
        }
        length += width * 2;
     
        vectoidRoute = new int[length + 1][2];
      }
     
      private static void makeVectoidRoute() {
     
        int number = 0;
        int x2 = 0;
        int y2 = 0;
     
        for (int i = 1; i < makeMap.length; i++) {
     
          int x1 = grid[makeMap[i - 1][0]][makeMap[i - 1][1]].x;
          int y1 = grid[makeMap[i - 1][0]][makeMap[i - 1][1]].y;
     
          x2 = grid[makeMap[i][0]][makeMap[i][1]].x;
          y2 = grid[makeMap[i][0]][makeMap[i][1]].y;
     
          int xDist = x2 - x1;
          int yDist = y2 - y1;
     
          if (i == 1) {
     
            if (xDist > 0) {
              x1 -= width;
              xDist += width;
            } else if (xDist < 0) {
     
              x1 += width;
              xDist -= width;
            } else if (yDist > 0) {
              y1 -= width;
              yDist += width;
     
            } else {
              y1 += width;
              yDist -= width;
            }
          } else if (i == makeMap.length - 1) {
     
            if (xDist > 0) {
              x2 += width;
              xDist += width;
            } else if (xDist < 0) {
     
              x2 -= width;
              xDist -= width;
            } else if (yDist > 0) {
              y2 += width;
              yDist += width;
     
            } else {
              y2 -= width;
              yDist -= width;
            }
          }
     
          if (xDist > 0 || yDist > 0) {
     
            for (int j = 0; j != xDist + yDist; j++) {
     
              if (yDist == 0) {
                vectoidRoute[number][0] = x1 + j;
                vectoidRoute[number][1] = y1;
     
              } else {
                vectoidRoute[number][0] = x1;
                vectoidRoute[number][1] = y1 + j;
     
              }
              number++;
            }
          } else {
     
            for (int j = 0; j != Math.abs(xDist + yDist); j++) {
     
              if (yDist == 0) {
                vectoidRoute[number][0] = x1 - j;
                vectoidRoute[number][1] = y1;
     
              } else {
                vectoidRoute[number][0] = x1;
                vectoidRoute[number][1] = y1 - j;
     
              }
              number++;
            }
          }
     
        }
     
        vectoidRoute[number][0] = x2;
        vectoidRoute[number][1] = y2;
     
      }
     
    }


     
    import java.awt.*;
    import java.awt.Color;
     
    public class Vectoid {
     
        static int maxNumberOfVectoids = 10;
        static int currentNumberOfVectoids = 0;
        public static Vectoid listOfVectoids[] = new Vectoid[10];
        static int countDead = 0;
     
        int radius = 50;
        float distance = 0;
        int x = Square.vectoidRoute[Math.round(distance)][0];
        int y = Square.vectoidRoute[Math.round(distance)][1];
        float speed = 3;
        int slowTime = 0;
        float slowSpeed = 0;
        int health = 100;
        Color bodyColor = new Color(255, 0, 0);
        boolean dead = false;
        static long timer;
     
        public void draw(Graphics g) {
            x = Square.vectoidRoute[Math.round(distance)][0];
            y = Square.vectoidRoute[Math.round(distance)][1];
            g.setColor(bodyColor);
            g.fillOval(x, y, radius, radius);
        }
     
        public void move() {
            distance += speed;
            outOfMap();
        }
     
        private void outOfMap() {
            if (distance >= Square.vectoidRoute.length) {
                dead = true;
                GamePanel.lives -= 1;
                countDead++;
                VectorTD.frame.livesLabel.setText("Lives: " + GamePanel.lives);
                if (countDead == maxNumberOfVectoids) {
                    GamePanel.roundStart = false;
     
                }
     
            }
        }
     
        public void takeDamage(int damage) {
            health -= damage;
     
            if (health <= 0) {
                dead = true;
     
                GamePanel.money += 10;
            }
        }
     
        public static void drawVectoids(Graphics g) {
     
            if (GamePanel.roundStart == true) {
                for (int i = 0; i < Vectoid.currentNumberOfVectoids; i++) {
                    if (listOfVectoids[i].dead == false) {
                        listOfVectoids[i].draw(g);
                        listOfVectoids[i].move();
                    }
                }
            }
     
        }
     
        public static void makeVectoids() {
            for (int i = 0; i < maxNumberOfVectoids; i++) {
                listOfVectoids[i] = new Vectoid();
            }
        }
     
        public static void spawnVectoids() {
            if ((currentNumberOfVectoids < maxNumberOfVectoids) && (System.nanoTime() - timer) > 300000000) {
                listOfVectoids[currentNumberOfVectoids].dead = false;
                listOfVectoids[currentNumberOfVectoids].health = 100;
                listOfVectoids[currentNumberOfVectoids].distance = 0;
                currentNumberOfVectoids++;
                timer = System.nanoTime();
     
            }
     
        }
     
        public static void newRound() {
            if (GamePanel.roundStart == false) {
                GamePanel.roundStart = true;
                currentNumberOfVectoids = 0;
                timer = System.nanoTime();
            }
        }
    }


     
    //pakke der styrer farver
    import java.awt.Color;
    import javax.swing.*;
    //Pakke der hjælper med at lave vinduet
    import javax.swing.JFrame;
    import javax.swing.JButton;
    import java.awt.*;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    import javax.swing.BoxLayout;
     
    public class GameFrame extends JFrame {
     
        static final int SCREEN_WIDTH = 1100;
        static final int SCREEN_HEIGHT = 800;
     
        final JLabel moneyLabel = new JLabel("Money: " + GamePanel.money + "$", SwingConstants.CENTER);
        final JLabel livesLabel = new JLabel("Lives: " + GamePanel.lives, SwingConstants.CENTER);
        final JLabel roundLabel = new JLabel("Round: " + GamePanel.round, SwingConstants.CENTER);
        final JLabel interestLabel = new JLabel("Interest: " + GamePanel.interest + "%", SwingConstants.CENTER);
     
        JPanel topMenu;
        JPanel screen;
     
        GameFrame() {
     
            screen = new JPanel();
            screen.setLayout(new BorderLayout());
     
            // adding top menu
            addTopMenu();
            // adding game
            screen.add(new GamePanel(), BorderLayout.CENTER);
            // adding side menu
            addSideMenu();
     
            this.setContentPane(screen);
     
            // Stopper programmet når vinduet lukkes
            setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
     
            // Vinduets størrelse
            setSize(SCREEN_WIDTH, SCREEN_HEIGHT);
     
            // Vinduets baggrundsfarve
            this.setBackground(Color.BLACK);
     
            // Viser vinduet
            setVisible(true);
     
            // Gør at man ikke kan ændre størrelse på vinduet
            setResizable(false);
     
        }
     
        void addSideMenu() {
            JPanel sideMenu = new JPanel();
            sideMenu.setLayout(new BoxLayout(sideMenu, BoxLayout.Y_AXIS));
            //sideMenu.CENTER_ALIGNMENT;
     
            sideMenu.setBackground(Color.BLACK);
     
            JPanel towerMenu = new JPanel();
            towerMenu.setLayout(new GridLayout(2, 4, 0, 0));
            towerMenu.setBackground(Color.BLACK);
     
            JButton Green_Laser_Mk1 = new JButton(new ImageIcon("Images/Green_Laser_Mk1.png"));
            towerMenu.add(Green_Laser_Mk1);
     
            JButton Purple_Power_Mk1 = new JButton(new ImageIcon("Images/Purple_Power_Mk1.png"));
            towerMenu.add(Purple_Power_Mk1);
     
            JButton Orange_Incinerator_Mk1 = new JButton(new ImageIcon("Images/Orange_Incinerator_Mk1.png"));
            towerMenu.add(Orange_Incinerator_Mk1);
     
            JButton Blue_Rays_Mk1 = new JButton(new ImageIcon("Images/Blue_Rays_Mk1.png"));
            towerMenu.add(Blue_Rays_Mk1);
     
            JButton Green_Laser_Mk2 = new JButton(new ImageIcon("Images/Green_Laser_Mk2.png"));
            towerMenu.add(Green_Laser_Mk2);
     
            JButton Purple_Power_Mk2 = new JButton(new ImageIcon("Images/Purple_Power_Mk2.png"));
            towerMenu.add(Purple_Power_Mk2);
     
            JButton Orange_Incinerator_Mk2 = new JButton(new ImageIcon("Images/Orange_Incinerator_Mk2.png"));
            towerMenu.add(Orange_Incinerator_Mk2);
     
            JButton Blue_Rays_Mk2 = new JButton(new ImageIcon("Images/Blue_Rays_Mk2.png"));
            towerMenu.add(Blue_Rays_Mk2);
     
            final JLabel towerMenuName = new JLabel("Towers");
            towerMenuName.setForeground(Color.white);
            towerMenuName.setBackground(new Color(4, 23, 22));
            towerMenuName.setFont(new Font("Verdana", Font.PLAIN, 25));
     
            final JLabel towerInformation = new JLabel("Orange_Incinerator_Mk1", SwingConstants.CENTER);
            towerInformation.setForeground(Color.white);
            towerInformation.setBackground(new Color(4, 23, 22));
            towerInformation.setFont(new Font("Verdana", Font.PLAIN, 25));
     
            sideMenu.add(towerMenuName);
            sideMenu.add(towerMenu);
            sideMenu.add(towerInformation);
     
            screen.add(sideMenu, BorderLayout.EAST);
            //screen.add(towerMenu, BorderLayout.EAST);
        }
     
        void addTopMenu() {
            topMenu = new JPanel();
            topMenu.setLayout(new GridLayout(2, 3, 10, 10));
            topMenu.setBackground(Color.BLACK);
     
     
            JButton roundStart = new JButton("Next round");
            roundStart.setBackground(new Color(245, 28, 92));
            roundStart.setFocusPainted(false);
     
            roundStart.addActionListener(new ActionListener() {
                public void actionPerformed(ActionEvent event) {
                    Vectoid.newRound();
     
                }
            });
     
            JButton autoStart = new JButton("Auto start: " + GamePanel.roundStart);
            autoStart.setBackground(new Color(245, 28, 92));
            autoStart.setFocusPainted(false);
     
            addLabel(moneyLabel);
            addLabel(livesLabel);
            topMenu.add(roundStart);
            addLabel(roundLabel);
            addLabel(interestLabel);
            topMenu.add(autoStart);
     
            screen.add(topMenu, BorderLayout.NORTH);
        }
     
        void addLabel(JLabel jLabel) {
            jLabel.setOpaque(true);
            jLabel.setForeground(Color.white);
            jLabel.setBackground(new Color(4, 23, 22));
            topMenu.add(jLabel);
        }
     
    }

    Also I have a folder with images.

  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Layout in java

    gridlayout keeps bugging for me and I dont know what to do
    Please explain.
    If you don't understand my answer, don't ignore it, ask a question.

  3. #3
    Junior Member
    Join Date
    Feb 2022
    Posts
    24
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Layout in java

    I keeps going over my CENTER region, and my jLabelels are not centeret.

    Is it possible to upload images?

  4. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Layout in java

    Not sure what your problem is. I get a large rectangle with a grid-path on the left underneath two rows of black labels
    and several rows on the right. The top 2 rows on the right have red buttons. Beneath those two rows are two rows with four columns each of images.
    The bottom row has a text label.
    If you don't understand my answer, don't ignore it, ask a question.

  5. #5
    Junior Member
    Join Date
    Feb 2022
    Posts
    24
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Layout in java

    First and foremost I would like the EAST menu to be exacly as big is my pictures. Then I want my EAST menu to be very close to my canvas (almost touching). Third i want my jLabels in my EAST menu to be centeret realetive to my EAST menu. I have send a link to the game I'm taking inpiration from. (If you think the link looks wierd it becuase I'm from Denmark). Best regards Gustav

    https://www.crazygames.dk/spil/vector-td


    Little note I also have some troubles drawing images with Graphics though I might just ask now that I'm in dialoge with someone who has knowlegde in java.

  6. #6
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Layout in java

    As I have said before, I am not very good at GUI layouts. The only way I know to get the GUI to be exactly where you want it to be is to use the setBounds method for the components.
    There are more experienced java programmers on this site: http://www.coderanch.com/forums
    Try asking your question there.
    If you don't understand my answer, don't ignore it, ask a question.

  7. #7
    Junior Member
    Join Date
    Feb 2022
    Posts
    24
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Layout in java

    Do you know how to display image using Graphics?

  8. #8
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Layout in java

    Use the Graphics class's drawImage method.
    If you don't understand my answer, don't ignore it, ask a question.

  9. #9
    Junior Member
    Join Date
    Feb 2022
    Posts
    24
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Layout in java

    Yeah but how do refrence the image?

  10. #10
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Layout in java

    The same way as any other value, by using a variable.

    There are lots of examples of code that uses the drawImage method. Do a Search for code samples.
    If you don't understand my answer, don't ignore it, ask a question.

  11. #11
    Junior Member
    Join Date
    Feb 2022
    Posts
    24
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Layout in java

    Is there a way to make line drawed with drawLine thicker?

  12. #12
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Layout in java

    Did you Google it?
    Here is one I found: https://www.rgagnon.com/javadetails/java-0260.html
    If you don't understand my answer, don't ignore it, ask a question.

  13. #13
    Junior Member
    Join Date
    Feb 2022
    Posts
    24
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Layout in java

    Alright thanks this works.

    Do you know how to store a refrence to a class?

  14. #14
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Layout in java

    What do you mean by a "class"?
    What is it that you want to store?
    Where do you want to store it?

    For example:
       String str = new String();  //  store reference to instance of String class in the variable str
    If you don't understand my answer, don't ignore it, ask a question.

  15. #15
    Junior Member
    Join Date
    Feb 2022
    Posts
    24
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Layout in java

    What do you mean by a "class"? I have alle these different towers (each is its own class)

    Where do you want to store it? In a varible

    What is it that you want to store? What I want to store is a refrence to the class. So when I call the varible i'm able to create a new instance of the class

  16. #16
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Layout in java

    Your posted code is full of variables that hold references to instances of classes that are created by using new.
    I am confused about what you are asking.

    when I call the varible i'm able to create a new instance of the class
    You do not call variables. You call methods.
    A new instance of a class is created by using new: ClassName variable = new ClassName();
    If you don't understand my answer, don't ignore it, ask a question.

  17. #17
    Junior Member
    Join Date
    Feb 2022
    Posts
    24
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Layout in java

    What I want is somehow to store "ClassName" in a varible or something. So when I use the varible I'm able to create a new ClassName

    ClassName variable = new ClassName();

  18. #18
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Layout in java

    when I use the varible I'm able to create a new ClassName
    I do not know what that means.
    This statement uses variable:
     another = variable + 10;
    How would that usage of variable create an instance of the class: ClassName?
    An instance of a class is created by using the keyword: new
    If you don't understand my answer, don't ignore it, ask a question.

  19. #19
    Junior Member
    Join Date
    Feb 2022
    Posts
    24
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Layout in java

    We have 2 classed Dummy1 and Dummy2

    What I want is a vairble that "stores" those classes like so:

    "classholder" ranodmClass = Dummy1 or "classholder" randomClass = Dummy2

    Now I can use randomClass test = new randomClass to create either a object of Dummy1 or Dummy2

  20. #20
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Layout in java

    I think that can be done by using Reflection. See the java.lang.reflect package. It has classes and methods to allow you to get a class's name from a variable and to use that to create a new instance of the class.

    However reflection is not very flexible.

    In your example test would refer to an instance of some class. How does the compiler know what class is referred to so that it can check for proper use of that variable for accessing methods in that class instance that was created?
    If you don't understand my answer, don't ignore it, ask a question.

Similar Threads

  1. [SOLVED] The layout of GUI
    By rx78mk2 in forum AWT / Java Swing
    Replies: 6
    Last Post: December 22nd, 2013, 08:07 PM
  2. GUI Layout - Does anyone know how to set a layout?
    By mikemontesa in forum What's Wrong With My Code?
    Replies: 1
    Last Post: November 20th, 2013, 04:35 PM
  3. Creating table with layout in excel through java code
    By kvangala in forum What's Wrong With My Code?
    Replies: 1
    Last Post: December 26th, 2012, 01:07 PM
  4. Replies: 1
    Last Post: April 14th, 2011, 07:50 AM
  5. Grid bag layout inside grid bag layout
    By kiddkoder in forum What's Wrong With My Code?
    Replies: 1
    Last Post: March 29th, 2011, 08:07 AM