简体   繁体   English

如何使 JTable 在 Window 在 Eclipse 最大化时自动调整大小

[英]How to Make JTable Resize Automatically When Window Maximize in Eclipse

working on a gui app in Eclipse using windowsbuilder plugin.使用 windowsbuilder 插件在 Eclipse 中开发 gui 应用程序。 The app works just fine.该应用程序运行良好。 but the jTable won't resize when I drag the edges of the window.但是当我拖动 window 的边缘时,jTable 不会调整大小。 or when I maximize the app.或者当我最大化应用程序时。 There are other components on the window which I would like to automatically resize; window 上还有其他组件,我想自动调整大小; however, the jtable is the one that is a must for me.然而,jtable 对我来说是必须的。 when I maximize the window the table stays the same size.当我最大化 window 时,表格的大小保持不变。 I'm using Absolute Layout for the layout manager.我正在为布局管理器使用绝对布局。 so positioning the elements on the window was just a matter of drag and drop.因此在 window 上定位元素只是拖放的问题。 I have looked for the properties that allow me to set resize = true or something similar and I found autoRisizeMode.我一直在寻找允许我设置 resize = true 或类似的属性,然后我找到了 autoRisizeMode。 so I set it to true.所以我把它设置为真。 that didn't work.那没有用。 any suggestions?有什么建议么? Here's the code for my Gui initialize() method这是我的 Gui initialize() 方法的代码

private void initialize()  {        
        frame = new JFrame();
        frame.setAlwaysOnTop(true);
        frame.getContentPane().setBackground(new Color(240, 240, 240));
        frame.setBounds(100, 100, 882, 577);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.getContentPane().setLayout(null);

        JLabel lblIpRange = new JLabel("Ip Range");
        lblIpRange.setFont(new Font("Tahoma", Font.PLAIN, 14));
        lblIpRange.setBounds(10, 59, 55, 14);
        frame.getContentPane().add(lblIpRange);

        startIpLabel = new JTextField();
        startIpLabel.setBounds(75, 58, 100, 20);
        frame.getContentPane().add(startIpLabel);
        startIpLabel.setColumns(10);

        endIpLabel = new JTextField();
        endIpLabel.setBounds(198, 58, 100, 20);
        frame.getContentPane().add(endIpLabel);
        endIpLabel.setColumns(10);      
        String[] columns = {"Number", "Ip Address", "Hostname", "Mac"};             

        JButton scanButton = new JButton("Scan");
        scanButton.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {    
                try {
                    ipScan = new IpScanMain(startIpLabel.getText());
                    ipScan.startIpScanning();
                } catch (IOException e1) {
                    // TODO Auto-generated catch block
                    e1.printStackTrace();
                }
                allNodes = ipScan.getAllNodes();
                int rows = allNodes.size();
                data = new Object[rows][COLUMNS];
                int index = 0;
                for(Node node : allNodes) {
                    data[index][0] = index;
                    data[index][1] = node.getIp();
                    data[index][2] = node.getHostName();
                    data[index][3] = node.getMac();
                    index++;
                }
                table.setModel(new DefaultTableModel(data, columns));
            }


        });

        scanButton.setBounds(323, 57, 89, 23);
        frame.getContentPane().add(scanButton);

        JSeparator separator = new JSeparator();
        separator.setBounds(11, 106, 845, 2);
        frame.getContentPane().add(separator);

        JScrollPane scrollPane = new JScrollPane();
        scrollPane.setViewportBorder(new LineBorder(new Color(0, 0, 0)));
        scrollPane.setBounds(10, 119, 846, 410);
        frame.getContentPane().add(scrollPane);

        table = new JTable();
        table.setAutoResizeMode(JTable.AUTO_RESIZE_LAST_COLUMN);
        table.setFont(new Font("Tahoma", Font.PLAIN, 14));
        table.setBackground(Color.WHITE);
        table.setBorder(new LineBorder(new Color(0, 0, 0), 0));
        table.setCellSelectionEnabled(true);
        table.setFillsViewportHeight(true);
        table.setColumnSelectionAllowed(true);
        table.setModel(new DefaultTableModel(
            new Object[][] { },
            new String[] {
                "Number", "Ip Address", "Hostname", "Mac", "Ports"
            }
        ));
        table.getColumnModel().getColumn(0).setPreferredWidth(30);
        table.getColumnModel().getColumn(0).setMaxWidth(2147483619);
        table.getColumnModel().getColumn(3).setPreferredWidth(80);
        scrollPane.setColumnHeaderView(table);
        scrollPane.setViewportView(table);

        JMenuBar menuBar = new JMenuBar();
        menuBar.setBackground(new Color(240, 240, 240));
        menuBar.setBounds(10, 0, 849, 26);
        frame.getContentPane().add(menuBar);

        JMenu mnFile = new JMenu("File");
        menuBar.add(mnFile);

        JMenuItem mntmClose = new JMenuItem("Open File");
        mnFile.add(mntmClose);

        JSeparator separator_1 = new JSeparator();
        mnFile.add(separator_1);

        JMenuItem mntmSave = new JMenuItem("Save");
        mnFile.add(mntmSave);

        JMenuItem mntmSaveAs = new JMenuItem("Save As");
        mnFile.add(mntmSaveAs);

        JSeparator separator_2 = new JSeparator();
        mnFile.add(separator_2);

        JMenuItem mntmPrint = new JMenuItem("Print...");
        mnFile.add(mntmPrint);

        JSeparator separator_3 = new JSeparator();
        mnFile.add(separator_3);

        JMenuItem mntmProperties = new JMenuItem("Settings");
        mnFile.add(mntmProperties);

        JSeparator separator_4 = new JSeparator();
        mnFile.add(separator_4);

        JMenuItem mntmClose_1 = new JMenuItem("Exit");
        mnFile.add(mntmClose_1);

        JMenu mnEdit = new JMenu("Edit");
        menuBar.add(mnEdit);

        JMenuItem mntmCopy = new JMenuItem("Copy");
        mnEdit.add(mntmCopy);

        JMenuItem mntmPaste = new JMenuItem("Paste");
        mnEdit.add(mntmPaste);

        JMenu mnHelp = new JMenu("Help");
        menuBar.add(mnHelp);

        JMenuItem mntmGuide = new JMenuItem("Guide");
        mnHelp.add(mntmGuide);

        JMenuItem mntmAbout = new JMenuItem("About");
        mnHelp.add(mntmAbout);

        JPanel panel = new JPanel();
        panel.setBorder(new LineBorder(new Color(0, 0, 0)));
        panel.setBounds(574, 37, 281, 58);
        frame.getContentPane().add(panel);
        panel.setLayout(null);

        JLabel lblNewLabel = new JLabel("IPv4");
        lblNewLabel.setFont(new Font("Tahoma", Font.BOLD, 14));
        lblNewLabel.setBounds(10, 11, 46, 14);
        panel.add(lblNewLabel);

        JLabel lblNewLabel_1 = new JLabel("IPv6");
        lblNewLabel_1.setFont(new Font("Tahoma", Font.BOLD, 14));
        lblNewLabel_1.setBounds(10, 36, 46, 14);
        panel.add(lblNewLabel_1);

        JLabel ipv4Label = new JLabel("192.168.1.10/24");
        ipv4Label.setFont(new Font("Tahoma", Font.PLAIN, 14));
        ipv4Label.setBounds(53, 11, 123, 14);
        panel.add(ipv4Label);

        JLabel ipv6Label = new JLabel("ff80:ee3d:ff35:abdd:3dce:335d");
        ipv6Label.setFont(new Font("Tahoma", Font.PLAIN, 14));
        ipv6Label.setBounds(53, 36, 196, 14);
        panel.add(ipv6Label);

        JComboBox comboBox = new JComboBox();       
        comboBox.setBounds(446, 57, 100, 22);
        frame.getContentPane().add(comboBox);

        System.out.println(Thread.currentThread().getName());

        //combobox network interface stuff
        NetInterface netint = null;
        try {
            netint = new NetInterface(frame, comboBox);
        } catch (SocketException | UnknownHostException e1) {
            e1.printStackTrace();
        }
        EventQueue.invokeLater(netint);        

    }

The reason your JTable is not resized after frame gets resized, is because you getContentPane.setLayout(null) .您的JTable在调整框架大小后未调整大小的原因是因为您getContentPane.setLayout(null) You should use the proper LayoutManager and let it do the work for you.您应该使用正确的 LayoutManager并让它为您完成工作。 Setting absolute layout ( null ) will give you hard time since you have to set the coordinates and the dimensions of each component manually.设置绝对布局 ( null ) 会给您带来困难,因为您必须手动设置每个组件的坐标和尺寸。 Plus, as you are already facing, JFrame cannot be resizable or work in a screen with other resolution, which is totally unfriendly.另外,正如您已经面临的那样, JFrame无法调整大小或在其他分辨率的屏幕上工作,这完全不友好。

Another post where it explains more and why absolute positioning is bad in Swing is this one.另一篇文章解释了更多以及为什么 Swing 中的绝对定位不好

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何使框架随窗口自动调整大小? - How to make frame automatically resize with window? 调整窗口大小时如何自动调整JFreeChart的大小 - How to automatically resize JFreeChart when window is resized 如何像Windows一样调整JTable的大小? - How to make a JTable resize as the windows does? 使用Netbeans自动最大化窗口 - Automatically Maximize Window Using Netbeans 如何保持JTable自定义渲染器在窗口调整大小时保持外观? - How to keep JTable custom renderer looks on window resize? 如何在完整的jframe窗口上制作jtable - How to make jtable on full jframe window 如何在调整窗口大小时自动调整BorderPane上按钮的大小? - How to automatically resize buttons on BorderPane as the window is resized? (JavaFX)(SceneBuilder)如果将所有内容都设置为AnchorPane,则在调整窗口大小时如何自动调整场景大小? - (JavaFX)(SceneBuilder) How can I automatically resize the scene when the window is resized if everything is set into a AnchorPane? 如何做到这一点,以便Eclipse在编辑时自动在窗口中更新代码? - How can I make it so Eclipse automatically updates my code in a window as I edit it? 如何使 AnchorPane 随窗口调整大小? - How do you make AnchorPane resize with the window?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM