java对象在窗口大小调整大小

时间:2015-07-22 01:19:42

标签: java swing resize window

我要在窗口调整大小时调整像JTable这样的少量元素。我一直在尝试这段代码,但它无法正常工作:

    table.setLocation(0, 23);
    Dimension siz = contentPane.getMaximumSize();
    table.setSize(siz.height, siz.width - 46);

它调整了我的桌子大小,但它让它变得无穷无尽,我不想要它。另外,我想将滚动条连接到此表,如果可能的话 - 在precentage中设置列宽

1 个答案:

答案 0 :(得分:1)

您的主要问题(调整大小)更多地与您依赖表单编辑器有关,而与Swing或Java有关

有关详细信息,请查看Laying Out Components Within a Container

您似乎也没有使用JScrollPane来容纳JTable。请查看How to Use TablesHow to Use Scroll Panes了解更多详情

Sizing

import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.EventQueue;
import java.awt.FlowLayout;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.GridLayout;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;
import javax.swing.table.DefaultTableModel;

public class ResizeTest {

    public static void main(String[] args) {
        new ResizeTest();
    }

    public ResizeTest() {
        EventQueue.invokeLater(new Runnable() {
            @Override
            public void run() {
                try {
                    UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
                } catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
                    ex.printStackTrace();
                }

                JFrame frame = new JFrame("Testing");
                frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                frame.add(new TestPane());
                frame.pack();
                frame.setLocationRelativeTo(null);
                frame.setVisible(true);
            }
        });
    }

    public class TestPane extends JPanel {

        private JTable table;
        private JButton historyButton;
        private JButton otherButton;

        public TestPane() {

            table = new JTable(new DefaultTableModel(10, 10));
            historyButton = new JButton("History");
            otherButton = new JButton("Other");

            setLayout(new BorderLayout());
            JPanel buttons = new JPanel(new FlowLayout(FlowLayout.LEFT));
            buttons.add(historyButton);
            buttons.add(otherButton);
            add(buttons, BorderLayout.NORTH);

            add(new JScrollPane(table));

            JPanel footers = new JPanel(new GridLayout(1, 2));
            JLabel left = new JLabel("Left");
            left.setHorizontalAlignment(JLabel.LEFT);
            JLabel right = new JLabel("Right");
            right.setHorizontalAlignment(JLabel.LEFT);
            footers.add(left);
            footers.add(right);

            add(footers, BorderLayout.SOUTH);
        }

        @Override
        public Dimension getPreferredSize() {
            return new Dimension(200, 200);
        }

    }

}