java两个并排但大小固定的JPanel
我对整个java都是新手,我一直在尝试创建一个窗口,其中三分之二的窗口是文本框,另三分之一是空的JPanel(供以后使用),但我不确定如何执行。我尝试过Gridlayout
,但注意到您无法指定组件可以占用多少列。我真的不介意怎么做,但是有人能帮我吗
我已经搜索了多个其他帖子,比如这篇文章,其中有一个非常相似的概念,但得到了一个我尝试过的答案,但最终得到了一个我不喜欢的产品: Two JPanels side by side in a JFrame with one JPanel having a fixed width,我更喜欢这样的东西,它被锁定在左边的2/3和右边的1/3:Picture of TextPanel and JPanel
以下是我的尝试:
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;
}
}
我希望有人能帮助我,我不介意这是一种与Gridlayout
完全不同的方法。对不起,如果我做错了或是做了一些奇怪的事。我对整个stackoverflow网站和java都是新手
共 (0) 个答案