我有一个包含四个组件的JFrame:三个JPanel和一个JTabbedPane。两个面板具有可选组件,大约位于BorderLayout.NORTH和EAST中,而JTabbedPane大约位于CENTER中。 TabbedPane的选项卡在窗格底部对齐。我说大概是因为实际的布局是用GridBagLayout完成的(对不起!)。
目前,焦点遍历周期如下:
Current Cycle:
1) North JPanel
2) East JPanel
3) JTabbedPane selected tab
4) JTabbedPane selected tab content
但是,我希望它是:
Desired Cycle:
1) North JPanel
2) JTabbedPane selected tab content
3) JTabbedPane selected tab
4) East JPanel
我已经尝试了Java Focus Guide给出的基于矢量的FocusTraversalPolicy而没有运气:我无法让光标进入选项卡面板内容。
然后我考虑扩展/更改其他的FocusTraversalPolicy类,但是也无处可去。
我很确定问题是当前的FocusTraversalPolicy使用屏幕上的组件位置来确定布局周期。从技术上讲,侧面板在屏幕上比中央选项卡式窗格更高。我真的很想改变它。任何其他人遇到这个/有任何见解?谢谢!
编辑:
这是一个SSCCE,其中包含适当的布局代码,以说明问题所在。请注意:问题不在于布局,因为必须保持不变。
package SO;
import java.awt.Color;
import java.awt.FlowLayout;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.GridLayout;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTabbedPane;
import javax.swing.JTextField;
public class ssccee7729709{
JPanel north;
JPanel north2;
JPanel east;
JTabbedPane center;
JFrame content;
void initialize() {
north = new JPanel(new FlowLayout()) {
{
this.setBackground(Color.CYAN);
}
};
north.add(new JLabel("Title panel"));
north2 = new JPanel(new FlowLayout()) {
{
this.setBackground(Color.RED);
}
};
north2.add(new JLabel("north2 Panel"));
north2.add(new JTextField(4));
north2.add(new JTextField(4));
east = new JPanel(new GridLayout(3,1)) {
{
this.setBackground(Color.BLUE);
}
};
east.add(new JButton("b1"));
east.add(new JButton("b2"));
center = new JTabbedPane(JTabbedPane.BOTTOM, JTabbedPane.WRAP_TAB_LAYOUT);
for (int i = 0; i < 2; i++) {
JPanel panel = new JPanel(new GridLayout(4,1));
panel.add(new JLabel("Panel " + i));
panel.add(new JTextField(6));
panel.add(new JTextField(6));
panel.add(new JTextField(6));
center.addTab("Tab " + i, panel);
}
content = new JFrame();
content.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
content.setLayout(new GridBagLayout());
GridBagConstraints gbc = new GridBagConstraints();
{
gbc.fill = GridBagConstraints.BOTH;
gbc.gridwidth = 4;
gbc.weightx = 1;
content.add(north, gbc);
}
{
gbc = new GridBagConstraints();
gbc.fill = GridBagConstraints.BOTH;
gbc.anchor = GridBagConstraints.PAGE_START;
gbc.gridwidth = 1;
gbc.weightx = 1;
// gbc.weighty = .1;
gbc.gridy = 1;
gbc.gridx = 1;
content.add(north2, gbc);
}
{
gbc = new GridBagConstraints();
gbc.fill = GridBagConstraints.BOTH;
gbc.gridwidth = GridBagConstraints.RELATIVE;
gbc.gridheight = GridBagConstraints.RELATIVE;
gbc.weighty = 1;
gbc.gridy = 2;
content.add(center, gbc);
}
{
gbc = new GridBagConstraints();
gbc.fill = GridBagConstraints.VERTICAL;
gbc.anchor = GridBagConstraints.SOUTHEAST;
gbc.gridwidth = GridBagConstraints.REMAINDER;
gbc.gridx = 3;
gbc.gridheight = 4;
content.add(east, gbc);
}
content.setVisible(true);
content.pack();
}
public static void main(String args[]) {
ssccee7729709 so = new ssccee7729709();
so.initialize();
}
}
答案 0 :(得分:3)
如果您查看LayoutFocusTraversalPolicy的来源,这是默认策略,它几乎只是扩展SortingFocusTraversalPolicy,需要Comparator<? extends Component>
。
您应该能够提供自己的比较器,它是一个内部类,因此可以访问父组件,并通过查看自己和父母提供适当的比较值。
查看来自LayoutComparator的来源可能不会受到影响,这是LayoutFocusTraveralPolicy使用的。
答案 1 :(得分:2)
克隆LayoutComparator后,我不得不在compare
方法中添加以下行:
...previous code...
int zOrder = a.getParent().getComponentZOrder(a) - b.getParent().getComponentZOrder(b);
/// added these:
if (a instanceof EastPanel) {
if (b instanceof JTabbedPane || b instanceof NorthPanel) {
return -1;
}
}
if (b instanceof EastPanel) {
if (a instanceof JTabbedPane || a instanceof NorthPanel) {
return 1;
}
}
if (horizontal) {
if (leftToRight) {
...more code...
老实说我不知道它是如何工作的......使用compare
命令组件的机制对我来说很奇怪......但这很有用,所以......很有魔力!