0

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

下面是我在@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();
}

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

       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;
4

1 回答 1

1

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

  1. 在一个 LayoutManager 上使用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();
                }
            }
        });
    }

}
于 2012-11-21T08:10:43.057 回答