简体   繁体   English

为什么我的UI在浏览器中运行时会丢失格式而在我的计算机上运行(Java Swing)?

[英]Why does my UI lose formatting when run in-browser vs run on my computer (Java Swing)?

For my CSE 205 (Java programming 2) class, we have to design a really simple GUI applet. 对于我的CSE 205(Java编程2)类,我们必须设计一个非常简单的GUI小程序。 I'm pretty familiar with Swing, having used it for a few of my own projects before hand. 我对Swing非常熟悉,我已经将它用于我自己的一些项目。 I've designed the program with few problems, and it looks perfect on my computer when run from Eclipse: 我设计的程序几乎没有问题,从Eclipse运行时,它在我的计算机上看起来很完美:

在此输入图像描述

But when I got to submit it online, where it runs in browser, the UI gets severely demented, returning back to default: 但是当我在网上提交它,它在浏览器中运行时,UI会严重痴迷,返回默认值:

在此输入图像描述

I've grown accustomed to using the GridBagLayout due to it's simplicity. 我已经习惯了使用GridBagLayout,因为它很简单。 That's what I am using here. 这就是我在这里使用的。 The classes CreatePanel and SelectPanel (as seen in the first image) both extend JPanel (as per my professor). CreatePanel和SelectPanel类(如第一张图片中所示)都扩展了JPanel(根据我的教授)。 I set each using: 我设置每个使用:

this.setLayout(new GridBagLayout());

And and the components to each by: 以及每个组件:

//Call the method addItem() to add the create button to the panel
addItem(this, createB,0,2,2,1,defaultInset,GridBagConstraints.CENTER);



 //-------
    public void addItem(JPanel p, JComponent c, int x, int y, int width, int height, int[] inset, int align)
      {
            GridBagConstraints gc = new GridBagConstraints();
            gc.gridx = x;
            gc.gridy = y;
            gc.gridwidth = width;
            gc.gridheight = height;
            gc.weightx = 0;
            gc.weighty = 0;
            gc.insets = new Insets(inset[0],inset[1],inset[2],inset[3]);
            gc.anchor = align;
            gc.fill = GridBagConstraints.NONE;
            p.add(c, gc);
        }

Anyone have any ideas of what might be going on? 任何人都对可能发生的事情有任何想法? Considering over half my time was spent getting the layouts looking right, I'd love a simple fix. 考虑到超过一半的时间花在让布局看起来正确,我想要一个简单的修复。 I can post more code if needed. 如果需要,我可以发布更多代码。 Thanks for any suggestions. 谢谢你的任何建议。

--EDIT-- - 编辑 -

Alright I've done a bit of playing around based on @ MadProgrammer's suggestions. 好吧,我根据@MadProgrammer的建议做了一些游戏。 I think I've kind of narrowed down where the problem is. 我想我已经缩小了问题的范围。

This is the code I am using to set the size of the JTextField to the right of the label "Enter a Pet Type: 这是我用来将JTextField的大小设置为“输入宠物类型”标签右侧的代码:

petTypeTF = new JTextField("");
petTypeTF.setPreferredSize(new Dimension(125, 60));

Now if I change the code to anything else, like setting the number of columns: 现在,如果我将代码更改为其他任何内容,例如设置列数:

petTypeTF = new JTextField("",12);
 petTypeTF.setPreferredSize(new Dimension(125, 60));

I get a UI that looks identical to the one when I submit it online. 我得到的UI看起来与我在线提交时的UI相同。 Something to do with setting the number of columns seems to be screwing it up. 与设置列数有关的事情似乎搞砸了。 Does this help narrow it down for anyone? 这有助于缩小任何人的范围吗?

There's really not enough code to ascertain the full extend of the problem, but a few ideas might help. 实际上没有足够的代码来确定问题的完整范围,但一些想法可能有所帮助。

GridBagLayout will want to try and get it's components to laid out based on there preferred sizes if possible. 如果可能的话, GridBagLayout将尝试根据首选大小来设置它的组件。 If it can't it will look at there minimum size instead. 如果它不能,它会看到最小尺寸。

Make sure you are creating your text components with columns (and in the case of the text area) rows. 确保使用列(以及文本区域)行创建文本组件。 This will establish the preferred size of the components automatically based on a number of important factors. 这将根据许多重要因素自动确定组件的首选大小。

在此输入图像描述

public class BadLayout11 {

    public static void main(String[] args) {
        new BadLayout11();
    }

    public BadLayout11() {
        EventQueue.invokeLater(new Runnable() {
            @Override
            public void run() {
                try {
                    UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
                } catch (Exception ex) {
                }

                JTabbedPane pane = new JTabbedPane();
                pane.add("Pet List Creation", new TestPane());

                JFrame frame = new JFrame("Test");
                frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                frame.add(pane);
                frame.pack();
                frame.setLocationRelativeTo(null);
                frame.setVisible(true);

            }
        });
    }

    public class TestPane extends JPanel {

        public TestPane() {

            setLayout(new GridBagLayout());

            JPanel fields = new JPanel(new GridBagLayout());

            JTextField field = new JTextField(12);
            JTextArea area = new JTextArea(10, 20);
            JLabel label = new JLabel("Enter a Pet Type");
            JButton button = new JButton("Create a Pet");

            GridBagConstraints gbc = new GridBagConstraints();
            gbc.gridx = 0;
            gbc.gridy = 0;
            gbc.insets = new Insets(2, 2, 2, 2);
            gbc.anchor = GridBagConstraints.WEST;

            fields.add(label, gbc);
            gbc.weightx = 1;
            gbc.gridx++;
            fields.add(field, gbc);

            gbc.fill = GridBagConstraints.NONE;
            gbc.gridx = 0;
            gbc.weightx = 0;
            gbc.gridy++;
            gbc.gridwidth = 2;
            gbc.anchor = GridBagConstraints.CENTER;
            fields.add(button, gbc);

            gbc = new GridBagConstraints();
            gbc.gridx = 0;
            gbc.gridy = 0;
            gbc.insets = new Insets(2, 2, 2, 2);
            gbc.gridheight = GridBagConstraints.REMAINDER;
            gbc.weightx = 1;
            gbc.weighty = 1;
            gbc.fill = GridBagConstraints.BOTH;
            add(fields, gbc);
            gbc.gridx++;
            add(new JScrollPane(area), gbc);

        }

    }

}

Unfortunately, at some point, everything will reach a point that it is simply not possible to maintain the layout and things will either collapse or simply appear clipped. 不幸的是,在某些时候,一切都将达到一个点,即根本不可能维持布局,事情会崩溃或只是被剪裁。

In these cases, you could place the entire container within a JScrollPane , but I'd consider that a worst case scenario in this case. 在这些情况下,您可以将整个容器放在JScrollPane ,但在这种情况下,我认为这是最糟糕的情况。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM