Java JTable 和 JToolBar 奇怪地调整大小

发布于 2024-12-21 23:40:17 字数 2607 浏览 2 评论 0原文

今天我注意到我的程序有非常奇怪的行为。基本上我有 JToolBar uder,它是带有 JTable 的 JScrollPane。两者都在 JFrame 内的 JPanel 内。每个容器都使用 MigLayout。

现在,如果我启动应用程序,这是它的默认外观: 在此处输入图像描述

但是,如果我移动 JToolBar 并将其剪辑回到原来的位置,现在看起来像这样:在此处输入图像描述

突然没有边框了。如果一开始就没有的话,我会更喜欢,但是改变 GUI 的外观根本不是一个好功能...如果您知道出了什么问题,请帮助:)

代码:

public class Gui extends JFrame {

private static final long serialVersionUID = 1L;
private JPanel mainPnl = null;
private JToolBar toolbar = null;
private Session session = null;

public Gui(Session session) {
    try {
        UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
    } catch (UnsupportedLookAndFeelException e) {
    } catch (ClassNotFoundException e) {
    } catch (InstantiationException e) {
    } catch (IllegalAccessException e) {
    }

    this.session = session;

    setTitle("PRO2-Contact Manager v_0.1");
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    setSize(800,420);
    setResizable(true);

    initMenu();
    initMainPnl();
    initToolbar();
    initTable();

    // KeyboardFocusManager manager =
    // KeyboardFocusManager.getCurrentKeyboardFocusManager();
    // manager.addKeyEventDispatcher(new MyDispatcher(aList));

    setLocationRelativeTo(null);
    setVisible(true);
}

private void initMenu() {
    JMenuBar menu = new JMenuBar();
    MenuListener ml = new MenuListener();

    JMenu file = new JMenu("File");
    file.setMnemonic(KeyEvent.VK_F);
    menu.add(file);

    JMenuItem exit = new JMenuItem("Exit");
    exit.setMnemonic(KeyEvent.VK_E);
    exit.addActionListener(ml);
    file.add(exit);

    JMenu help = new JMenu("Help");
    help.setMnemonic(KeyEvent.VK_H);
    menu.add(help);

    JMenuItem controls = new JMenuItem("Controls");
    controls.setMnemonic(KeyEvent.VK_C);
    controls.addActionListener(ml);
    help.add(controls);

    JMenuItem about = new JMenuItem("About");
    about.setMnemonic(KeyEvent.VK_A);
    about.addActionListener(ml);
    help.add(about);

    setJMenuBar(menu);
}

private void initMainPnl(){
    mainPnl = new JPanel(new MigLayout());

    add(mainPnl);
}

private void initToolbar() {
    toolbar = new JToolBar(JToolBar.HORIZONTAL);
    toolbar.add(new JButton());
    mainPnl.add(toolbar,"wrap");
}

private void initTable() {
    MyTable table = new MyTable(new MyTableModel(this));
    JScrollPane sp = new JScrollPane(table);
    sp.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED);

    mainPnl.add(sp,"w 100%, h 100%");
}}

I noticed very strange behavior of my program today. Basically I have JToolBar uder which is JScrollPane with JTable. Both inside JPanel inside JFrame. Every container using MigLayout.

Now, if I start app, this is its default look: enter image description here

But, if I move JToolBar and clip it back to its original position, now it looks like this:enter image description here

Suddenly there are no borders. I would preffer if there werent any at all at first place, but changing look of GUI is not good feature at all... Please if you know what is wrong, help :)

CODE:

public class Gui extends JFrame {

private static final long serialVersionUID = 1L;
private JPanel mainPnl = null;
private JToolBar toolbar = null;
private Session session = null;

public Gui(Session session) {
    try {
        UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
    } catch (UnsupportedLookAndFeelException e) {
    } catch (ClassNotFoundException e) {
    } catch (InstantiationException e) {
    } catch (IllegalAccessException e) {
    }

    this.session = session;

    setTitle("PRO2-Contact Manager v_0.1");
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    setSize(800,420);
    setResizable(true);

    initMenu();
    initMainPnl();
    initToolbar();
    initTable();

    // KeyboardFocusManager manager =
    // KeyboardFocusManager.getCurrentKeyboardFocusManager();
    // manager.addKeyEventDispatcher(new MyDispatcher(aList));

    setLocationRelativeTo(null);
    setVisible(true);
}

private void initMenu() {
    JMenuBar menu = new JMenuBar();
    MenuListener ml = new MenuListener();

    JMenu file = new JMenu("File");
    file.setMnemonic(KeyEvent.VK_F);
    menu.add(file);

    JMenuItem exit = new JMenuItem("Exit");
    exit.setMnemonic(KeyEvent.VK_E);
    exit.addActionListener(ml);
    file.add(exit);

    JMenu help = new JMenu("Help");
    help.setMnemonic(KeyEvent.VK_H);
    menu.add(help);

    JMenuItem controls = new JMenuItem("Controls");
    controls.setMnemonic(KeyEvent.VK_C);
    controls.addActionListener(ml);
    help.add(controls);

    JMenuItem about = new JMenuItem("About");
    about.setMnemonic(KeyEvent.VK_A);
    about.addActionListener(ml);
    help.add(about);

    setJMenuBar(menu);
}

private void initMainPnl(){
    mainPnl = new JPanel(new MigLayout());

    add(mainPnl);
}

private void initToolbar() {
    toolbar = new JToolBar(JToolBar.HORIZONTAL);
    toolbar.add(new JButton());
    mainPnl.add(toolbar,"wrap");
}

private void initTable() {
    MyTable table = new MyTable(new MyTableModel(this));
    JScrollPane sp = new JScrollPane(table);
    sp.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED);

    mainPnl.add(sp,"w 100%, h 100%");
}}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

巷雨优美回忆 2024-12-28 23:40:17

从技术上讲,JToolBar 可以添加到具有任何布局约束的容器中。仅当对于 BorderLayout 时才支持自动将浮动工具栏重新添加到容器。不要将其添加到 mainPanel,而是将其添加到 contentPane:

private void initToolbar() {
    toolbar = new JToolBar(JToolBar.HORIZONTAL);
    toolbar.add(new JButton());
    add(toolbar, BorderLayout.NORTH);
}

顺便说一句:您的代码不会在您的上下文之外进行编译 - MyPanel、MyTableModel、MenuListener 是本地类,在其他地方不可用,而且缺少 main 方法。为了更快地获得更好的帮助,请考虑将来提供 SSCCE :-)

Technically, a JToolBar can be added to a container with any layout constraint. Automatic re-adding a floating toolBar to the container is supported only if for a BorderLayout. Instead of adding to the mainPanel, add it to the contentPane:

private void initToolbar() {
    toolbar = new JToolBar(JToolBar.HORIZONTAL);
    toolbar.add(new JButton());
    add(toolbar, BorderLayout.NORTH);
}

BTW: your code doesn't compile outside of your context - MyPanel, MyTableModel, MenuListener are local classes not available anywhere else plus it's missing a main method. For better help sooner, consider to supply a SSCCE in future :-)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文