我正在尝试创建一个应用程序,它将使用一些基本的github信息垂直列出组件。
我最初有这个
我想用可点击的框架(或至少带按钮的框架)替换按钮,如下所示:
____________________________________
|_____JLabel_____|_____JLabel______|
| |
|_________Fixed-length label_______|
| |
|_______________Button_____________|
我对Swing开发很新,所以我不知道任何设计习语。请原谅杂乱的代码。这是我尝试过的。
JPanel [] panels;
private void createStubs(PriorityQueue<Issue> issues) {
int i = 0;
while(!issues.isEmpty()) {
Issue issue = issues.poll();
// Create components
JPanel panOuter = new JPanel(new BorderLayout());
JPanel panTop = new JPanel(new GridBagLayout());
JPanel panTopRight = new JPanel(new BorderLayout());
JPanel panTopLeft = new JPanel(new BorderLayout());
JPanel panMiddle = new JPanel(new BorderLayout());
JPanel panBottom = new JPanel();
panTop.setBorder(BorderFactory.createEmptyBorder(5, 5, 5, 5));
panTopLeft.setBorder(BorderFactory.createEmptyBorder(5, 5, 5, 5));
panTopRight.setBorder(BorderFactory.createEmptyBorder(5, 5, 5, 5));
panBottom.setBorder(BorderFactory.createEmptyBorder(5, 5, 5, 5));
panMiddle.setBorder(BorderFactory.createEmptyBorder(5, 5, 5, 5));
// Add components to content panel
panOuter.add(panMiddle, BorderLayout.CENTER);
panOuter.add(panBottom, BorderLayout.SOUTH);
panOuter.add(panTop, BorderLayout.NORTH);
JLabel lblTopLeft = new JLabel(issue.getTitle(), JLabel.CENTER);
JLabel lblTopRight = new JLabel(issue.getAuthor().getLogin(), JLabel.CENTER);
JLabel lblCenter = new JLabel(issue.getBodyShort(), JLabel.CENTER);
JButton btnBottom = new JButton("Check out this issue");
panMiddle.add(lblCenter);
panBottom.add(btnBottom);
GridBagConstraints gbc = new GridBagConstraints();
gbc.gridwidth = GridBagConstraints.REMAINDER;
panTopLeft.add(lblTopLeft, BorderLayout.NORTH);
panTopRight.add(lblTopRight, BorderLayout.NORTH);
panTop.add(panTopLeft, gbc);
panTop.add(panTopRight, gbc);
panOuter.add(panMiddle, BorderLayout.CENTER);
panOuter.add(panBottom, BorderLayout.SOUTH);
panOuter.add(panTop, BorderLayout.NORTH);
addPanels(panOuter, i);
++i;
}
}
private void addPanels(JPanel panel, int i) {
panels[i] = panel;
add(panels[i]);
}
所以它现在看起来像这样
我怎样才能让标题和作者在同一条线上?在问题之间添加换行符并使其可滚动也很好。
谢谢, erip
答案 0 :(得分:2)
有几种方法可以实现所需的布局。在这里,我使用GridBagLayout
作为内部面板。我添加颜色只是为了显示组件占用的空间。
包含的面板使用GridLayout
并添加到滚动窗格中。
public class Test extends JFrame {
Test() {
JPanel main = new JPanel(new GridLayout(0, 1));
for (int i = 0; i < 20; i ++)
main.add(new IssuePanel());
getContentPane().add(new JScrollPane(main));
setDefaultCloseOperation(EXIT_ON_CLOSE);
pack();
setLocationRelativeTo(null);
setVisible(true);
}
private class IssuePanel extends JPanel {
IssuePanel() {
setBorder(new LineBorder(Color.BLUE));
setLayout(new GridBagLayout());
GridBagConstraints c = new GridBagConstraints();
JLabel topLeft = new JLabel("Top Left");
topLeft.setOpaque(true);
topLeft.setBackground(Color.RED);
c.gridx = 0;
c.gridy = 0;
add(topLeft, c);
JLabel topRight = new JLabel("Top Right");
topRight.setOpaque(true);
topRight.setBackground(Color.GREEN);
c.gridx = 1;
c.gridy = 0;
add(topRight, c);
JLabel middle = new JLabel("Middle");
middle.setOpaque(true);
middle.setBackground(Color.YELLOW);
c.gridwidth = GridBagConstraints.REMAINDER;
c.gridx = 0;
c.gridy = 1;
add(middle, c);
JButton button = new JButton("Button");
button.setOpaque(true);
button.setBackground(Color.ORANGE);
c.gridx = 0;
c.gridy = 2;
add(button, c);
}
}
public static void main(String[] args) {
new Test();
}
}