-2
我需要關於java swing佈局的幫助。我想要做這樣的任何事物(請參見圖片):我如何做擺動複雜佈局Java
我試圖把網格包佈局,但我不能與內容10:00(上圖)插入floatting的JPanel。任何人都可以幫助我?
我需要關於java swing佈局的幫助。我想要做這樣的任何事物(請參見圖片):我如何做擺動複雜佈局Java
我試圖把網格包佈局,但我不能與內容10:00(上圖)插入floatting的JPanel。任何人都可以幫助我?
在大多數情況下,你通常需要考慮使用多個佈局,但在你的情況下,GridBagLayout
應該是能夠實現你想要的基本佈局
對於需要拓展多列組件,您可以使用GridBagConstraints#gridwidth
,同樣,對於需要擴展多行GridBagConstraints#gridheight
作爲一個整體,打破你佈局的要求爲職責的各個領域,例如,記分卡他們的自我,有自己的佈局要求,並應組件是自包含的組件,這將減少佈局的複雜性。
更新
你可以做一個容器中的(核心)的佈局,但實在是太大了容易折斷,相反,你應該嘗試向下突破UI段...
這些都是,兩個核心部分,我看到的,也有在其中小節,但是這是我一開始...
import java.awt.Color;
import java.awt.Dimension;
import java.awt.EventQueue;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;
import javax.swing.border.LineBorder;
public class LayoutTest {
public static void main(String[] args) {
new LayoutTest();
}
public LayoutTest() {
EventQueue.invokeLater(new Runnable() {
@Override
public void run() {
try {
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
} catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
ex.printStackTrace();
}
JFrame frame = new JFrame("Testing");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.add(new TestPane());
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
});
}
public class TestPane extends JPanel {
public TestPane() {
setLayout(new GridBagLayout());
GridBagConstraints gbc = new GridBagConstraints();
gbc.gridx = 0;
gbc.gridy = 0;
gbc.gridheight = GridBagConstraints.REMAINDER;
gbc.weighty = 1;
gbc.fill = GridBagConstraints.HORIZONTAL;
add(new ScoreCardsPane(), gbc);
gbc.gridx = 1;
add(new TimePane(), gbc);
}
}
public class TimePane extends JPanel {
public TimePane() {
setLayout(new GridBagLayout());
GridBagConstraints gbc = new GridBagConstraints();
gbc.gridx = 0;
gbc.gridy = 0;
gbc.insets = new Insets(4, 4, 4, 4);
add(createScoreCard(), gbc);
gbc.gridx++;
add(createScoreCard(), gbc);
gbc.gridx = 0;
gbc.gridy = 2;
add(createScoreCard(), gbc);
gbc.gridx++;
add(createScoreCard(), gbc);
gbc.gridx = 0;
gbc.gridy = 1;
gbc.weightx = 1;
gbc.weighty = 1;
gbc.anchor = GridBagConstraints.EAST;
gbc.gridwidth = 3;
add(new TimeCard(), gbc);
}
}
public class TimeCard extends JPanel {
public TimeCard() {
setLayout(new GridBagLayout());
GridBagConstraints gbc = new GridBagConstraints();
gbc.gridwidth = GridBagConstraints.REMAINDER;
gbc.weightx = 1;
gbc.fill = GridBagConstraints.HORIZONTAL;
add(new JLabel("TEMPO"), gbc);
JLabel time = new JLabel("10:00");
time.setBorder(new LineBorder(Color.BLACK));
add(time, gbc);
}
}
public class ScoreCardsPane extends JPanel {
public ScoreCardsPane() {
setLayout(new GridBagLayout());
GridBagConstraints gbc = new GridBagConstraints();
gbc.gridx = 0;
gbc.gridy = 0;
gbc.gridwidth = GridBagConstraints.REMAINDER;
gbc.insets = new Insets(4, 4, 4, 4);
gbc.anchor = GridBagConstraints.WEST;
JLabel topLabel = new JLabel("PONTOS");
add(topLabel, gbc);
JLabel bottomLabel = new JLabel("PONTOS");
gbc.gridy = 3;
add(bottomLabel, gbc);
gbc.anchor = GridBagConstraints.CENTER;
gbc.gridy = 1;
gbc.gridwidth = 1;
add(createScoreCard(), gbc);
gbc.gridx++;
add(createScoreCard(), gbc);
gbc.gridx++;
add(createScoreCard(), gbc);
gbc.gridy++;
gbc.gridx = 0;
add(createScoreCard(), gbc);
gbc.gridx++;
add(createScoreCard(), gbc);
gbc.gridx++;
add(createScoreCard(), gbc);
}
}
protected static JPanel createScoreCard() {
JPanel card = new JPanel() {
@Override
public Dimension getPreferredSize() {
return new Dimension(100, 100);
}
};
card.setBackground(Color.RED);
return card;
}
}
請顯示您的[最小代碼嘗試](http://stackoverflow.com/help/mcve),讓我們知道它如何不適合您。 – 2014-10-16 18:52:58