gpt4 book ai didi

java - 如何在 Swing 中创建这个组件?

转载 作者:行者123 更新时间:2023-11-30 03:13:16 26 4
gpt4 key购买 nike

我正在尝试创建一个应用程序,该应用程序将垂直列出具有一些基本 github 信息的组件。

我本来就有这个

Current progress

我想用可点击的框架(或至少带有按钮的框架)替换按钮,如下所示:

____________________________________
|_____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]);
}

现在看起来像这样

Little better

如何才能真正使标题和作者在同一行?最好在问题之间添加换行符并使其可滚动。

谢谢,埃里普

最佳答案

有几种方法可以实现您想要的布局。这里我使用 GridBagLayout 作为内部面板。我添加颜色只是为了显示组件占用的空间。包含面板使用 GridLayout 并添加到滚动 Pane 。

enter image description here

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();
}
}

关于java - 如何在 Swing 中创建这个组件?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33198547/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com