Page 1 of 2 12 LastLast
Results 1 to 20 of 22
  1. #1
    Unknown is offline Member
    Join Date
    Dec 2008
    Posts
    15
    Rep Power
    0

    Default GUI Final Project help

    Hi, I'm have to do a final project for java that I'd really like some help on.

    Here is the source code:

    import javax.swing.*;
    import javax.swing.border.*;
    import javax.swing.BorderFactory;
    import java.awt.*;
    import java.awt.Color;
    import java.awt.event.*;
    import java.util.*;
    import java.awt.Image;


    public class FinalProject implements ActionListener
    {


    public static void main(String [] args){



    Font font = new Font("Arial", Font.PLAIN, 72);
    Font font2 = new Font("Arial", Font.ITALIC, 30);


    JFrame frame = new JFrame ("Nintendo Database");
    frame.setSize(800,350);
    frame.setLocation (150,150);
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOS E);




    JPanel panel = new JPanel (new GridLayout(5,1));
    frame.setContentPane(panel);



    JLabel title=new JLabel("Nintendo Database");
    title.setFont(font);
    title.setHorizontalAlignment(JLabel.CENTER);
    title.setForeground(Color.WHITE);
    title.setBackground(Color.BLACK);
    title.setOpaque(true);
    // title.setBorder(BorderFactory.createLineBorder (Color.WHITE));
    panel.add(title,BorderLayout.CENTER);


    JMenuBar menuBar = new JMenuBar();
    frame.setJMenuBar(menuBar);
    JMenu menu = new JMenu ("File");
    menuBar.add(menu);
    JMenuItem menuItem = new JMenuItem("Clear");
    menu.add(menuItem);
    JMenuItem menuItem1 = new JMenuItem("Exit");
    menu.add(menuItem1);





    JLabel info=new JLabel("About");
    info.setFont(font2);
    info.setForeground(Color.BLUE);
    title.setBackground(Color.RED);
    info.setOpaque(true);
    panel.add(info,BorderLayout.WEST);
    //JLabel text=new



    /* JLabel pic=new JLabel("Picture");
    pic.setFont(font2);
    pic.setForeground(Color.BLACK);
    pic.setBackground(Color.WHITE);
    pic.setOpaque(true);
    panel.add(pic,BorderLayout.EAST);
    */

    JLabel buttons=new JLabel("Menu");
    // buttons.setHorizontalAlignment(JLabel.CENTER);
    buttons.setForeground(Color.BLACK);
    buttons.setBackground(Color.WHITE);
    buttons.setOpaque(true);
    panel.add(buttons, BorderLayout.SOUTH);


    JButton exitButton;
    exitButton = new JButton("Exit");
    exitButton.addActionListener(this);
    exitButton.setHorizontalAlignment(JButton.LEADING) ;
    panel.add(exitButton, BorderLayout.SOUTH);




    frame.setContentPane(panel);
    frame.setVisible(true);
    }


    public void actionPerformed (ActionEvent event)
    {
    System.exit(0);
    }

    }

    Here are some problems that I've come across and need help on:
    - the exit button seems to take up the whole window, how can I adjust its size
    -the exit button won't actually exit, when I compile it, it says "non static variable this cannot be referenced in static content".
    -how do I add images in this
    -part of the project tells me to use the menu bar to flip through pages of information, how would this actually work?
    -how do I place text in here?

    Thanks to anyone who answers any of these questions

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    You'll want to read the Sun tutorial on how to use Layout managers to be able to use them more effectively. Currently you have a JPanel that uses one layout, GridLayout(5, 1) and then place all your components on this JPanel using this layout, and doing it this way is using a pretty blunt instrument. Instead you want to use several JPanels each using it's own layout, and nest some of these JPanels in each other thereby achieving a more sophisticated look for your app. For instance your Exit button could easily be placed in its own JPanel that uses its own layout, say for instance a FlowLayout.

    Regardless, read the layout section and play around with the ideas you've gained from the tutorial (except I'd advise you to avoid the GridBagLayout for now -- it's a PIA).
    Last edited by Fubarable; 12-20-2008 at 05:48 AM.

  3. #3
    Unknown is offline Member
    Join Date
    Dec 2008
    Posts
    15
    Rep Power
    0

    Default

    Can someone tell me how to implement JTextArea? I need to display information in my program but I don't know how? And if its possible explain the BoxLayout, I want to use that but I don't really understand what the website is saying

  4. #4
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    um,... you're welcome, ... I think ....

    What specific questions do you have about JTextArea or BoxLayout? Your questions are quite general, and there's really not much any of us can type here that would be as thorough or well-written as the tutorials. But if on the other hand you had a specific question about something in the tutorials, then I / we may be able to better help you. My own suggestion is to put your current project aside and just play with code that uses JTextArea to get a feel for it. Then do the same for BoxLayout. Once you feel comfortable with them, then put them in your assignment.

    Best of luck.

  5. #5
    Unknown is offline Member
    Join Date
    Dec 2008
    Posts
    15
    Rep Power
    0

    Default

    I'm very sorry that I forgot to thank you forgive me, I'm forgetful. The question that I have about the JTextArea is how I actually call the method.

  6. #6
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    you're welcome. As for JTextArea, first off, it's not a method, but rather is a component and so it is not "called" per se. Instead you create a JTextArea variable, initialize it by calling one of its constructors, and then place it in a container usually with the help of a layout. How you construct it depends on several things, especially what size you want it. If you haven't done so, check out the JTextArea API that lists the constructors that are available. I usually use one to set the rows and columns, and I usually set it in a JScrollPane.

  7. #7
    Unknown is offline Member
    Join Date
    Dec 2008
    Posts
    15
    Rep Power
    0

    Default

    I think I have a basic understanding of it and I'll try to create a separate program for it. But, if its not too much trouble can you post an example for me, please. I'm looking at the website but can't seem to find one. Thanks in advances.

  8. #8
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    an example? hm, here's a quick and dirty conversion utility I wrote to extract information from a procedure report that was loaded on the clipboard. It also uses BoxLayout as one of its layouts. Please be warned that this is not meant to be used by anyone but me, so the code is not very robust.
    Java Code:
    import java.awt.BorderLayout;
    import java.awt.Color;
    import java.awt.GridLayout;
    import java.awt.Toolkit;
    import java.awt.Window;
    import java.awt.datatransfer.Clipboard;
    import java.awt.datatransfer.StringSelection;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    
    import javax.swing.BorderFactory;
    import javax.swing.BoxLayout;
    import javax.swing.JButton;
    import javax.swing.JFrame;
    import javax.swing.JLabel;
    import javax.swing.JPanel;
    import javax.swing.JScrollPane;
    import javax.swing.JTextArea;
    
    public class ProcedureFindings2
    {
        private static final int TA_COLUMNS = 50;
        private static final int TA_ROWS = 20;
        private static final String CONVERT = "Convert";
        private static final String TO_CLIPBOARD = "Copy to ClipBoard";
        private static final String EXIT = "Exit";
        private JPanel mainPanel = new JPanel();
        
        // two JTextAreas constructed with row and column parameters
        private JTextArea preArea = new JTextArea(TA_ROWS, TA_COLUMNS);
        private JTextArea postArea = new JTextArea(TA_ROWS, TA_COLUMNS);
        
        public ProcedureFindings2()
        {
            JPanel fieldPanel = createFieldPanel();
            JPanel btnPanel = createButtonPanel();
            
            mainPanel.setBorder(BorderFactory.createEmptyBorder(10, 10, 10, 10));
            mainPanel.setLayout(new BorderLayout(10, 20));
            mainPanel.add(fieldPanel, BorderLayout.CENTER);
            mainPanel.add(btnPanel, BorderLayout.SOUTH);
        }
    
        private JPanel createButtonPanel()
        {
            JButton convertText = new JButton(CONVERT);
            JButton copyToClip = new JButton(TO_CLIPBOARD);
            JButton exitBtn = new JButton(EXIT);
            exitBtn.setForeground(new Color(200, 0, 0));
            convertText.addActionListener(new BtnListener());
            copyToClip.addActionListener(new BtnListener());
            exitBtn.addActionListener(new BtnListener());
            JPanel innerBtnPanel = new JPanel(new GridLayout(1, 0, 10, 0));
            innerBtnPanel.add(convertText);
            innerBtnPanel.add(copyToClip);
            innerBtnPanel.add(exitBtn);
            JPanel btnPanel = new JPanel();
            btnPanel.add(innerBtnPanel);
            return btnPanel;
        }
        
        private class BtnListener implements ActionListener
        {
            public void actionPerformed(ActionEvent e)
            {
                String cmd = e.getActionCommand();
    
                if (cmd.equals(CONVERT))
                {
                    String preString = preArea.getText();
                    if (!preString.isEmpty())
                    {
                        preString = preString.trim();
                        preString = preString.replaceAll("                     ",
                            "");
                        preString = preString.replaceAll("          ", " ");
                        preString = preString.replaceAll("      ", " ");
                        preString = preString.replaceAll("\n", "");
                        preString = preString.replaceAll("-", "  ");
                        preString = preString.replaceAll("Recommendation",
                            "   Recommendation");
                    }
                    postArea.setText(preString);
                    postAreaToClipBoard();
                }
                else if (cmd.equals(TO_CLIPBOARD) && !postArea.getText().isEmpty())
                {
                    postAreaToClipBoard();
                }
                else if (cmd.equals(EXIT))
                {
                    JButton btn = (JButton) e.getSource();
                    Window window = (Window) btn.getTopLevelAncestor(); 
                    window.dispose();
                }
            }
    
            private void postAreaToClipBoard()
            {
                Toolkit toolkit = Toolkit.getDefaultToolkit();
                Clipboard clipboard = toolkit.getSystemClipboard();
                StringSelection selection = new StringSelection(postArea.getText());
                clipboard.setContents(selection, null);
                preAreaFocusAndSelect();
            }
    
            private void preAreaFocusAndSelect()
            {
                preArea.setSelectionStart(0);
                preArea.setSelectionEnd(preArea.getText().length());
                preArea.requestFocus();
            }
        }
    
        private JPanel createFieldPanel()
        {
            JPanel preLabelPanel = new JPanel();
            preLabelPanel.add(new JLabel("Text Pre Adjustment"));
            JPanel postLabelPanel = new JPanel();
            postLabelPanel.add(new JLabel("Text Post Adjustment"));
            
            JPanel prePanel = new JPanel();
            JPanel postPanel = new JPanel();
            prePanel.setLayout(new BoxLayout(prePanel, BoxLayout.PAGE_AXIS));
            postPanel.setLayout(new BoxLayout(postPanel, BoxLayout.PAGE_AXIS));
            
            // add the JTextAreas to JScrollPanes and add them to the pre and post panels
            prePanel.add(preLabelPanel);
            prePanel.add(new JScrollPane(preArea));
            postPanel.add(postLabelPanel);
            postPanel.add(new JScrollPane(postArea));
            
            JPanel fieldPanel = new JPanel(new GridLayout(1, 0, 10, 10));
            fieldPanel.add(prePanel);
            fieldPanel.add(postPanel);
            return fieldPanel;
        }
        
        public JPanel getMainPanel()
        {
            return mainPanel;
        }
        
        private static void createAndShowGUI()
        {
            JFrame frame = new JFrame("ProcedureFindings Application");
            frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            frame.getContentPane().add(new ProcedureFindings2().getMainPanel());
            frame.pack();
            frame.setLocationRelativeTo(null);
            frame.setVisible(true);
        }
    
        public static void main(String[] args)
        {
            javax.swing.SwingUtilities.invokeLater(new Runnable()
            {
                public void run()
                {
                    createAndShowGUI();
                }
            });
    
        }
    }

  9. #9
    Unknown is offline Member
    Join Date
    Dec 2008
    Posts
    15
    Rep Power
    0

    Default

    Thank you for your example, I'm guessing that for my program, I'll need multiple methods too. So, is the prestring part the text area?

  10. #10
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    My mistake in that the initial String is not loaded into the clipboard but instead is loaded into the preArea JTextArea.

    This is a conversion program and converts one String to another. The variable preString is just that, a String, that is initially set to the text held in the JTextArea on the left, the "preArea". This text is extracted from the JTextArea by calling its getText() method. After preString has been changed, it is posted to the clipboard and also placed into the JTextArea on the right, the "postArea" by calling the latter JTextArea's setText(...) method.

  11. #11
    Unknown is offline Member
    Join Date
    Dec 2008
    Posts
    15
    Rep Power
    0

    Default

    can someone tell me how to change the sizes of the buttons please?

  12. #12
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    I'm going to ask you for a better question. Please answer these questions and also give us any other information that may be pertinent here:

    what specifically is your problem, what code have you tried to solve the problem, and how isn't it working?

  13. #13
    Unknown is offline Member
    Join Date
    Dec 2008
    Posts
    15
    Rep Power
    0

    Default

    well, I guess right now I want to get the buttons to resize the buttons that I have and get them working. In order to resize the buttons, I wanted to try boxlayout, but I don't think I implemented it right. I changed to borderlayout afterwords, because like you posted earlier, gridlayout is a pain. The main probelm that I'm trying to deal with right now is that I can't resize the buttons and they end up overlapping. Like for instance this is what I'd like my program to look.

    -------------------------Title---------------------------------

    Information JPEG Image


    -----------------------CheckBox------------------------------
    Button1-------|------Button2----------|-------Button3--------

    Ignore that I didn't put things like the menu. Anyways, here's how my program looks now, keep in mind that not everything is done like I haven't put in images or the checkbox, and the text (though I think I can do that easily).

    -------------------------Title-----------------------------

    Information placeholder

    ------------------------Button3--------------------------

    Button3 overlaps the other two buttons that I have placed there, so I want to resize the buttons right now so they fit in the same space. I hope this gives you a better understanding

  14. #14
    Unknown is offline Member
    Join Date
    Dec 2008
    Posts
    15
    Rep Power
    0

    Default

    The JPEG should be on the right sorry

  15. #15
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    just a guess, but one way is to:
    • Make the main JPanel use BorderLayout
    • The title is on a JPanel that uses FlowLayout and is added to the main panel BorderLayout.NORTH
    • The image is an ImageIcon on a JLabel and is added to the main JPanel BoderLayout.CENTER
    • The south JPanel will hold the buttons and the checkBox and will be added to the main JPanel BorderLayout.SOUTH
    • The south JPanel could use BoxLayout oriented along the page axis.
    • The JCheckBox can be added to a JPanel using flowlayout and added first to the south JPanel
    • The buttons will be added to a buttonPanel JPanel that uses GridLayout(1, 0, gap, 0) where gap an int is on the order of 10 to 30 or so.


    YMMV.

  16. #16
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    for instance:
    Java Code:
    import java.awt.*;
    import javax.swing.*;
    
    public class ButtonSizes
    {
      private static final String[] BUTTON_STRINGS = 
      {
        "Button 1", "Button 2", "Button 3"
      };
      private JPanel mainPanel = new JPanel();
    
      public ButtonSizes()
      {
        JLabel title = new JLabel("Title", SwingConstants.CENTER);
        title.setFont(title.getFont().deriveFont(Font.BOLD, 40));
        
        JLabel imageLabel = new JLabel("Image will be here", SwingConstants.CENTER);
        imageLabel.setPreferredSize(new Dimension(400, 200));
        imageLabel.setOpaque(true);
        imageLabel.setBackground(Color.pink);
        imageLabel.setBorder(BorderFactory.createLineBorder(Color.blue));
        
        JCheckBox checkBox = new JCheckBox("Check Box");
        JPanel checkBoxPanel = new JPanel();
        checkBoxPanel.add(checkBox);
        
        JPanel buttonPanel = new JPanel(new GridLayout(1, 0, 10, 0));
        for (int i = 0; i < BUTTON_STRINGS.length; i++)
        {
          JButton button = new JButton(BUTTON_STRINGS[i]);
          buttonPanel.add(button);
        }
        
        JPanel southPanel = new JPanel();
        southPanel.setLayout(new BoxLayout(southPanel, BoxLayout.PAGE_AXIS));
        southPanel.add(checkBoxPanel);
        southPanel.add(buttonPanel);
    
        int ebGap = 5;
        mainPanel.setBorder(BorderFactory.createEmptyBorder(ebGap, ebGap, ebGap, ebGap));
        mainPanel.setLayout(new BorderLayout(10, 10));
        mainPanel.add(title, BorderLayout.NORTH);
        mainPanel.add(imageLabel, BorderLayout.CENTER);
        mainPanel.add(southPanel, BorderLayout.SOUTH);
      }
    
      public JComponent getComponent()
      {
        return mainPanel;
      }
    
      private static void createAndShowUI()
      {
        JFrame frame = new JFrame("ButtonSizes");
        frame.getContentPane().add(new ButtonSizes().getComponent());
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.pack();
        frame.setLocationRelativeTo(null);
        frame.setVisible(true);
      }
    
      public static void main(String[] args)
      {
        java.awt.EventQueue.invokeLater(new Runnable()
        {
          public void run()
          {
            createAndShowUI();
          }
        });
      }
    }

  17. #17
    Unknown is offline Member
    Join Date
    Dec 2008
    Posts
    15
    Rep Power
    0

    Default

    The suggestion about the buttons worked perfectly, thank you.

  18. #18
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    Cool, glad you've got it working.

  19. #19
    Unknown is offline Member
    Join Date
    Dec 2008
    Posts
    15
    Rep Power
    0

    Default

    I have a question , I noticed in your example(thanks for that) that you had separate methods for different parts. I'm trying to do everything in the main method. Is that possible or should I create different methods too?

  20. #20
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    It's generally considered bad form to do everything in a main method and leads to creation of monster methods that are near impossible to debug or extend, not to mention the lack ability to reuse any code.

Page 1 of 2 12 LastLast

Similar Threads

  1. Final int
    By Fireking in forum New To Java
    Replies: 12
    Last Post: 06-22-2013, 06:44 PM
  2. [SOLVED] is final class members are also final ?
    By haoberoi in forum New To Java
    Replies: 4
    Last Post: 11-10-2008, 04:01 PM
  3. help to complete final part of project
    By dirtycash in forum New To Java
    Replies: 6
    Last Post: 12-31-2007, 07:21 AM
  4. Poi 3.0-final
    By levent in forum Java Software
    Replies: 0
    Last Post: 05-22-2007, 08:05 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •