【问题标题】:JinternalFrame with jtable, inside a JFrame带有 jtable 的 JinternalFrame,在 JFrame 内
【发布时间】:2014-02-25 07:03:52
【问题描述】:

我有这个类和另一个类连接到数据库并向我显示一个数据库表这部分程序工作得很好下面解释的问题,但是没有:

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 作为 ShowEmployee 的容器,这样:

  • ManagerInterface 中我调用了 JFrame

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

  • 我将 JInternalFrame 添加到 ma​​nagerInterface 类frame,它由 ManagerInterface.getframe.add 行定义。 (frame),插入 ShowEmployee。

问题如下:

  • 如果我在 ShowEmployee 内定义一个框架(在本例中为大型机)并添加 internalframe 我会看到:

  • 但是,如果我将 JInternalFrame 添加到框架 ManagerInterface 我会看到:

也就是说,我没有看到ScrollPane所代表的表格的属性行,在frame managerInterface里面是看不到的, 我以这种方式定义滚动窗格,在 ShowEmployee 中定义。 JScrollPane 滚动 = 新 JScrollPane (table); frame.getContentPane (.) add (scroll BorderLayout.SOUTH);

【问题讨论】:

  • 无关:请学习java命名约定并遵守。
  • 所有这些代码似乎真的没有必要。尤其是当您使用我们可能必须或可能不必尝试运行示例的第三方库时。而且这个问题似乎与数据库没有任何关系。您可以使用标题的静态名称轻松创建a Minimal, Complete, Tested and Readable example。为了尽快获得更好的帮助,请尝试创建一个 MCTRE,如链接所示
  • 代码已编辑,@peeskillet
  • 您的代码似乎有一个 main 方法在任何类之外的任何地方悬空,我不确定除了过度使用和错过之外还有什么其他问题- 使用静力学。您可能想解决这个问题,只发布真实代码,即编译和运行的代码。
  • 请务必阅读有关 internalFrames 的教程章节(该教程在 swing 标签 wiki 中引用) - 它们旨在添加到除 JDesktopPane 之外的任何其他内容中

标签: java swing jtable


【解决方案1】:
  1. 正如@kleopatra 所说,遵循 java 命名约定。变量以小写开头。

  2. 当类名是ManagerInterface时,你到底为什么要命名JFrameManagerInterface

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

  4. 只需将ShowEmployee 子类化为JInternalFrame。然后只需将其添加到ManagerInterface 中的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() {
    
        }
    }
    
  5. 添加到 4。您应该将 JInternalFrame 添加到 JDesktopPanes 而不是 JFrame

    JDesktopPane desktop;
    
    public ManagerInterface() {
        showEmployees = new ShowEmployees();
        desktop = new JDesktopPane();
        desktop.add(showEmployees);
    
        frame = new JFrame("MagagerInterface");
        frame.setContentPane(desktop);
        ....
    }
    
  6. 从 EDT 运行您的 Swing 应用程序

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

    Initial Threads

  7. 以下不会导致问题,但您应该知道父级只能有一个父级容器。因此,您不应该尝试将表格添加到框架 滚动窗格中。只需添加滚动窗格

    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);

    }
}

【讨论】:

  • @HovercraftFullOfEels 刚刚修复 :)
  • 谢谢你的回答,我忘了取消showEmployee上的main方法,我曾经在我的ide中进行调试,我曾经将managerInterface命名为类名,因为对我来说更容易,记住。
  • @CiMat:还要注意不要滥用静态修饰符。您正在发布的代码中执行此操作。
  • 为什么? @HovercraftFullOfEels
  • @CiMat:为什么要避免滥用静态修饰符?因为这会阻止您封装状态和行为,因此会严重阻碍您增强和修改代码的能力。通过引入“全局”变量,即使无法很好地测试代码,也会变得很困难。
猜你喜欢
  • 1970-01-01
  • 2013-02-22
  • 2022-01-23
  • 2017-08-06
  • 1970-01-01
  • 2012-10-29
  • 2016-02-20
  • 2016-06-01
  • 2012-05-09
相关资源
最近更新 更多