【问题标题】:How to ReSize All Controls Of Application With Different Resolutions如何使用不同的分辨率调整应用程序的所有控件的大小
【发布时间】:2012-11-21 04:54:02
【问题描述】:

我正在使用 Swing 应用程序。我在分辨率(1280 * 1024)中构建了许多表格。当我以分辨率部署应用程序时,表单会以分辨率 (1024*768) 和其他较小的分辨率进行裁剪。我试过MIGLayOutManager!和GridBagLayout!因为如果您从头开始开发应用程序,这些是更可取的,但它们对我不起作用。我想使用 Example

下面是我在@Guillaume Polet 编辑Answer时用于GridBagLayOut的代码

        try {
    Utility util = new Utility(null);
    Component[] com = null;
    java.util.List<Component> jComp = new ArrayList<Component>();
    if (obj instanceof JPanel) {
        JPanel panel = (JPanel) obj;
        com = panel.getComponents();
    } else if (obj instanceof JFrame) {
        JFrame JFrm = (JFrame) obj;

        jComp = util.harvestAllComp(JFrm);

    } else if (obj instanceof JFrame) {

        JFrame frm = (JFrame) obj;
        //  frm.cp = cp;
        String dbName = "GoldNew";
        util = new Utility(dbName);
        DBEngine db = new DBEngine(dbName);

        for (int a = 0; a < jComp.size(); a++) {
            if (jComp.get(a) instanceof JLabel) {

                JLabel label = (JLabel) com[a];
            } else if (jComp.get(a) instanceof JTextField) {
                JTextField jtxt = (JTextField) jComp.get(a);
                jtxt.setEditable(boolEnable);

                // Do Nothing 
            } else if (jComp.get(a) instanceof JTextArea) {
                JTextArea jtxt = (JTextArea) jComp.get(a);
                jtxt.setEditable(boolEnable);

            } else if (jComp.get(a) instanceof JXDatePicker) {
                JXDatePicker jdate = (JXDatePicker) jComp.get(a);
                jdate.setEditable(boolEnable);
                jdate.getEditor().setEditable(false);

                // Do Nothing 
            } else if (jComp.get(a) instanceof JTabbedPane) {
                JTabbedPane tabPane = (JTabbedPane) jComp.get(a);
                Component[] comTab = tabPane.getComponents();
                tabPane.removeAll();
                GridBagLayout tabLayOut = new GridBagLayout();
                tabPane.setLayout(tabLayOut);
                addRowOfComponents(tabPane, comTab);

            } 
            if (jComp.get(a) instanceof JPanel) {
                JPanel panel2 = (JPanel) jComp.get(a);
                Component[] comTab = panel2.getComponents();
                panel2.removeAll();
                GridBagLayout tabLayOut = new GridBagLayout();
                panel2.setLayout(tabLayOut);
                addRowOfComponents(panel2, comTab);

            } else if (jComp.get(a) instanceof JTable) {

                final JTable tbl = (JTable) jComp.get(a);
                tbl.getTableHeader().setReorderingAllowed(false);
            } else if ((!(jComp.get(a) instanceof JTextField)) && (!(jComp.get(a) instanceof JXDatePicker))
                    && (!(jComp.get(a) instanceof JTextArea))
                    && (!(jComp.get(a) instanceof JLabel))) {
                // jComp.get(a).setEnabled(boolEnable);
                jComp.get(a).setEnabled(boolEnable);

            }
        }
    }
} catch (Exception ex) {
    ex.printStackTrace();
}

我想使用组件大小和屏幕大小来确定纵横比和使用纵横比缩放组件,如Example!使用以下信息。如果有人指导如何完成任务,我将非常感激。

       Dimension dim = Toolkit.getDefaultToolkit().getScreenSize();
        frm.setBounds(0, 0, dim.width - 100, dim.height - 100);
        int w = frm.getSize().width;//Size Of Form
        int h = frm.getSize().height;
        int x = (dim.width - w) / 2;
        int y = (dim.height - h) / 2;

【问题讨论】:

  • 就个人而言,如果您要设置屏幕尺寸限制,我可能会在顶层容器中添加一个滚动窗格,以便它处理那些低于您预期的情况。如果您真的想“调整”图形大小,我会查看 JLayer (Java 7) 或 JXLayer
  • @MadProgrammer 你能给我一些关于如何在我的例子中使用 JXLayer 或 JLayer 的提示吗?
  • @MadProgrammer 我已经测试过将 scrollPane 添加到 jframe ,它会在 compileTime 上引发错误。当我将 ScrollPane 添加到顶级容器(即 JTabPane 和 JPanel)时,它没有满足要求,并且表单被剪裁在底部。
  • "你能给我一些关于如何使用 JXLayer 或 JLayer 的提示吗" 我希望我可以,网上曾经有一些很好的例子,但他们已经消失了。我可能有一些工作中的例子
  • 滚动窗格出现什么错误?

标签: java swing layout deployment screen-resolution


【解决方案1】:

我认为您错误地使用了 LayoutManagers。不是将它们设置在容器上,而是将它们设置在其内容上(虽然这是允许的,但并不经常这样做)。

  1. Container 上使用 LayoutManager
  2. 可以选择为其子组件设置约束。

这是一个使用GridBagLayout 的完全愚蠢的示例,它根据框架的大小增加或减少所有组件的大小:

import java.awt.Container;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.net.MalformedURLException;

import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JComponent;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JList;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTabbedPane;
import javax.swing.JTable;
import javax.swing.JTextArea;
import javax.swing.JTextField;
import javax.swing.SwingUtilities;

public class TestGridBagLayout {

    protected void initUI() throws MalformedURLException {
        final JFrame frame = new JFrame();
        frame.setTitle(TestGridBagLayout.class.getSimpleName());
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        JPanel mainPanel = new JPanel(new GridBagLayout());
        JTable table = new JTable(new Object[][] { new Object[] { "Cell 1" }, new Object[] { "Cell 2" }, new Object[] { "Cell 3" } },
                new Object[] { "Header 1" });
        JList list = new JList(new Object[] { "Element 1", "Element 2", "Element 3", "Element 4" });
        JTextArea textArea = new JTextArea(8, 30);
        JTabbedPane tabbedPane = new JTabbedPane();
        tabbedPane.addTab("Tab 1", new JLabel("Tab 1"));
        tabbedPane.addTab("Tab 2", new JLabel("Tab 2"));
        tabbedPane.addTab("Tab 3", new JLabel("Tab 3"));
        addRowOfComponents(mainPanel, new JLabel("A label"), new JButton("A button"), new JTextField("A textfield", 24), new JComboBox(
                new Object[] { "Item 1", "Item 2", "Item 3", "Item 4", "Item 5" }));
        addRowOfComponents(mainPanel, new JScrollPane(table), new JScrollPane(list), new JScrollPane(textArea), tabbedPane);
        frame.add(mainPanel);
        frame.setSize(800, 500);
        frame.setVisible(true);
    }

    private void addRowOfComponents(Container parent, JComponent... children) {
        GridBagConstraints gbc = new GridBagConstraints();
        gbc.weightx = 1.0;
        gbc.weighty = 1.0;
        gbc.fill = GridBagConstraints.BOTH;
        gbc.insets = new Insets(3, 3, 3, 3);
        for (int i = 0; i < children.length; i++) {
            JComponent jComponent = children[i];
            if (i + 1 == children.length) {
                gbc.gridwidth = GridBagConstraints.REMAINDER;
            }
            parent.add(jComponent, gbc);
        }
        parent.revalidate();
    }

    public static void main(String[] args) {
        SwingUtilities.invokeLater(new Runnable() {
            @Override
            public void run() {
                try {
                    new TestGridBagLayout().initUI();
                } catch (MalformedURLException e) {
                    e.printStackTrace();
                }
            }
        });
    }

}

【讨论】:

  • 我将您的代码应用到我的示例中,并为表单内的每个容器使用了 newRowOf 组件,但它正在工作并且表单仍在从底部剪裁。分辨率 (1024*768) ;
  • Guillaume Polet 我已经通过应用您的答案编辑了我的问题,但它对我不起作用。
  • @Prog_Anila 发布SSCCE,因为我们看到的当前代码无法为您提供更多帮助。
  • @downvoter 你愿意解释一下吗?还是只是纯粹的随机投票?
  • 我已经向您发送了我的调整大小函数的代码(我用于所有帧的通用函数),它应该是一个 SSCCE。 @Guillaume Polet
猜你喜欢
  • 2010-10-12
  • 1970-01-01
  • 2011-12-27
  • 1970-01-01
  • 2023-03-03
  • 2011-06-12
  • 1970-01-01
  • 2014-02-28
  • 1970-01-01
相关资源
最近更新 更多