-1

how can I get the buttons to appear on the line underneath the textarea?

//Imports
import java.awt.CardLayout;
import java.awt.Color;
import java.awt.Component;
import java.awt.Insets;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.BorderFactory;
import javax.swing.JButton;
import javax.swing.JCheckBox;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.JTextPane;

public class ProfessorPhysInstall {

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
        // TODO code application logic here
        //Variables
        final JFrame mainframe = new JFrame();
        mainframe.setSize(500, 435);
        final JPanel cards = new JPanel(new CardLayout());
        final CardLayout cl = (CardLayout)(cards.getLayout());
        mainframe.setTitle("Future Retro Gaming Launcher");
        //Screen1
        JPanel screen1 = new JPanel();
        JTextPane TextPaneScreen1 = new JTextPane();
        TextPaneScreen1.setEditable(false);
        TextPaneScreen1.setBackground(new java.awt.Color(240, 240, 240));
        TextPaneScreen1.setText("Welcome to the install wizard  for Professor Phys!\n\nPlease agree to the following terms and click the next button to continue.");
        TextPaneScreen1.setSize(358, 48);
        TextPaneScreen1.setLocation(0, 0);
        TextPaneScreen1.setBorder(BorderFactory.createCompoundBorder(BorderFactory.createLineBorder(Color.black),BorderFactory.createEmptyBorder(5, 5, 5, 5)));
        TextPaneScreen1.setMargin(new Insets(4,4,4,4));
        screen1.add(TextPaneScreen1);
        JTextArea TextAreaScreen1 = new JTextArea();
        JScrollPane sbrText = new JScrollPane(TextAreaScreen1);
        TextAreaScreen1.setRows(15);
        TextAreaScreen1.setColumns(40);
        TextAreaScreen1.setEditable(false);
        TextAreaScreen1.setText("Lots of text");
        TextAreaScreen1.setBorder(BorderFactory.createCompoundBorder(BorderFactory.createLineBorder(Color.black),BorderFactory.createEmptyBorder(5, 5, 5, 5)));
        TextAreaScreen1.setMargin(new Insets(4,4,4,4));
        screen1.add(sbrText);
        final JCheckBox Acceptance = new JCheckBox();
        Acceptance.setText("I Accept The EULA Agreenment.");
        screen1.add(Acceptance);
        final JButton NextScreen1 = new JButton();
        NextScreen1.setText("Next");
        NextScreen1.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent ae) {
                if(Acceptance.isSelected())
                    cl.next(cards);
            }
        });      
        screen1.add(NextScreen1);        
        JButton CancelScreen1 = new JButton();
        CancelScreen1.setText("Cancel");
        CancelScreen1.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent ae) {
                System.exit(0);
            }
        });      
        screen1.add(CancelScreen1);
        cards.add(screen1);

        //Screen2
        JPanel screen2 = new JPanel();
        JTextPane TextPaneScreen2 = new JTextPane();
        TextPaneScreen2.setEditable(false);
        TextPaneScreen2.setBackground(new java.awt.Color(240, 240, 240));
        TextPaneScreen2.setText("Please select the Future Retro Gaming Launcher.");
        TextPaneScreen2.setSize(358, 48);
        TextPaneScreen2.setLocation(0, 0);
        TextPaneScreen2.setBorder(BorderFactory.createCompoundBorder(BorderFactory.createLineBorder(Color.black),BorderFactory.createEmptyBorder(5, 5, 5, 5)));
        TextPaneScreen2.setMargin(new Insets(4,4,4,4));
        screen2.add(TextPaneScreen2);
        final JButton BackScreen2 = new JButton();
        BackScreen2.setText("Back");
        BackScreen2.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent ae) {
                if(Acceptance.isSelected())
                    cl.previous(cards);
            }
        });    
        screen2.add(BackScreen2);     
        final JButton NextScreen2 = new JButton();
        NextScreen2.setText("Next");
        NextScreen2.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent ae) {
                if(Acceptance.isSelected())
                    cl.next(cards);
            }
        });      
        screen2.add(NextScreen2);        
        JButton CancelScreen2 = new JButton();
        CancelScreen2.setText("Cancel");
        CancelScreen2.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent ae) {
                System.exit(0);
            }
        });      
        screen2.add(CancelScreen2);
        cards.add(screen2);

        mainframe.add(cards);
        mainframe.setVisible(true);
    }
}

This is what it looks like

enter image description here

Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
Kyle
  • 2,339
  • 10
  • 33
  • 67
  • Don't post your code anywhere but here, and explain what you've tried to solve the problem yourself. – Brian Roach Jun 13 '13 at 01:23
  • This is not a debugging service. If something is behaving unexpectedly, we can help. But if it's just about finding a very specific thing you missed, then it's unlikely to help anyone else, and is therefore a bad fit for this site. – Zong Jun 13 '13 at 01:26
  • Okay sorry about that. The code was really long so I thought it would be easier to read. Basically I don't know what to do. For the first screen(screen1) everything was perfect. But now I"m getting buttons not on their own line. I tried adding a margin but I think it has to do with the space being filled. It seems that if there isn't enough space for the button, it goes to the next line. – Kyle Jun 13 '13 at 01:31

3 Answers3

2

Use an appropriate LayoutManager

You've added all the components to screen2 which has a FlowLayout by default. There are many different ways to achieve what you're after.

More reading: Using Layout Managers

splungebob
  • 5,357
  • 2
  • 22
  • 45
2

The default layout manager for JPanel is FlowLayout. As the name suggests, it simply flows the components one after the other.

You will need to try a different layout manager, something like GridBagLayout will do what you want, but it is a complex layout. You may be better of trying to use compound layouts to achieve the same effect.

That is, break the UI down into small chunks and apply different layout managers for each group.

Check out...

You will also benifit from reading...

Ps...

You may also want to consider breaking each screen up into it's own class, the code as it stands is very hard to read and understands - IMHO

MadProgrammer
  • 343,457
  • 22
  • 230
  • 366
2

The usual way is to combine layouts for the look needed. E.G.

enter image description here

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

public class ProfessorPhysInstall {

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
        //Variables
        final JFrame mainframe = new JFrame();
        mainframe.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
        mainframe.setSize(500, 435);
        final JPanel cards = new JPanel(new BorderLayout());
        mainframe.setTitle("Future Retro Gaming Launcher");

        //Screen2
        JPanel screen2 = new JPanel();
        JTextPane TextPaneScreen2 = new JTextPane();
        TextPaneScreen2.setText("Please select the Future Retro Gaming Launcher.");
        // set the preferred size, rather than the size!
        //TextPaneScreen2.setSize(358, 48);
        TextPaneScreen2.setPreferredSize(new Dimension(358, 48));
        TextPaneScreen2.setBorder(
            BorderFactory.createCompoundBorder(
                BorderFactory.createLineBorder(Color.black),
                BorderFactory.createEmptyBorder(5, 5, 5, 5)));
        TextPaneScreen2.setMargin(new Insets(4,4,4,4));
        screen2.add(TextPaneScreen2);

        JPanel buttons = new JPanel(new FlowLayout(FlowLayout.CENTER, 5,5));
        final JButton BackScreen2 = new JButton();
        BackScreen2.setText("Back");
        buttons.add(BackScreen2);
        final JButton NextScreen2 = new JButton("Next");
        buttons.add(NextScreen2);

        JButton CancelScreen2 = new JButton("Cancel");
        buttons.add(CancelScreen2);

        cards.add(screen2, BorderLayout.PAGE_START);
        cards.add(buttons, BorderLayout.PAGE_END);

        mainframe.add(cards);
        mainframe.pack();
        mainframe.setVisible(true);
    }
}

Tips

  1. Please learn common Java naming conventions (specifically the case used for the names) for class, method & attribute names & use them consistently.
  2. For better help sooner, post an SSCCE.
  3. Don't set the size of top level containers. Instead layout the content & call pack().
  4. See the Nested Layout Example for ideas about how to combine layouts to create the required layout.


  5. Also heed the advice already offered on the other two answers.
Community
  • 1
  • 1
Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433