如何重新确定不同分辨率应用的所有控制

时间:2012-11-21 04:54:03

标签: java swing layout deployment screen-resolution

我正在使用Swing应用程序。我在Resolution(1280 * 1024)中构建了许多表单。当我在分辨率中部署应用程序时,表单以分辨率(1024 * 768)和其他较小的分辨率进行裁剪。我试过了MIGLayOutManager!和GridBagLayout!因为如果你从头开始开发这个应用程序,但是它们对我不起作用,这些更好。我想使用。来调整框架的子控件的大小 Example

以下是我在@Guillaume Polet编辑答案时用于 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();
}

我想使用组件大小和屏幕大小来确定宽高比和缩放组件,使用Aspect 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;

1 个答案:

答案 0 :(得分:1)

我认为你错误地使用了LayoutManagers。您可以在内容上设置它们,而不是在容器上设置它们(虽然这是允许的,但这种做法并不常见)。

  1. Container
  2. 上使用LayoutManager
  3. 可选择为其子组件设置约束。
  4. 这是一个使用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();
                    }
                }
            });
        }
    
    }