从右到左方向的按钮大小无关紧要 [英] Irrelevant change of button size in Right to Left orientation

查看:161
本文介绍了从右到左方向的按钮大小无关紧要的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有9个jbuttons添加到jpanel,面板添加到jscrollpane并添加到jframe。

I have 9 jbuttons added to jpanel and the panel added to jscrollpane and it added to jframe.

http://www.pic1.iran-forum.ir/images/up9/95426323683658592564.jpg

当我改变帧方向时:
applyComponentOrientation(ComponentOrientation.RIGHT_TO_LEFT);

面板向右移动,按钮大小固定,不会填充面板,但您在下图中看到滚动条填满了面板的所有宽度

the panel move to right and size of buttons got fixed and wouldn`t fill the panel but you see in the image below that scrolbar is fill all width of panel

http://www.pic1.iran-forum.ir /images/up9/60975202722295688553.jpg

(我使用gridbaglayout添加按钮,使用borderlayout.center添加滚动窗格)。

(i used gridbaglayout for adding buttons and borderlayout.center for add scrollpane).

是java中的错误还是?

is that a bug in java or ?

编辑:
这是最简单的视图。它有帮助吗?

its the simplest view . Does it help?

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();
}
}


推荐答案

玩了很多属性,并在陈述中上下移动,我找到了答案。
如果你把

after playing with lots of properties and moving up and down the statements i found the answer. if you put

scpButtons.setComponentOrientation(ComponentOrientation.LEFT_TO_RIGHT);

applyComponentOrientation(ComponentOrientation.RIGHT_TO_LEFT);

一切都会好的,不需要 setSize(getWidth()+ 1,getHeight()+ 1);

everything will be okay and no need to setSize(getWidth() + 1, getHeight() + 1);

这很有趣它从我那里得到了一天...... c#或其他langs有没有像这个?

so funny it got a whle day from me :) does c# or other langs have bugs like this?

如果让 SwingUtilities bloc运行,请调整窗口大小,一切都是LtR。

if you let SwingUtilities bloc to run , befor resizing the window everything are yet LtR.

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");

        JMenu mnuSettings = new JMenu("MENU");
        JMenuBar menubar = new JMenuBar();
        menubar.add(mnuSettings);
        setJMenuBar(menubar);

        JPanel pnlButton = initPnlButton();
        scpButtons.setViewportView(pnlButton);
        setLayout(new BorderLayout());
        setDefaultCloseOperation(EXIT_ON_CLOSE);
        add(scpButtons, BorderLayout.CENTER);

        pack();
        // [1]
        setExtendedState(JFrame.MAXIMIZED_BOTH);
        //setSize(getWidth() + 1, getHeight() + 1);
        // [2]
        setVisible(true);
        // SwingUtilities.invokeLater(new Runnable() {
        // public void run() {
        applyComponentOrientation(ComponentOrientation.RIGHT_TO_LEFT);
//here
        scpButtons.setComponentOrientation(ComponentOrientation.LEFT_TO_RIGHT);
        // }
        // });
    }

    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(btnRight, gbc);
        gbc.gridx = 4;
        gbc.gridy = 0;
        pnlButton.add(btnLeft, gbc);
        pnlButton.setPreferredSize(new Dimension(1000, 700));
        return pnlButton;
    }

    public static void main(String[] args) {
        new MyFrame();
    }

}

这篇关于从右到左方向的按钮大小无关紧要的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆