无法更改 JTable 的位置和大小

Unable to change location and size of JTable

我正在 Java 制作 JTable

这是我的代码。我已经把 table 放在面板上 (jtjp1),jtjp1 放在 adminjp1 上。 adminjp1 放在 adminjf1

代码

 package Hotel_room_reservation_system;

import java.io.*;
import java.lang.*;
import java.awt.*;
import java.awt.Event.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.*;
import java.util.*;
import javax.swing.table.*;

public class Hotel_room_reservation_system extends JFrame implements ActionListener {

public JFrame adminjf1;
public JTable jt1;
public JPanel adminjp1;
public JPanel jtjp1; 

public Hotel_room_reservation_system() {
    guimake();
}

public void guimake() {
   adminjf1 = new JFrame("ADMIN");

   adminjp1 = new JPanel();
   jtjp1 =new JPanel(new BorderLayout());
   jt1=new JTable();
   String[] columns = {"Name", "Age", "Gender"};

   String[][] data = {{"John", "18", "Male"},
                     {"Daisy", "19", "Female"},
                     {"Dave", "23", "Male"},
                     {"Jake", "30", "Male"}};

   jt1 = new JTable(data, columns)                 
   {
        public boolean isCellEditable(int data, int columns)
        {
           return false;
        }


        public Component prepareRenderer(TableCellRenderer r, int data, int columns)
        {
               Component c = super.prepareRenderer(r, data, columns);

               if ((data % 2 == 0))
                   c.setBackground(Color.WHITE);

               else
                    c.setBackground(Color.LIGHT_GRAY);

               return c;
        }
    };



    JScrollPane jps = new JScrollPane(jt1);
    //   jt1.setFillsViewportHeight(true);
    jt1.setFillsViewportHeight(true);
    jtjp1.setLocation(10,10);

    adminjf1.setSize(900, 900);
    adminjf1.setLayout(null);
    adminjp1.setBackground(Color.WHITE);
    adminjp1.setSize(800, 800);
    jtjp1.setSize(550,180);
    jtjp1.setBackground(Color.red);


    adminjp1.setLocation(20, 25); 
    jtjp1.add(jps);
    adminjp1.add(jtjp1);
    adminjf1.add(adminjp1);
    adminjf1.setVisible(true);
}

public static void main(String[] args) {

    SwingUtilities.invokeLater(new Runnable() {
        @Override
        public void run() {
            new Hotel_room_reservation_system();
        }
    });
}

@Override
public void actionPerformed(ActionEvent e) {
    throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
  }
 }

问题

  1. 我无法移动名为 jtjp1 的面板的大小。
  2. 我无法将 jtjp1 的位置更改为向左或向下。我已将 jtjp1 放在 adminjp1.

table 组件未调整大小的原因是您使用 setLayout(null) 设置了所谓的空布局管理器。设置这意味着您应该注意在 GUI 中定位和调整组件大小。 这就是布局管理器所做的。

一旦你使用布局管理器,例如 GroupLayout,这个例子就像 预期。

package com.zetcode;

import java.awt.Color;
import java.awt.Component;
import java.awt.Container;
import javax.swing.GroupLayout;
import javax.swing.JComponent;
import javax.swing.JFrame;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.SwingUtilities;
import javax.swing.table.TableCellRenderer;

public class HotelReservationSystemEx extends JFrame {

    private JTable table;

    public HotelReservationSystemEx() {

        initUI();
    }

    private void initUI() {

        String[] columns = {"Name", "Age", "Gender"};

        String[][] data = {{"John", "18", "Male"},
        {"Daisy", "19", "Female"},
        {"Dave", "23", "Male"},
        {"Jake", "30", "Male"}};

        table = new JTable(data, columns) {
            @Override
            public boolean isCellEditable(int data, int columns) {
                return false;
            }

            @Override
            public Component prepareRenderer(TableCellRenderer r, int data, int columns) {
                Component c = super.prepareRenderer(r, data, columns);

                if ((data % 2 == 0)) {
                    c.setBackground(Color.WHITE);
                } else {
                    c.setBackground(Color.LIGHT_GRAY);
                }

                return c;
            }
        };

        //table.setFillsViewportHeight(true);

        JScrollPane spane = new JScrollPane(table);

        createLayout(spane);

        setTitle("Hotel reservation system");
        setLocationRelativeTo(null);
        setDefaultCloseOperation(EXIT_ON_CLOSE);        
    }

    private void createLayout(JComponent... arg) {

        Container pane = getContentPane();
        GroupLayout gl = new GroupLayout(pane);
        pane.setLayout(gl);

        gl.setAutoCreateContainerGaps(true);

        gl.setHorizontalGroup(gl.createParallelGroup()
                .addComponent(arg[0])
        );

        gl.setVerticalGroup(gl.createSequentialGroup()
                .addComponent(arg[0])
        );

        pack();
    }

    public static void main(String[] args) {

        SwingUtilities.invokeLater(() -> {
            HotelReservationSystemEx ex = new HotelReservationSystemEx();
            ex.setVisible(true);
        });
    }
}

此外,您无需创建 JPanel 并实例化 JTable 两次。

截图如下: