问题描述
我是整个 Java 的新手,我一直在尝试制作一个窗口,其中 2/3 的窗口是文本框,另一个是空的 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 的新手。
解决方法
暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!
如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。
小编邮箱:dio#foxmail.com (将#修改为@)