2017-05-17 126 views
0

我试图让游戏看起来是这样的:Layout Design的Java布局适合我的布局

我试图找到这个最合适的布局设计。我尝试使用网格布局布局,但有一些组件未被正确添加。这是我的代码:

//Gridbag for the whole frame 
GridBagLayout gridbag = new GridBagLayout(); 
GridBagConstraints constraints = new GridBagConstraints(); 
this.setLayout(gridbag); 
constraints.fill = GridBagConstraints.BOTH; 
constraints.insets = new Insets(10, 10, 10, 10); 
constraints.weightx = 0.5; 

//Top left panel 
pnlHP = new JPanel(); 
pnlHP.setBackground(new Color (75, 55, 28)); 
gridbag.setConstraints(pnlHP, constraints); 
this.add(pnlHP); 

GridBagLayout gridbagHP = new GridBagLayout(); 
GridBagConstraints constraintsHP = new GridBagConstraints(); 
pnlHP.setLayout(gridbagHP); 
constraintsHP.fill = GridBagConstraints.BOTH; 
constraintsHP.insets = new Insets(10, 10, 10, 10); 
constraintsHP.weightx = 1.0; 

lblHPTitle = new JLabel(); 
lblHPTitle.setText("HP"); 
lblHPTitle.setForeground(Color.WHITE); 
lblHPTitle.setFont(new Font("Arial", Font.PLAIN, 60)); 
gridbagHP.setConstraints(lblHPTitle, constraintsHP); 
pnlHP.add(lblHPTitle); 

lblHP = new JLabel(); 
lblHP.setText("asdf"); 
lblHP.setForeground(Color.WHITE); 
lblHP.setFont(new Font("Arial", Font.PLAIN, 20)); 
lblHP.setHorizontalAlignment(SwingConstants.RIGHT); 
constraintsHP.gridwidth = GridBagConstraints.REMAINDER; 
gridbagHP.setConstraints(lblHP, constraintsHP); 
pnlHP.add(lblHP); 

pgbHP = new JProgressBar(); 
pgbHP.setBackground(new Color (75, 55, 28)); 
pgbHP.setValue(25); 
constraintsHP.weightx = 0.0; 
gridbagHP.setConstraints(pgbHP, constraintsHP); 
pnlHP.add(pgbHP); 

//Top center part 
btnGo = new JButton(); 
btnGo.setBackground(new Color (126, 72, 28)); 
btnGo.setText("Start Adventure!"); 
btnGo.setForeground(Color.WHITE); 
btnGo.setFont(new Font("Arial", Font.PLAIN, 42)); 
gridbag.setConstraints(btnGo, constraints); 
this.add(btnGo); 

//Top right panel 
pnlMPSP = new JPanel(); 
pnlMPSP.setBackground(new Color (75, 55, 28)); 
constraints.gridwidth = GridBagConstraints.REMAINDER; 
gridbag.setConstraints(pnlMPSP, constraints); 
this.add(pnlMPSP); 

GridBagLayout gridbagMPSP = new GridBagLayout(); 
GridBagConstraints constraintsMPSP = new GridBagConstraints(); 
pnlMPSP.setLayout(gridbagMPSP); 
constraintsMPSP.fill = GridBagConstraints.BOTH; 
constraintsMPSP.insets = new Insets(10, 10, 10, 10); 
constraintsMPSP.weightx = 1.0; 

lblMPSP = new JLabel(); 
lblMPSP.setText("asdf"); 
lblMPSP.setForeground(Color.WHITE); 
lblMPSP.setFont(new Font("Arial", Font.PLAIN, 20)); 
gridbagMPSP.setConstraints(lblMPSP, constraintsMPSP); 
pnlMPSP.add(lblMPSP); 

lblMPSPTitle = new JLabel(); 
lblMPSPTitle.setText("MP"); 
lblMPSPTitle.setForeground(Color.WHITE); 
lblMPSPTitle.setFont(new Font("Arial", Font.PLAIN, 60)); 
lblMPSPTitle.setHorizontalAlignment(SwingConstants.RIGHT); 
constraintsMPSP.gridwidth = GridBagConstraints.REMAINDER; 
gridbagMPSP.setConstraints(lblMPSPTitle, constraintsMPSP); 
pnlMPSP.add(lblMPSPTitle); 

pgbMPSP = new JProgressBar(); 
pgbMPSP.setBackground(new Color (0, 0, 255)); 
pgbMPSP.setValue(25); 
constraintsMPSP.weightx = 0.0; 
gridbagMPSP.setConstraints(pgbMPSP, constraintsMPSP); 
pnlMPSP.add(pgbMPSP); 

//Middle Left 
lblNotifications = new JLabel(); 
lblNotifications.setText("<html>N<br>o<br>t<br>i<br>f<br>i<br>c<br>a<br>t<br>i<br>o<br>n<br>s</html>"); 
lblNotifications.setFont(new Font("Arial", Font.PLAIN, 20)); 
lblNotifications.setBackground(Color.WHITE); 
gridbag.setConstraints(lblNotifications, constraints); 
this.add(lblNotifications); 

//Middle Center 
txtNotifCenter = new JTextPane(); 
txtNotifCenter.setBackground(new Color (205, 160, 96)); 
txtNotifCenter.setEnabled(false); 
txtNotifCenter.setDisabledTextColor(Color.black); 
scpNotifCenter = new JScrollPane(txtNotifCenter); 
scpNotifCenter.setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_AS_NEEDED); 
gridbag.setConstraints(scpNotifCenter, constraints); 
this.add(scpNotifCenter); 

//Middle Right 
txtXPInfo = new JTextPane(); 
txtXPInfo.setBackground(new Color (205, 160, 96)); 
txtXPInfo.setEnabled(false); 
txtXPInfo.setDisabledTextColor(Color.black); 
scpXPInfo = new JScrollPane(txtXPInfo); 
scpXPInfo.setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_AS_NEEDED); 
constraints.gridwidth = GridBagConstraints.REMAINDER; 
gridbag.setConstraints(scpXPInfo, constraints); 
this.add(scpXPInfo); 

//I haven't made the bottom part yet 

this.pack(); 

如果这是运行时,lblNotifications和txtNotifCenter(或scpNotifCenter)似乎有相同的宽度。我想让它像图片中那样。我是否应该使用其他布局,或者我只是以错误的方式使用网格布局布局?先谢谢你!

+2

一个可行的办法是不使用单一的布局,而是窝JPanels,每个使用自己的布局。另一种选择是使用更灵活的第三方布局,如MiGLayout。 –

+2

'我正试图为此找到最合适的布局设计。“ - 您从不受限于单个布局管理器。您可以使用不同的布局管理器嵌套每个面板,以实现所需的布局。因此,使用合适的布局管理器为一组组件合理设计屏幕的关键。 – camickr

+0

@DontKnowMuchButGettingBetter嵌套JPanel对程序非常重要? – Grandevox

回答

1

GridBagLayout非常难以使用,只有那些贪婪的人才会使用它。它实际上最初打算由GUI构建者使用,而不是用手编写。

您应该使用嵌套布局。你实际上有一个漂亮的标准布局。对于主窗口使用BorderLayout(这是所有顶级容器(如JFrame)的默认布局)。

您的HP,Start Adventure和MP将位于使用X_AXIS BoxLayout的JPanel中(JPanel默认使用FlowLayout,因此您必须将其设置为BoxLayout)。 JPanel将进入BorderLayout的NORTH位置。

您的两个通知文本的区域可能会走在最JSplitPane的(一个文本区域的左侧,右侧的其他文本区域),然后把拆分窗格中的BorderLayout的中心。

然后你的CharMode,Level和Logout将会在另一个使用X_AXIS BoxLayout的JPanel中。 JPanel将进入BorderLayout的南部位置。

教程边境和Box布局:

https://docs.oracle.com/javase/tutorial/uiswing/layout/border.html https://docs.oracle.com/javase/tutorial/uiswing/layout/box.html

拆分窗格:

https://docs.oracle.com/javase/tutorial/uiswing/components/splitpane.html

+0

谢谢!这与我试图达到的目标非常吻合! – Grandevox

+0

您可能需要考虑使用JavaFX而不是Swing(JavaFX是Swing的替代品)。使用JavaFX,您可以使用SceneBuilder来构建GUI(您仍然可以使用我描述的相同的总体布局......它具有HBox和VBox,而不是带有指定轴的BoxLayout)。另外,您可以使用类似CSS的语法来设计JavaFX组件的样式。你或许可以把它看起来完全像你的样机。 JavaFX入门链接:http://docs.oracle.com/javase/8/javase-clienttechnologies.htm CSS:http://docs.oracle.com/javase/8/javafx/user-interface-tutorial/css_tutorial .htm#JFXUI733 – Michael

+0

好的,我会研究它。谢谢! – Grandevox

1

不确定屏幕的哪些部分是组件,哪些不是(例如hp显示的是一个组件还是它们的一组?),但是GridBagLayout肯定可以做到你想要的。

关键不是尝试和一个GridBagLayout这样做,因为那样一切都需要排成一个网格。

将屏幕分为三行。在布局上使用以排列三行(例如BoxLayout),然后在每行内部使用单独的布局管理器(例如GridBagLayout)来布置该行内的组件。

+0

你能告诉我一个如何做的例子吗?或者至少一个链接将非常感谢! – Grandevox

+0

只需创建3个JPanel。把它们一个放在另一个下面。将第一行控件放在第一个JPanel中。中间JPanel的下一行。那个JPanel中最底层的那个。不管你喜欢如何布置每一行。 –

+0

好的,我会试试。谢谢你的建议! – Grandevox