如何调整具有不同分辨率的应用程序的所有控件的大小



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

下面是我在编辑 Answer by @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();
}
我想使用组件尺寸和

屏幕尺寸来确定纵横比和缩放组件,使用宽高比配比,例如! 使用以下信息。如果有任何机构指导如何完成任务,我将不胜感激。

       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. Container上使用布局管理器
  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();
                }
            }
        });
    }
}

最新更新