我需要GUI布局的指导
把它缩小到主要的几点:
- 我有三个主要的JPanel(信息部分,操作和数据结构)
- 如果不移动JLabel,我就无法填充这些
- 我需要一个子面板的操作与网格布局(不能得到这个工作,它真的很烦我现在)
- 我需要它看起来像下面的图片
- 红色分隔线是可选的,使它更整洁一点
我的下一步是实现一个堆栈,但我想首先使它看起来正常。
的数据
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.GridLayout;
import javax.swing.BorderFactory;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextArea;
public class StackPanel extends JPanel {
JPanel west, westSub1, east, south, southSub1;
JTextArea infoText, popText, pushText, peekText, resultText;
JLabel aTitle, bTitle, cTitle, Result;
JButton push, pop, peek, test;
public StackPanel() {
// Creating JPanels
setLayout(new BorderLayout());
west = new JPanel();
westSub1 = new JPanel(new GridLayout(3,2));
east = new JPanel();
south = new JPanel();
west.add(westSub1);
// Creating JLabels / JTextArea
aTitle = new JLabel("Operations");
bTitle = new JLabel("Data Structure Contents");
cTitle = new JLabel("Information");
infoText = new JTextArea("This is where commands will be displayed.");
pushText = new JTextArea("pushtxt");
popText = new JTextArea("poptxt");
peekText = new JTextArea("g");
resultText = new JTextArea("");
west.add(aTitle);
westSub1.add(pushText);
westSub1.add(popText);
westSub1.add(peekText);
westSub1.add(resultText);
east.add(bTitle);
south.add(cTitle);
south.add(infoText);
// Creating & Adding JButtons
push = new JButton("PUSH");
pop = new JButton("POP") ;
peek = new JButton("PEEK");
test = new JButton("TEST");
westSub1.add(push);
westSub1.add(pop);
westSub1.add(peek);
westSub1.add(test);
// Setting the placements of GUI objects
add(west, BorderLayout.WEST);
add(east, BorderLayout.CENTER);
add(south, BorderLayout.SOUTH);
// Declaring JPanel sizes // Width|Height
west.setPreferredSize(new Dimension(200,200));
east.setPreferredSize(new Dimension(400,100));
south.setPreferredSize(new Dimension(100,150));
// Setting black borders for JPanels
west.setBorder(BorderFactory.createLineBorder(Color.black));
east.setBorder(BorderFactory.createLineBorder(Color.black));
south.setBorder(BorderFactory.createLineBorder(Color.black));
// Setting JPanel background colours
west.setBackground(new Color(234,237,242));
east.setBackground(new Color(255,255,255));
south.setBackground(new Color(240,240,240));
}
}
字符串
4条答案
按热度按时间wvmv3b1j1#
也许你可以使用
TitledBorder
代替在西/东/南面板的顶部使用标签。这将在面板周围放置一条矩形线,顶部有一个标题。请阅读Swing教程中关于How to Use Borders的部分,以了解更多信息和工作示例。
如果你不想这样做,那么你可能需要将默认的FlowLayout或每个面板更改为另一个布局。例如,你可以使用
BorderLayout
。然后将标签添加到PAGE_START
,将其他组件添加到CENTER
。要点是你可以使用不同的布局嵌套面板以实现你想要的布局。polhcujo2#
这里有一些东西可以让你开始。请阅读评论。我没有做所有需要的布局更改:我只是要演示应该做什么,所以你得到的想法。
字符串
这段代码有它需要的所有导入,就像运行
main
一样。oxf4rvwz3#
基本的Swing布局管理器要么非常初级,要么使用起来非常复杂。因此,可能可以选择使用FormLayout或MigLayout,它们很复杂,但使用起来并不复杂。
c8ib6hqw4#
我使用Swing已经是很久以前的事了。但是我在Swing的默认布局上遇到了同样的问题。我的建议是改为TableLayout。它非常容易使用,你可以得到精确的结果。
请查看教程:http://www.clearthought.info/sun/products/jfc/tsc/articles/tablelayout/Simple.html
不完美,但我会像这样的机会你的代码
字符串