我有 9 个 jbuttons 添加到 jpanel 和面板添加到 jscrollpane 并添加到 jframe。
http://www.pic1.iran-forum.ir/images/up9/95426323683658592564.jpg
当我通过以下方式更改框架方向时:
applyComponentOrientation(ComponentOrientation.RIGHT_TO_LEFT);
面板向右移动并且按钮的大小得到固定并且不会填充面板但是您在下图中看到滚动条填充了面板的所有宽度
http://www.pic1.iran-forum.ir/images/up9/60975202722295688553.jpg
(我使用 gridbaglayout 添加按钮和borderlayout.center 添加滚动窗格)。
这是java中的错误还是?
编辑:它是最简单的视图。它有帮助吗?
import java.awt.BorderLayout;
import java.awt.ComponentOrientation;
import java.awt.Dimension;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import javax.swing.*;
public class MyFrame extends JFrame{
private JButton[] arrayButton = new JButton[9];
private JButton btnLeft = new JButton("<");
private JButton btnRight = new JButton(">");
private JScrollPane scpButtons = new JScrollPane();
public MyFrame() {
for (int i = 0; i < arrayButton.length; i++)
arrayButton[i] = new JButton("btn");
JPanel pnlButton = initPnlButton();
scpButtons.setViewportView(pnlButton);
setLayout(new BorderLayout());
add(scpButtons, BorderLayout.CENTER);
// comment it and see the result
applyComponentOrientation(ComponentOrientation.RIGHT_TO_LEFT);
pack();
setDefaultCloseOperation(EXIT_ON_CLOSE);
setExtendedState(JFrame.MAXIMIZED_BOTH);
setVisible(true);
}
private JPanel initPnlButton() {
JPanel pnlButton = new JPanel(new GridBagLayout());
GridBagConstraints gbc = new GridBagConstraints(0, 0, 1, 1, 1, 1, 10,
1, new Insets(0, 0, 0, 0), 0, 0);
int ind = 0;
int row = 3;
int column = 4;
for (int i = 0; i < row; i++) {
for (int j = 1; j < column; j++) {
gbc.gridx = j;
gbc.gridy = i;
pnlButton.add(arrayButton[ind++], gbc);
}
}
gbc.weightx = 0;
gbc.gridheight = 3;
gbc.gridx = 0;
gbc.gridy = 0;
pnlButton.add(btnLeft, gbc);
gbc.gridx = 4;
gbc.gridy = 0;
pnlButton.add(btnRight, gbc);
pnlButton.setPreferredSize(new Dimension(1000, 700));
return pnlButton;
}
public static void main(String[] args) {
new MyFrame();
}
}