我是一个非常新的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。必须处于摇摆状态。)
非常感谢。
答案 0 :(得分:1)
它是您在panel.setLayout(new FlowLayout());
中使用的布局。 JScrollPane
的流布局“效果不佳”。您可以更改布局,而无需做其他工作即可获得功能正常的滚动窗格。 (为了使流程布局能够与JScrollPane一起使用,您将不得不进行进一步的研究,可能与组件侦听器等一起使用)
在您的情况下,我将使用带有1列,无限行的GridLayout:
panel.setLayout(new GridLayout(0, 1));
或具有y轴的BoxLayout:
panel.setLayout(new BoxLayout(panel, BoxLayout.Y_AXIS));
此外,这些行不起作用:
scrollPane.setSize(300, 525);
scrollPane.getVerticalScrollBar();
滚动窗格的大小将从父容器(即JFrame的内容窗格)计算得出。
答案 1 :(得分:1)
这里有很多布局问题
例如
databaseReference = FirebaseDatabase.getInstance().getReference();
databaseReference.child(userID).addListenerForSingleValueEvent(new ValueEventListener() {
@Override
public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
for (DataSnapshot data : dataSnapshot.getChildren()){
Author author = data.getValue(Author.class);
authorList.add(author);
}
...
}
@Override
public void onCancelled(@NonNull DatabaseError databaseError) {
}
});
答案 2 :(得分:0)
两个回答都使我回答了我,谢谢你们! 非常感谢George Z.和Eover HoverCraft指出:
panel.setLayout(new FlowLayout());
不起作用。 乔治Z明确建议使用:
panel.setLayout (new GridLayout(0, 1));
对我有用的
乔治Z.明确指出:
scrollPane.setSize(300, 525);
scrollPane.getVerticalScrollBar();
并没有完成任何事情,正是Hovercraft Full Of Eels提到了首选尺寸,这使我一直都得到了答案(并弄清楚了我在尝试做错什么的地方)完成这2条浪费的代码行)。
充满鳗鱼的气垫船还帮助我了解了JFrame实际执行的“打包”操作。 你们都提供了所需的东西,非常感谢。这是一个简短的可行答案:
import java.awt.Dimension;
import java.awt.GridLayout;
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);
}
jframe.pack();
jframe.setVisible(true);
jframe.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
private void createUI() {
// center 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 GridLayout(0, 1));
jframe.add(panel);
scrollPane = new JScrollPane(vertBox);
scrollPane.setPreferredSize(new Dimension(300, 525));
panel.add(scrollPane);
setRadioButtons();
}
public static void main(String[] args) {
RadioButtonScroller radioButtonScroller = new RadioButtonScroller();
}
}