Frage

OK I have a frame with dimensions 1280x720. I need to split that up and create one side on the left that is 1000x720 and the right side 280x720. The panel that now is 1000x720 I need to split it again to be 1000x520 on top and 1000x200 the bottom side. I have tried for quite some time. It would really help if you have any links to help or share your experience. So its 3 areas: #1 1000x520. #3 280x720 #2 1000x200

    import javax.swing.JFrame;
import javax.swing.JSplitPane;
import javax.swing.UIManager;

public class GamePanel extends JFrame{


    private static final long serialVersionUID = 1L;
    public JSplitPane secondSplit;
    SplitTableHand splitTableHand = new SplitTableHand();
    ChatPanel chatPanel = new ChatPanel();


    public GamePanel() {

        secondSplit = new JSplitPane(JSplitPane.HORIZONTAL_SPLIT,splitTableHand, chatPanel);

        secondSplit.setOneTouchExpandable(false);
        secondSplit.setDividerLocation(1000);
        this.setSize(1280, 720);
        this.setResizable(false);
        this.setDefaultCloseOperation(EXIT_ON_CLOSE);
        //this.pack();
        this.setVisible( true );
        getContentPane().add( secondSplit );



    }
    public static void main( String args[] ){
        try {
            UIManager.setLookAndFeel("com.sun.java.swing.plaf.windows.WindowsLookAndFeel");
        } catch (Exception evt) {}
        javax.swing.SwingUtilities.invokeLater(new Runnable() {
            public void run() {
                GamePanel mainFrame = new GamePanel();

            }
        });


    }


}

    import java.awt.Dimension;

import javax.swing.JPanel;
import javax.swing.JSplitPane;


public class SplitTableHand extends JPanel{

    /**
     * 
     */
    private static final long serialVersionUID = 1L;
    JSplitPane splitPane;
    TablePanel tablePanel = new TablePanel();
    HandPanel handPanel = new HandPanel();

    public SplitTableHand() {


        splitPane = new JSplitPane(JSplitPane.VERTICAL_SPLIT, tablePanel, handPanel);

        splitPane.setOneTouchExpandable(false);
        splitPane.setDividerLocation(550);
        splitPane.setPreferredSize(new Dimension(1000, 720));
        add(splitPane);
        splitPane.setVisible(true);
    }


}

    import java.awt.BorderLayout;
import java.awt.Color;

import javax.swing.JLabel;
import javax.swing.JPanel;


public class TablePanel extends JPanel{

    private static final long serialVersionUID = 1L;
    private JLabel label = new JLabel("LABEL");

    public TablePanel() {
        setLayout(new BorderLayout());// we shall use absolute positioning for this
        this.setSize(1000, 520);
        this.setPreferredSize(getSize());
        this.setBackground(new Color(100,100,100))  ;
        this.add(label);
        this.setVisible(true);
    }
}

    import java.awt.Color;

import javax.swing.JButton;
import javax.swing.JPanel;


public class HandPanel extends JPanel{

    private static final long serialVersionUID = 1L;
    JButton button;
    public HandPanel() {
        //default layout is flowlayout
        this.setSize(1000, 200);
        this.setPreferredSize(getSize());
        this.setBackground(new Color(150,150,50))   ;

        for( int i = 0 ; i < 20;i++){
            String name = "Button"+ i; 
            button = new JButton(name);
            this.add(button);
            this.setVisible(true);
        }
    }

}

    import java.awt.Color;
import java.awt.FlowLayout;



import javax.swing.JButton;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.JTextField;


public class ChatPanel extends JPanel{
    /**
     * 
     */
    private static final long serialVersionUID = 1L;
    private JTextArea chatArea = new JTextArea(10, 30);
    private JTextField chatField = new JTextField(50);
    JScrollPane scrollPane = new JScrollPane(chatArea); 
    JButton button ;

    public ChatPanel() {
        setLayout(new FlowLayout());
        this.setSize(720, 280);
        this.setPreferredSize(getSize());
        this.setBackground(new Color(50,50,50)) ;
        chatArea.setEditable(false);
        chatArea.setLineWrap(true);
        chatArea.setVisible(true);
        chatField.setVisible(true);
        button = new JButton("Button");
        button.setEnabled(true);
        button.setVisible(true);

        this.add(scrollPane);
        this.add(chatField);
        this.setVisible(true);
        this.add(button);
    }

}
War es hilfreich?

Lösung

First of all your program is giving compilation errors. Fix them. You have to extend SplitTableHand with JPanel. With this your error in GamePanel gets resolved.

public class SplitTableHand extends JPanel

Second point is in GamePanel class constructor, you created topPanel and added it to the frame instead of adding secondSplit.

Change the constructor of GamePanel to

public GamePanel() {
    secondSplit = new JSplitPane(JSplitPane.HORIZONTAL_SPLIT,splitTableHand, chatPanel);

    secondSplit.setOneTouchExpandable(true);
    secondSplit.setDividerLocation(150);
    this.setSize(1280, 720);
    this.setResizable(false);
    this.setDefaultCloseOperation(EXIT_ON_CLOSE);
    //this.pack();
    this.setVisible( true );
    getContentPane().add( secondSplit );
}

In SplitTableHand class also, you have to add the splitPane.

Add a statement in it's constructor add(splitPane);

Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top