如何在 JOptionPane 中垂直排列按钮?
How do I arrange buttons vertically in a JOptionPane?
我正在尝试制作一个基于文本的冒险游戏,其中屏幕顶部是 JScrollPane 内的一个 JTextArea,它显示正在发生的事情,底部是一个 JOptionPane,您可以在其中单击一个按钮来制作选择。默认情况下,按钮水平排列。唯一的问题是,如果我的按钮太多,就没有空间容纳新按钮,它们就会被推离屏幕。我需要将它们垂直排列,因为它们比身高更胖。 JOptionPane 和 JScrollPane 嵌套在 gridLayout 中,而 gridLayout 嵌套在 JFrame 中。这是我用来制作框架的方法:
/**
* Make the frame and everything in it
*/
private void makeFrame()
{
frame = new JFrame("Adventure!");
JPanel contentPane = (JPanel)frame.getContentPane();
contentPane.setBorder(new EmptyBorder(6, 6, 6, 6));
contentPane.setLayout(new GridLayout(0, 1));
textArea = new JTextArea(20, 50);
textArea.setEditable(false);
textArea.setLineWrap(true);
textArea.setFont(new Font("font", Font.BOLD, 15));
JScrollPane scrollPane = new JScrollPane(textArea);
contentPane.add(textArea);
optionPane = new JOptionPane("", JOptionPane.DEFAULT_OPTION, JOptionPane.DEFAULT_OPTION, null, null);
contentPane.add(optionPane);
frame.pack();
// place the frame at the center of the screen and show
Dimension d = Toolkit.getDefaultToolkit().getScreenSize();
frame.setLocation(d.width/2 - frame.getWidth()/2, d.height/2 - frame.getHeight()/2);
frame.setVisible(true);
}
不使用 JOptionPane
,而是在 GridLayout
中使用 JButtons
。您可以像这样指定在创建时需要跨越和向下的组件数量:new GridLayout(0, 3)
。这将导致 3 个按钮堆叠在一起,第一个 int
是你想要跨越的数量,第二个是你想要向下的数量。试试这个:
/**
* Make the frame and everything in it
*/
private void makeFrame()
{
frame = new JFrame("Adventure!");
JPanel contentPane = (JPanel)frame.getContentPane();
contentPane.setBorder(new EmptyBorder(6, 6, 6, 6));
contentPane.setLayout(new GridLayout(0, 1));
textArea = new JTextArea(20, 50);
textArea.setEditable(false);
textArea.setLineWrap(true);
textArea.setFont(new Font("font", Font.BOLD, 15));
JScrollPane scrollPane = new JScrollPane(textArea);
contentPane.add(textArea);
//This replaces your JOptionPane block
buttonPane = new JPanel();
buttonPane.setLayout(new GridLayout(0, 1));
contentPane.add(buttonPane);
frame.pack();
// place the frame at the center of the screen and show
Dimension d = Toolkit.getDefaultToolkit().getScreenSize();
frame.setLocation(d.width/2 - frame.getWidth()/2, d.height/2 - frame.getHeight()/2);
frame.setVisible(true);
}
我正在尝试制作一个基于文本的冒险游戏,其中屏幕顶部是 JScrollPane 内的一个 JTextArea,它显示正在发生的事情,底部是一个 JOptionPane,您可以在其中单击一个按钮来制作选择。默认情况下,按钮水平排列。唯一的问题是,如果我的按钮太多,就没有空间容纳新按钮,它们就会被推离屏幕。我需要将它们垂直排列,因为它们比身高更胖。 JOptionPane 和 JScrollPane 嵌套在 gridLayout 中,而 gridLayout 嵌套在 JFrame 中。这是我用来制作框架的方法:
/**
* Make the frame and everything in it
*/
private void makeFrame()
{
frame = new JFrame("Adventure!");
JPanel contentPane = (JPanel)frame.getContentPane();
contentPane.setBorder(new EmptyBorder(6, 6, 6, 6));
contentPane.setLayout(new GridLayout(0, 1));
textArea = new JTextArea(20, 50);
textArea.setEditable(false);
textArea.setLineWrap(true);
textArea.setFont(new Font("font", Font.BOLD, 15));
JScrollPane scrollPane = new JScrollPane(textArea);
contentPane.add(textArea);
optionPane = new JOptionPane("", JOptionPane.DEFAULT_OPTION, JOptionPane.DEFAULT_OPTION, null, null);
contentPane.add(optionPane);
frame.pack();
// place the frame at the center of the screen and show
Dimension d = Toolkit.getDefaultToolkit().getScreenSize();
frame.setLocation(d.width/2 - frame.getWidth()/2, d.height/2 - frame.getHeight()/2);
frame.setVisible(true);
}
不使用 JOptionPane
,而是在 GridLayout
中使用 JButtons
。您可以像这样指定在创建时需要跨越和向下的组件数量:new GridLayout(0, 3)
。这将导致 3 个按钮堆叠在一起,第一个 int
是你想要跨越的数量,第二个是你想要向下的数量。试试这个:
/**
* Make the frame and everything in it
*/
private void makeFrame()
{
frame = new JFrame("Adventure!");
JPanel contentPane = (JPanel)frame.getContentPane();
contentPane.setBorder(new EmptyBorder(6, 6, 6, 6));
contentPane.setLayout(new GridLayout(0, 1));
textArea = new JTextArea(20, 50);
textArea.setEditable(false);
textArea.setLineWrap(true);
textArea.setFont(new Font("font", Font.BOLD, 15));
JScrollPane scrollPane = new JScrollPane(textArea);
contentPane.add(textArea);
//This replaces your JOptionPane block
buttonPane = new JPanel();
buttonPane.setLayout(new GridLayout(0, 1));
contentPane.add(buttonPane);
frame.pack();
// place the frame at the center of the screen and show
Dimension d = Toolkit.getDefaultToolkit().getScreenSize();
frame.setLocation(d.width/2 - frame.getWidth()/2, d.height/2 - frame.getHeight()/2);
frame.setVisible(true);
}