views:

131

answers:

4

This is making me very angry, I have worked on this for 2 days, have 2 books open and have looked through them, and STILL can't get this program to run the way I want it run. I'm getting to the point where if this doesn't help, I quit.

I want a SIMPLE Frame application. It has a JComboBox centered at the top. Next to it is a text field big enough to show numeric digits such as "$49.99" Below it is a spot for a Text area showing terms of service Below that is the checkbox agreeing to the terms of service Below that is 2 buttons "Accept" and "Decline"

I Have worked on this for 2 days, here is the coding:

public class Bar extends JFrame implements ActionListener
{
    public Bar(final JFrame frame)
    {
        String[] tests = { "A+ Certification", "Network+ Certification", "Security+ Certification", "CIT Full Test Package" };
        JButton button = new JButton("Click Meh");
        add(new JLabel("Welcome to the CIT Test Program "));
        add(new JLabel("Please select which Test Package from the list below."));
        frame.setVisible(true);
        frame.setSize(250,250);
        JPanel pane1 = new JPanel(new FlowLayout());
        JPanel pane2 = new JPanel(new FlowLayout());

        JMenuBar menuBar = new JMenuBar();
        JMenu fileMenu = new JMenu("File");
        JMenu editMenu = new JMenu("Edit");
        JMenu helpMenu = new JMenu("Help");
        menuBar.add(fileMenu);
        menuBar.add(editMenu);
        menuBar.add(helpMenu);
        JMenuItem newMenu = new JMenuItem("New  (Ctrl+N)");
        JMenuItem openMenu = new JMenuItem("Open  (Ctrl+O)");
        JMenuItem saveMenu = new JMenuItem("Save  (Ctrl+S)");
        saveMenu.addActionListener(this);
        JMenuItem exitMenu = new JMenuItem("Exit  (Ctrl+W)");
        JMenuItem cutMenu = new JMenuItem("Cut  (Ctrl+X)");
        JMenuItem copyMenu = new JMenuItem("Copy  (Ctrl+C)");
        JMenuItem pasteMenu = new JMenuItem("Paste  (Ctrl+V)");
        JMenuItem infoMenu = new JMenuItem("Help  (Ctrl+H)");
        fileMenu.add(newMenu);
        fileMenu.add(openMenu);
        fileMenu.add(saveMenu);
        fileMenu.add(exitMenu);
        editMenu.add(cutMenu);
        editMenu.add(copyMenu);
        editMenu.add(pasteMenu);
        helpMenu.add(infoMenu);
        frame.setJMenuBar(menuBar);
        JComboBox packageChoice =  new JComboBox(tests);
        frame.add(packageChoice);


    }

     public void actionPerformed(ActionEvent e)
  {
  Object source = e.getSource();
  {
  }

}

EDIT: Forgot to add the second program

public class JFrameWithPanel
{
    public static void main(String[] args)
    {
         JPanel panel = new Bar(new JFrame("CIT Test Program"));
    }
}

How do I get this to have everything where I want it and show up? I'm very confused because of this and now barely even get how Frames work.

A: 

1 . Your second program wont compile. You are trying to assign a JFrame to a JPanel.

It should have been

 JFrame frame = new Bar(new JFrame("CIT Test Program")); 

2 . It works. What you want is there. But not as you want it, because you have not told java how you want it to display. Try using a LayoutManager like GridBagLayout.

If you are in a hurry to create a GUI, try a IDE like NetBeans that makes your job easier via drag and drop.

EDIT:

An Example:

public class TestN extends JFrame
{
    private JLabel label ;
    private JComboBox combo;
    private JButton button;
    public TestN()
    {
        label = new JLabel("Label:");
        combo = new JComboBox();
        combo.addItem("Item 1");
        combo.addItem("Item 2");
        combo.addItem("Item 3");

        setLayout(new GridBagLayout());

        GridBagConstraints c1 = new GridBagConstraints();
        c1.gridx = 0;
        c1.gridy = 0;
        c1.weightx = 1;
        c1.weighty = 1;
        add(label, c1);

        GridBagConstraints c2 = new GridBagConstraints();
        c2.gridx = 1;
        c2.gridy = 0;
        c2.weightx = 1;
        c2.weighty = 1;
        add(combo, c2);

        button = new JButton("Ok");
        GridBagConstraints c3 = new GridBagConstraints();
        c3.gridx = 2;
        c3.gridy = 0;
        c3.weightx = 1;
        c3.weighty = 1;
        add(button, c3);

        setTitle("Test");
        setSize(200,100);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setVisible(true);
    }

    public static void main(String[] args)
    {
        new TestN();
    }
}

Disclaimer: This is a basic example just to give an example. Not meant to be production code ;-)

Nivas
components are still not showing, until I click the border, and then the JComboBox takes up the entire frame.
Nick Gibson
I dont know what change you did. I assume that you have not added a layout manager yet.Added a example, with gridbaglayout. Hope this helps.Nexttime on, try giving a screenshot, drawing or something describing what you want.Also, try using a IDE if you are in a hurry.
Nivas
Yeah, I just discovered NetBeans, I think I will try and use it..it works much better.
Nick Gibson
+1  A: 

Nick. I think the first thing you have to clear out is to know exactly what you want.

I helped you in this question http://stackoverflow.com/questions/3055777/how-to-...

From what you're describing you already have what you need. What's wrong with this:

alt text

So, I really think you should clearly describe what you want, in order to get it.

We would help you, but you have to clearly define what your problem is.

OscarRyz
A: 

Components in Swing have to be laid out in a certain order.

You start with a JFrame. The only components that are placed in the JFrame are a JMenuBar and a JPanel. You do not add any other components in a JFrame. You add components in a JPanel.

Here's Nick's code, reorganized to define the components in the correct order. I used GridLayout because it was quicker. You should use GridBagLayout, as Nivas said.

public class Bar {

    private static final long serialVersionUID = 1L;

    public Bar(final JFrame frame) {
        JMenuBar menuBar = buildMenuBar();
        frame.setJMenuBar(menuBar);

        JPanel masterPanel = new JPanel(new GridLayout(2, 1));

        JPanel pane1 = new JPanel(new GridLayout(3, 1));
        pane1.add(new JLabel("Welcome to the CIT Test Program "));
        pane1.add(new JLabel("Please select which Test Package from the list below."));
        JButton button = new JButton("Click Me");
        pane1.add(button);

        JPanel pane2 = new JPanel(new GridLayout(1, 1));
        String[] tests = { "A+ Certification", "Network+ Certification",
                "Security+ Certification", "CIT Full Test Package" };
        JComboBox packageChoice = new JComboBox(tests);
        pane2.add(packageChoice);

        masterPanel.add(pane1);
        masterPanel.add(pane2);

        frame.add(masterPanel);
        frame.pack();

        frame.setVisible(true);
//      frame.setSize(250, 250);

    }

    public JMenuBar buildMenuBar() {
        JMenuBar menuBar = new JMenuBar();
        JMenu fileMenu = new JMenu("File");
        JMenu editMenu = new JMenu("Edit");
        JMenu helpMenu = new JMenu("Help");
        menuBar.add(fileMenu);
        menuBar.add(editMenu);
        menuBar.add(helpMenu);
        JMenuItem newMenu = new JMenuItem("New  (Ctrl+N)");
        JMenuItem openMenu = new JMenuItem("Open  (Ctrl+O)");
        JMenuItem saveMenu = new JMenuItem("Save  (Ctrl+S)");
        saveMenu.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                // TODO Auto-generated method stub

            }
        });
        JMenuItem exitMenu = new JMenuItem("Exit  (Ctrl+W)");
        JMenuItem cutMenu = new JMenuItem("Cut  (Ctrl+X)");
        JMenuItem copyMenu = new JMenuItem("Copy  (Ctrl+C)");
        JMenuItem pasteMenu = new JMenuItem("Paste  (Ctrl+V)");
        JMenuItem infoMenu = new JMenuItem("Help  (Ctrl+H)");
        fileMenu.add(newMenu);
        fileMenu.add(openMenu);
        fileMenu.add(saveMenu);
        fileMenu.add(exitMenu);
        editMenu.add(cutMenu);
        editMenu.add(copyMenu);
        editMenu.add(pasteMenu);
        helpMenu.add(infoMenu);
        return menuBar;
    }
}

I moved the JMenuBar code into its own method so, hopefully, the rest of the code is easier to understand.

I have a master JPanel, that all of the other components are added to.

I used another JPanel to hold the two JLabels and the JButton.

I used a third JPanel to hold the JComboBox.

The basic pattern is as follows:

  • Define the JPanel.
  • Define the components.
  • Add the components to the JPanel.
  • Add the JPanel to the master JPanel
  • Add the master JPanel to the JFrame.
Gilbert Le Blanc
A: 

You have:

public class Bar extends JFrame implements ActionListener
{
    public Bar(final JFrame frame)
    {
        String[] tests = { "A+ Certification", "Network+ Certification", "Security+ Certification", "CIT Full Test Package" };
        JButton button = new JButton("Click Meh");
        add(new JLabel("Welcome to the CIT Test Program "));
        add(new JLabel("Please select which Test Package from the list below."));
        frame.setVisible(true);
        frame.setSize(250,250);

Each Bar object inherits from JFrame (i.e. a Bar object is a type of JFrame) but you pass in a different JFrame object (the frame parameter) in the constructor. You then call some methods on the Bar object - such as adding the two JLabel objects - and some on the passed-in JFrame - such as the setVisible and setSize methods.

So you have two different JFrame objects and you do some work to each of them and only one of them is ever set to be visible.

It looks as is the only reason you are passing in the JFrame to the constructor is to set the title of the window. If you pass in a String with the title to the Bar constructor and then call super(title); as the first line of the Bar constructor then you won't need the passed-in JFrame at all

barrowc