提问者:小点点

JinternalFrame with jtable, in a JFrame


我有这个类,另一个连接到数据库并向我展示数据库的表,这部分程序运行良好,下面解释的问题,但没有:

import java.awt.Color;
import java.awt.event.*;
import java.sql.SQLException;
import javax.swing.*;
public class ManagerInterface {
public static JFrame ManagerInterface = new JFrame("Manager Interface");

public ManagerInterface() {
    StartInterfaceGUI();
}

public static JFrame getframe() {
    return ManagerInterface;
}
private void StartInterfaceGUI() {



    ManagerInterface.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    ManagerInterface.setSize(1600, 900);
    new ShowEmployee();
    ManagerInterface.setVisible(true);
}
}
public static void main(String []args)
{
   new ManagerInterface();
}

这个类:

import java.awt.BorderLayout;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.sql.*;
import java.util.*;

import javax.swing.*;
import GUIManager.ManagerInterface;

public class ShowEmployee {

public static JInternalFrame frame = new JInternalFrame();
public JTable table = new JTable();
public JFrame mainframe = new JFrame();

public ShowEmployee() {

    frame.add(table);
    JScrollPane scroll = new JScrollPane(table);
    frame.getContentPane().add(scroll, BorderLayout.SOUTH);
    frame.setTitle("Employees");
    frame.setResizable(true);
    frame.setClosable(true);
    frame.setMaximizable(true);
    frame.setIconifiable(true);
    frame.setSize(650, 400);
    frame.pack();
    frame.setVisible(true);

           /* mainframe.add(frame);
    mainframe.setSize(650, 400);    //adding frame inside mainframe defined in this class
    mainframe.pack();
    mainframe.setVisible(true);*/


    //ManagerInterface.getframe().add(frame); //adding the internalframe to manager interface frame


}
 }

我使用ManagerInterface作为Show员工的容器,通过这种方式:

>

  • 在管理器界面中,我调用 JFrame

    ShowEmployee类由一个JInternalFrame表示,在其上添加一个JTable。

    问题如下:

    换句话说,我没有看到 ScrollPane 表示的表的属性行,它在帧管理器界面内部是不可见的,我用这种方式定义滚动窗格,在 ShowEmployee 中定义。JScrollPane scroll = new JScrollPane (table);frame.getContentPane (.) add (scroll BorderLayout.SOUTH);


  • 共1个答案

    匿名用户

    >

  • 如@kleopatra所述,遵循 java 命名约定。 变量以小写开头。

    当类名是 ManagerInterface 时,为什么要命名 JFrame ManagerInterface

    为什么您有两个main方法?您只需要在启动类ManagerInterface中使用它。

    只需将 ShowEmployee 子类设为 JInternalFrame。然后只需将其添加到管理器界面中的JFrame(您将要命名其他内容)中即可

    public class ManagerInterface {
        private Frame frame;
        private ShowEmployees showEmployee;
    
        public ManagerInterface() {
            showEmployees = new ShowEmployees();
    
            frame = new JFrame("MagagerInterface");
            frame.add(new ShowEmployees());
            frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            frame.pack();
            frame.setLocationRelativTo(null);
            frame.setVisible(true);
        }
    }
    
    public class ShowEmployees extends JInternalFrame {
        public ShowEmployees() {
    
        }
    }
    

    增加到4。您应该将< code>JInternalFrame添加到< code>JDesktopPanes,而不是< code>JFrame

    JDesktopPane desktop;
    
    public ManagerInterface() {
        showEmployees = new ShowEmployees();
        desktop = new JDesktopPane();
        desktop.add(showEmployees);
    
        frame = new JFrame("MagagerInterface");
        frame.setContentPane(desktop);
        ....
    }
    

    从EDT运行Swing应用程序

    public static void main(String[] args) {
        SwingUtilities.invokeLater(new Runnable(){
            public void run() {
                new ManagerInterface();
            }
        });
    }
    

    请参阅初始线程

    下面的不会引起问题,但是你应该知道一个父容器只能有一个父容器。所以你试图添加表格到框架和滚动窗格不应该这样做。只需添加滚动窗格

    frame.add(table);   <<---------------------Get Rid of MEEEE!
    JScrollPane scroll = new JScrollPane(table);
    frame.getContentPane().add(scroll, BorderLayout.SOUTH);
    

    这是一个运行示例,其中包含上述所有修复程序。

    import javax.swing.*;
    
    public class ManagerInterface {
    
        public JFrame frame = new JFrame("Manager Interface");
    
        private ShowEmployee showEmployee;
        private JDesktopPane desktop;
    
        public ManagerInterface() {
            showEmployee = new ShowEmployee();
            desktop = new JDesktopPane();
            desktop.add(showEmployee);
    
            frame = new JFrame("MagagerInterface");
            frame.setContentPane(desktop);
            frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            frame.setSize(600, 600);
            frame.setLocationRelativeTo(null);
            frame.setVisible(true);
        }
    
        public static void main(String[] args) {
            SwingUtilities.invokeLater(new Runnable() {
                public void run() {
                    new ManagerInterface();
                }
            });
        }
    }
    
    class ShowEmployee extends JInternalFrame {
    
        String[][] data = {{"Hello", "Hello", "Hello"},
        {"Hello", "Hello", "Hello"}};
        String[] cols = {"Col 1", "Col 2", "Col 3"};
    
        public JTable table = new JTable(data, cols);
    
        public ShowEmployee() {
    
            JScrollPane scroll = new JScrollPane(table);
            getContentPane().add(scroll);
            setTitle("Employees");
            setResizable(true);
            setClosable(true);
            setMaximizable(true);
            setIconifiable(true);
            pack();
            setVisible(true);
    
        }
    }