无法更改JTable的位置和大小

时间:2016-08-16 16:45:15

标签: java swing jtable border layout-manager

我在Java中创建JTable

这是我的代码。我已将表放在面板上(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

1 个答案:

答案 0 :(得分:2)

表组件未调整大小的原因是您使用setLayout(null)设置了所谓的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两次。

以下是截图:

Screenshot from the example