Java图形用户界面调整大小自动(Java GUI Automatically Resizing)

2019-09-19 19:44发布

这里就是GUI绘制(注意,该类扩展JFrame的)。

public Cache() {
    SubstanceColorChooserUI col = new SubstanceColorChooserUI();
    while (mode == 0);
    setResizable(false);
    setTitle("Cache");
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    setBounds(100, 100, 483, 374);
    setLocationRelativeTo(null);
    contentPane = new JPanel();
    contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
    setContentPane(contentPane);
    contentPane.setLayout(new BorderLayout());

    textField = new JTextField();
    textField.setBounds(10, 11, 328, 20);
    contentPane.add(textField);
    textField.setColumns(10);

    JButton btnLoadCache = new JButton("Load cache");
    btnLoadCache.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent arg0) {
            try {
                String loc = textField.getText();
                if (loc.equals("")) {
                    JOptionPane.showMessageDialog(Cache.this, "Please specify a location for the cache.", "Unable to load", JOptionPane.ERROR_MESSAGE);
                    return;
                }
                if (!loc.endsWith("\\"))
                    loc = loc + "\\";
                cache = new Store(loc);
                loadImages();
            } catch (Exception e) {
                JOptionPane.showMessageDialog(Cache.this, "Cache failed to initialize.\n" + e.getMessage(), "Unable to load", JOptionPane.ERROR_MESSAGE);
            }
        }
    });
    btnLoadCache.setBounds(351, 9, 112, 23);
    contentPane.add(btnLoadCache);

    JSplitPane splitPane = new JSplitPane();
    splitPane.setDividerLocation(150);
    splitPane.setContinuousLayout(true);
    splitPane.setBounds(10, 44, 453, 279);
    contentPane.add(splitPane);

    JPanel panellie = new JPanel();
    panellie.setLayout(new BorderLayout(0, 0));
    panel = new ImagePanel(null);

    scrollPane_1 = new JScrollPane();
    panellie.add(scrollPane_1, "Center");

    scrollPane_1.setViewportView(panel);
    splitPane.setRightComponent(panellie);

    JPanel panel_1 = new JPanel();
    splitPane.setLeftComponent(panel_1);
    panel_1.setLayout(new BorderLayout(0, 0));

    JScrollPane scrollPane = new JScrollPane();
    panel_1.add(scrollPane, BorderLayout.CENTER);

    model = new DefaultListModel<ListedImage>();
    list = new JList<ListedImage>(model);
    list.addListSelectionListener(new ListSelectionListener() {
        public void valueChanged(ListSelectionEvent arg0) {
            if (arg0.getValueIsAdjusting())
                return;
            ListedImage img = list.getModel().getElementAt(list.getSelectedIndex());
            panel.setImage(img.getImage());
        }
    });
    scrollPane.setViewportView(list);

    progressBar = new JProgressBar();
    progressBar.setStringPainted(true);
    progressBar.setDoubleBuffered(true);
    progressBar.setBounds(10, 328, 453, 14);
    contentPane.add(progressBar);

}

我将如何使它所以当我setResizeable为真,我开始拖动程序到更大和更小的,我怎么让这个里面的框架(按钮,标签等)的组件时,整个框架是调整大小调整

Answer 1:

使用布局管理器,而不是设置范围为每个组件。

它会从程序而异编程如何您希望您的组件移动。

看看这个 ,并尝试看看哪些布局将工作最适合你。



Answer 2:

1.您可以使用setSize()方法来设置你的JFrame的大小

2.您可以选择让JFrame的通过设置固定大小 isResizable(false)

3.使用的GroupLayout,通过NetBeans团队在2005年开发的请尝试使用Windows生成器专业版由谷歌免费提供了。 在这里,您可以选择的GroupLayout,然后将其拖放到JFrame中删除您的成分,即使你愿意,你可以手动编辑GUI代码。



文章来源: Java GUI Automatically Resizing