-1

this is the code I have so far

public class WindowHelpGui extends JFrame{
    JScrollPane scroll;
    //constructor
    public WindowHelpGui(){
         //add window title
        super("Help");
        //set window layout
        setLayout(null);

        JTabbedPane tabbedPane = new JTabbedPane();
        tabbedPane.setBounds(10, 10, 750, 550);

        JPanel panel1 = new JPanel();
        JPanel panel2 = new JPanel();
        JPanel panel3 = new JPanel();

        panel1.add(new JTextArea("\nDRAFT TEXT 101 
\ncodnvfdinofndovndofinvinfdivnifdnvidfnviofnivnidfnviofdnvindfinv
ivondfviondfiovndfionvifdnvionivfdninfdinfivnidfovniofnviofnvifdnv
fndiovnf\nh\ne\nl\nl\no\n\nf\nr\no\nm\n\nt\nh\ne\n\no\nt\nh"+
       "e\nr\n\ns\ni\nd\ne\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n
n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n
nHello")); 
       panel2.add(new JTextArea("panel2 working"));
       panel3.add(new JTextArea("panel3 working"));
       JScrollPane scroll = new JScrollPane(panel1, JScrollPane.
HORIZONTAL_SCROLLBAR_NEVER, JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDE
       add(scroll, panel1);

       tabbedPane.add("How to play", panel1);
       tabbedPane.add("Seeds", panel2);
       tabbedPane.add("Tools", panel3);
    
       //scroll = new Jscro
       this.add(tabbedPane);

        //set size of window
        setSize(800,600);
        //set visibility
        setVisible(true);
        //set resizable 
        setResizable(false);
        //set default close
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    }

when I run this I dont see any window popping up. Can someone help me please?

I have tried also tried using add(scroll, tabbedPane) but it just adds a new tab on my window.

qtdumdum
  • 1
  • 2

2 Answers2

0

The JTextArea must be added directly to the JScrollPane and you can add the scrollpane directly to the tabbedPane:

    JTextArea textArea = new JTextArea("\nDRAFT TEXT 101\ncodnvfdinofndovndofinvinfdivnifdnvidfnviofnivnidfnviofdnvindfinvivondfviondfiovndfionvifdnvionivfdninfdinfivnidfovniofnviofnvifdnvfndiovnf\nh\ne\nl\nl\no\n\nf\nr\no\nm\n\nt\nh\ne\n\no\nt\nh" +
            "e\nr\n\ns\ni\nd\ne\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\nn\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\nnHello");
    JScrollPane scrollPane = new JScrollPane(textArea, JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED, JScrollPane.HORIZONTAL_SCROLLBAR_NEVER);

    tabbedPane.add("How to play", scrollPane);
Thomas Kläger
  • 17,754
  • 3
  • 23
  • 34
0

You set the JTextArea into the ScrollPane constructor. Here is a runnable example (read comments in code):

import java.awt.Dimension;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTabbedPane;
import javax.swing.JTextArea;


public class WindowHelpGui extends JFrame {

    private static final long serialVersionUID = 34534L;
    
    private JTextArea textArea_1;
    private JTextArea textArea_2;
    private JTextArea textArea_3;
    
    private JTabbedPane tabbedPane;
    

    //constructor
    public WindowHelpGui() {
        createForm();
    }

    private void createForm() {
        //set window layout
//        setLayout(null);  // If possible, don't ever use a Null layout.

        //set resizable 
//        setResizable(false);  // It's nice to be able to resize from time to time.

        // add window title
        setTitle("Help");
        
        // set size of window
        setPreferredSize(new Dimension(800, 600));

        //set default close
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        // OPTIONAL - Set form on top of everything.
        setAlwaysOnTop(true);

        tabbedPane = new JTabbedPane();

        JPanel panel1 = new JPanel();
        JPanel panel2 = new JPanel();
        JPanel panel3 = new JPanel();

        textArea_1 = new JTextArea();
        textArea_1.setText("\nDRAFT TEXT 101\ncodnvfdinofndovndofinvinfdivnifdnvidfnviofnivnid"
                + "fnviofdnvindfinvivondfviondfiovndfionvifdnvionivfdninfdinfivnidfovniofnvio"
                + "fnvifdnvfndiovnf\nh\ne\nl\nl\no\n\nf\nr\no\nm\n\nt\nh\ne\n\no\nt\nhe\nr\n\n"
                + "s\ni\nd\ne\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n"
                + "\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\nHello");
        textArea_1.setCaretPosition(0);  // Set the caret to beginning of document.
        
        textArea_2 = new JTextArea();
        textArea_2.setText("Seeds Area Working");
        
        textArea_3 = new JTextArea();
        textArea_3.setText("Tools Area Working");

        JScrollPane scroll_1 = new JScrollPane();
        scroll_1.setViewportView(textArea_1);
        JScrollPane scroll_2 = new JScrollPane();
        scroll_2.setViewportView(textArea_2);
        JScrollPane scroll_3 = new JScrollPane();
        scroll_3.setViewportView(textArea_3);
        
        tabbedPane.add("How to play", scroll_1);
        tabbedPane.add("Seeds", scroll_2);
        tabbedPane.add("Tools", scroll_3);

        add(tabbedPane);
        
        pack(); // Now you can resize or maximize the form and everything will resize with it.
        
        setVisible(true);
        // Set Screen Location (after the pack())
        setLocationRelativeTo(null);
    }

    public static void main(String[] args) {
        new WindowHelpGui();
    }
    
}
DevilsHnd - 退職した
  • 8,739
  • 2
  • 19
  • 22