gpt4 book ai didi

java - 单选按钮的垂直滚动条

转载 作者:行者123 更新时间:2023-12-02 01:11:57 24 4
gpt4 key购买 nike

我是一个非常新的 Java 程序员。

我正在尝试创建带有滚动条的单选按钮的垂直列表。我的代码不会产生指定大小的窗口,滚动条也不会实际出现。

import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.Toolkit;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

import javax.swing.Box;
import javax.swing.ButtonGroup;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JRadioButton;
import javax.swing.JScrollPane;

public class RadioButtonScroller {
private List<String> abcList = new ArrayList<>(Arrays.asList("Aa", "Bb", "Cc", "Dd", "Ee", "Ff", "Gg", "Hh", "Ii",
"Jj", "Kk", "Ll", "Mm", "Nn", "Oo", "Pp", "Qq", "Rr", "Ss", "Tt", "Uu", "Vv", "Ww", "Xx", "Yy", "Zz"));
private JRadioButton pickButton;
private JFrame jframe = new JFrame("Select one:");
private JScrollPane scrollPane;
private JPanel panel = new JPanel();
private Box vertBox;

public RadioButtonScroller() {
createUI();
}

public void setRadioButtons() {
ButtonGroup group = new ButtonGroup();

for (int i = 0; i < abcList.size(); i++) {
String theseLetters = abcList.get(i);
pickButton = new JRadioButton(theseLetters);

group.add(pickButton);
vertBox.add(pickButton);
}
}

private void createUI() {
//centers on screen
Dimension dim = Toolkit.getDefaultToolkit().getScreenSize();
jframe.setLocationRelativeTo(null);
jframe.setLocation(dim.width / 3 - jframe.getSize().width / 3, dim.height / 4 - jframe.getSize().height / 4);

vertBox = Box.createVerticalBox();
jframe.setSize(300, 525);
vertBox.setSize(300, 525);
jframe.setVisible(true);

panel.setLayout(new FlowLayout());
jframe.add(panel);
scrollPane = new JScrollPane(vertBox);
scrollPane.setSize(300, 525);
scrollPane.getVerticalScrollBar();
panel.add(scrollPane);

jframe.pack();
jframe.setVisible(true);
jframe.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setRadioButtons();
}

public static void main(String[] args) {
RadioButtonScroller radioButtonScroller = new RadioButtonScroller();
}
}

我之前已经成功地将 JFrame 与 Vbox 一起使用,但是 JPanel 和 JScrollPane 的添加让我感到困惑。

(由于某些兼容性问题,我无法使用 JavaFX。必须正在运行。)

非常感谢。

最佳答案

这里有很多布局问题

  1. 布局管理器在大多数情况下并不考虑组件的大小,而是考虑其首选大小。
  2. JScrollPane 的大小由其视口(viewport)的首选大小控制。
  3. FlowLayout 非常基本且无情,通常无法很好地处理复杂的组合布局。
  4. 不要在打包 JFrame 之后添加组件,而是在打包 JFrame 之前添加组件。否则组件和布局管理器将不会相应地调整自身大小。

例如,

import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.GridLayout;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import javax.swing.*;

@SuppressWarnings("serial")
public class RadioButtonScroller2 extends JPanel {
private List<String> abcList = new ArrayList<>(Arrays.asList("Aa", "Bb", "Cc", "Dd", "Ee", "Ff", "Gg", "Hh", "Ii",
"Jj", "Kk", "Ll", "Mm", "Nn", "Oo", "Pp", "Qq", "Rr", "Ss", "Tt", "Uu", "Vv", "Ww", "Xx", "Yy", "Zz"));
private ButtonGroup buttonGroup = new ButtonGroup();

public RadioButtonScroller2() {
JPanel radioPanel = new JPanel(new GridLayout(0, 1));
ItemListener il = itemEvent -> {
if (itemEvent.getStateChange() == ItemEvent.SELECTED) {
AbstractButton btn = (AbstractButton) itemEvent.getSource();
System.out.println("Selection: " + btn.getActionCommand());
}
};
for (String radioText : abcList) {
JRadioButton radioButton = new JRadioButton(radioText);
radioButton.setActionCommand(radioText);
radioButton.addItemListener(il);
radioPanel.add(radioButton);
buttonGroup.add(radioButton);
}

JScrollPane scrollPane = new JScrollPane(radioPanel);
scrollPane.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);
scrollPane.getViewport().setPreferredSize(new Dimension(150, 200));

setLayout(new BorderLayout());
add(scrollPane);
}

private static void createAndShowGui() {
RadioButtonScroller2 mainPanel = new RadioButtonScroller2();

JFrame frame = new JFrame("Test");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.getContentPane().add(mainPanel);
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}

public static void main(String[] args) {
SwingUtilities.invokeLater(() -> createAndShowGui());
}
}

关于java - 单选按钮的垂直滚动条,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59256308/

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