我需要 GUI 布局指导
将范围缩小到要点:
我的下一步是实现一个堆栈,但我想首先让它看起来正常。
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));
}
}
也许您可以使用
TitledBorder
,而不是在每个西/东/南面板的顶部使用标签。这将在面板周围放置一条矩形线,并在顶部显示标题。
阅读 Swing 教程中关于如何使用边框的部分,了解更多信息和工作示例。
如果您不想这样做,那么您可能需要将默认的 FlowLayout 或每个面板更改为其他布局。例如,您可以使用
BorderLayout
。然后将标签添加到 PAGE_START
并将其他组件添加到 CENTER
。要点是您可以嵌套具有不同布局的面板来实现您想要的布局。
这里有一些可以帮助您入门的东西。请阅读评论,并根据需要随时要求澄清。我没有做所有需要的布局更改:我只是为了演示应该做什么,所以你明白了。
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.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextArea;
import javax.swing.WindowConstants;
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());
// Creating JLabels / JTextArea
aTitle = new JLabel("Operations");
bTitle = new JLabel("Data Structure Contents");
west = new JPanel();
//you need to set layout manager to the panel, to lay out its components
west.setLayout(new BorderLayout());
west.setPreferredSize(new Dimension(200,200));
west.setBorder(BorderFactory.createLineBorder(Color.black));
west.setBackground(new Color(234,237,242));
add(west, BorderLayout.WEST);
west.add(aTitle, BorderLayout.NORTH);//use panel's layout manager
//you have 4 rows so GridLayout(3,2) is wrong
westSub1 = new JPanel(new GridLayout(4,2));
//for a grid layout: add components in the right order
push = new JButton("PUSH");
westSub1.add(push);
pushText = new JTextArea("pushtxt");
westSub1.add(pushText);
pop = new JButton("POP") ;
westSub1.add(pop);
popText = new JTextArea("poptxt");
westSub1.add(popText);
peek = new JButton("PEEK");
westSub1.add(peek);
peekText = new JTextArea("g");
westSub1.add(peekText);
test = new JButton("TEST");
westSub1.add(test);
resultText = new JTextArea("");
westSub1.add(resultText);
west.add(westSub1, BorderLayout.CENTER);//use panel's layout manager
east = new JPanel();
east.setPreferredSize(new Dimension(400,100));
east.setBorder(BorderFactory.createLineBorder(Color.black));
east.setBackground(new Color(255,255,255));
east.add(bTitle);
add(east, BorderLayout.CENTER);
south = new JPanel();
//you need to set layout manager to the panel, to lay out its components
south.setLayout(new BorderLayout());
south.setPreferredSize(new Dimension(100,150));
south.setBorder(BorderFactory.createLineBorder(Color.black));
south.setBackground(new Color(240,240,240));
add(south, BorderLayout.SOUTH);
cTitle = new JLabel("Information");
south.add(cTitle, BorderLayout.NORTH); //use panel's layout manager
infoText = new JTextArea("This is where commands will be displayed.");
south.add(infoText, BorderLayout.CENTER);
}
public static void main(String[] args){
JFrame frame = new JFrame();
frame.setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
frame.getContentPane().add(new StackPanel());
frame.pack();
frame.setVisible(true);
}
}
代码拥有它需要的所有导入,就像墙一样运行它。
或 MigLayout 可能是一个可以使用的选项,它们很复杂,但使用起来并不太复杂。