I am new to the entire java thing and I have been attempting to make a window with 2/3 of the window being a text box and the other 3rd being an empty JPanel (For later use), but I am unsure how to execute this. I have tried Gridlayout
but noticed you are unable to specify how many columns a component can take up. I don't really mind how it is done, but could someone please help me with this?
I have searched multiple other posts such as this one which had a very similar concept but got an answer that I have tried but ended in a product I didn't enjoy: Two JPanels side by side in a JFrame with one JPanel having a fixed width, What I would prefer is something like this which is locked to 2/3's on the left and 1/3 on the right: Picture of TextPanel and JPanel
Here is what I have attempted:
import javax.swing.*;
import javax.swing.text.SimpleAttributeSet;
import javax.swing.text.StyleConstants;
import java.awt.*;
import java.io.File;
import java.io.IOException;
public class GUI {
public static final int sizeX = 1280;
public static final int sizeY = 680;
public static JTextPane textBox;
public static JPanel createGui() {
//Create panels
JPanel mainPanel = new JPanel();
JPanel textPanel = new JPanel();
JPanel inventoryPanel = new JPanel();
//Setup panel details
mainPanel.setPreferredSize(new Dimension(sizeX, sizeY));
textPanel.setBackground(Color.BLACK);
inventoryPanel.setBackground(new Color(71, 22, 22));
//Create custom font
try {
Font customFont = Font.createFont(Font.TRUETYPE_FONT, new File("TheDungeonDungeon/resources/font.ttf")).deriveFont(10f);
GraphicsEnvironment ge = GraphicsEnvironment.getLocalGraphicsEnvironment();
ge.registerFont(customFont);
textBox = new JTextPane();
textBox.setFont(customFont);
} catch (IOException|FontFormatException e) {
System.out.println("<Error> Font failure");
}
SimpleAttributeSet attributeSet = new SimpleAttributeSet();
StyleConstants.setForeground(attributeSet, Color.white);
textBox.setAutoscrolls(true);
textBox.setEditable(false);
textBox.setBackground(Color.black);
textBox.setCharacterAttributes(attributeSet, true);
textBox.setText("Text");
textPanel.add(textBox);
inventoryPanel.add(new JLabel("Empty"));
mainPanel.setLayout(new GridLayout(1,3));
mainPanel.add(textPanel); //I want this to take up 2 columns
mainPanel.add(inventoryPanel); //I want this to take up 1 column
return mainPanel;
}
}
I hope someone can help and I don't mind if it is a completely different method then the Gridlayout
. Sorry if I did some things wrong or weirdly. I am new to the entire stackoverflow website and java.