我尝试创建自定义界面,例如daches显示的界面:
-----------------------------------------------------------------
- ----------------------------------------------------------- -
- - - -
- ----------------------------------------------------------- -
- -
- ---------- -
- - - -
- ---------- -
- -
- ---------- ----------------------------------- ---------- -
- - - - - - - -
- ---------- ----------------------------------- ---------- -
- ---------- ---------- -
- - - - - -
- ---------- ---------- -
-----------------------------------------------------------------
我按照示例GroupLayout Example
进行了操作以下是我使用的代码:
GroupLayout layout = new GroupLayout(getContentPane());
getContentPane().setLayout(layout);
layout.setAutoCreateGaps(true);
layout.setAutoCreateContainerGaps(true);
layout.setHorizontalGroup(layout.createParallelGroup(LEADING)
.addComponent(msgLbl)
.addGroup(layout.createSequentialGroup()
.addGroup(layout.createParallelGroup(LEADING)
.addComponent(empty)
.addComponent(fldrLbl3)
.addComponent(empty))
.addGroup(layout.createParallelGroup(LEADING)
.addGroup(layout.createSequentialGroup()
.addComponent(empty)
.addComponent(timerLabel)
.addComponent(empty))
.addComponent(fldr)
.addGroup(layout.createSequentialGroup()
.addComponent(empty)
.addComponent(empty)
.addComponent(strtButton)))
.addGroup(layout.createParallelGroup(LEADING)
.addComponent(empty)
.addComponent(chFldrButton)
.addComponent(PstPndButton))
));
layout.linkSize(SwingConstants.VERTICAL, empty, empty, empty, strtButton, PstPndButton);
layout.setVerticalGroup(layout.createSequentialGroup()
.addComponent(msgLbl)
.addGroup(layout.createParallelGroup(LEADING)
.addComponent(empty)
.addComponent(empty)
.addComponent(timerLabel)
.addComponent(empty)
.addComponent(empty))
.addGroup(layout.createParallelGroup(LEADING)
.addComponent(fldrLbl3)
.addComponent(fldr)
.addComponent(chFldrButton))
.addGroup(layout.createParallelGroup(LEADING)
.addComponent(empty)
.addComponent(empty)
.addComponent(empty)
.addComponent(strtButton)
.addComponent(PstPndButton))
);
但它没有正确显示某些原因。我想我错过了什么,你能帮助我吗?
// *********** ******** // 我缺少的是使用Alignment enum:LEADING,TRAILING,CENTER和BASELINE。
当我跟随时更加清晰:How to Use GroupLayout
对于未来用户来说,正确的方法是:
layout.setHorizontalGroup(layout.createSequentialGroup()
.addComponent(fldrLbl3)
.addGroup(layout.createParallelGroup()
.addGroup(layout.createParallelGroup(CENTER)
.addComponent(msgLbl)
.addComponent(timerLabel))
.addGroup(layout.createParallelGroup(TRAILING )
.addComponent(fldr)
.addComponent(strtButton)) )
.addGroup(layout.createParallelGroup(LEADING)
.addComponent(chFldrButton)
.addComponent(PstPndButton))
);
layout.setVerticalGroup(layout.createSequentialGroup()
.addGroup(layout.createParallelGroup(CENTER)
.addComponent(msgLbl))
.addGroup(layout.createParallelGroup(CENTER)
.addComponent(timerLabel))
.addGroup(layout.createParallelGroup(BASELINE)
.addComponent(fldrLbl3)
.addComponent(fldr)
.addComponent(chFldrButton))
.addGroup(layout.createParallelGroup(BASELINE)
.addComponent(strtButton)
.addComponent(PstPndButton)
)
);
答案 0 :(得分:5)
以下是输出,使用Nested Layout
import java.awt.*;
import java.util.Random;
import javax.swing.*;
public class NestedLayout {
private static final int GAP = 5;
private Random random;
public NestedLayout () {
random = new Random ();
}
private void displayGUI () {
JFrame frame = new JFrame ( "Nested Layout Example" );
frame.setDefaultCloseOperation ( JFrame.DISPOSE_ON_CLOSE );
JPanel contentPane = new JPanel ();
contentPane.setBorder ( BorderFactory.createEmptyBorder (
GAP, GAP, GAP, GAP) );
contentPane.setLayout ( new GridLayout ( 4, 1, GAP, GAP ) );
contentPane.add ( getHeaderPanel () );
contentPane.add ( getMiddlePanel () );
contentPane.add ( getFooterPanel () );
contentPane.add ( getPSPanel () );
frame.setContentPane ( contentPane );
frame.pack ();
frame.setLocationByPlatform ( true );
frame.setVisible ( true );
}
private JPanel getHeaderPanel () {
JPanel panel = new JPanel ();
panel.setLayout ( new BorderLayout ( GAP, GAP) );
panel.setBorder ( BorderFactory.createEmptyBorder (
GAP, GAP, GAP, GAP) );
panel.add ( getLabel ( "Header JLabel" ), BorderLayout.CENTER );
return panel;
}
private JPanel getMiddlePanel () {
JPanel panel = new JPanel ();
panel.setLayout ( new GridBagLayout () );
panel.setBorder ( BorderFactory.createEmptyBorder (
GAP, GAP, GAP, GAP) );
panel.add ( getLabel ( "Middle JLabel" ) );
return panel;
}
private JPanel getFooterPanel () {
JPanel panel = new JPanel ();
panel.setLayout ( new BorderLayout ( GAP, GAP) );
panel.setBorder ( BorderFactory.createEmptyBorder (
GAP, GAP, GAP, GAP) );
panel.add ( getLabel ( "Left JLabel" ), BorderLayout.LINE_START );
panel.add ( getLabel ( "Center JLabel" ), BorderLayout.CENTER );
panel.add ( getLabel ( "Right JLabel" ), BorderLayout.LINE_END );
return panel;
}
private JPanel getPSPanel () {
JPanel panel = new JPanel ();
panel.setLayout ( new FlowLayout ( FlowLayout.RIGHT, GAP, GAP) );
panel.setBorder ( BorderFactory.createEmptyBorder (
GAP, GAP, GAP, GAP) );
panel.add ( getLabel ( "First JLabel" ) );
panel.add ( getLabel ( "Second JLabel" ) );
return panel;
}
private JLabel getLabel ( String text ) {
JLabel label = new JLabel ( text, JLabel.CENTER );
label.setOpaque ( true );
label.setBackground ( getRandomColour () );
return label;
}
private Color getRandomColour () {
return new Color ( random.nextFloat (), random.nextFloat (),
random.nextFloat (), random.nextFloat () );
}
public static void main ( String[] args ) {
Runnable runnable = new Runnable () {
@Override
public void run () {
new NestedLayout ().displayGUI ();
}
};
EventQueue.invokeLater ( runnable );
}
}
<强>输出:强>