导出的Swing项目显示不正常。

huangapple 未分类评论51阅读模式
英文:

Exported swing project not displaying properly

问题

我最近一直在尝试使用Swing(初学者),在为朋友制作一个应用程序时遇到了问题。我使用的是IDEA IDE,布局和所有的框架都显示正常,但当我使用Maven导出项目时,jar文件显示的布局中缺少了面板和按钮。

我的框架源代码如下:
(注意:我删除了所有其他的方法,因为它们没有修改布局(只有其他对象的getter和setter),我还删除了按钮的actionCalls和其他类似的代码。)

public class OOBFrame extends JFrame {
    // ...(你的代码继续)

    public OOBFrame(String title) {
        super(title);

        setLayout(new GridBagLayout());
        GridBagConstraints gc = new GridBagConstraints();

        c.setBackground(Gui.BACKGROUND);
        c.setLayout(new GridBagLayout());
        c.setPreferredSize(Gui.getMonitorSize());

        // ...(你的代码继续)

        validate();
        pack();
        setExtendedState(JFrame.MAXIMIZED_BOTH);
        setVisible(false);
    }
}

导出的Swing项目显示不正常。

谢谢你提供关于我可能做错了什么的任何见解。
该框架在应用程序的上一个窗口中也使用了setVisible(true),所以这不是问题。
如果需要,我可以提供其他标签和面板的任何代码,或者整个项目的代码。

编辑后:

运行jar的控制台输出。
D:\GitProjects\KSP-NoU-Battle System 3\target>java -cp Battle_Simulator_3.0-1.0.0.0.jar impl.App
java.nio.file.NoSuchFileException: database.csv
[ERR 17:02:24] java.nio.file.NoSuchFileException: countermeasures.txt
[ERR 17:02:24] java.nio.file.NoSuchFileException: weapons.txt
[LOG 17:02:24] Crafts saved:      [0]
然后日志结束,但应用程序的第一个窗口启动。然后是错误的窗口。

解决方案:应用程序缺少资源文件,导致面板无法加载。
英文:

I have been experimenting with Swing lately (beginner) and while trying to make one application for a friend, I encountered a problem. I am using IDEA IDE and the layout and all frames displays properly, but when I export the project using maven, the jar file shows the layout with missing panels and buttons.

My source code for the frame is:
(Note: I removed all other methods as they have not been modifying the layout (only getters and setters for other objects and I also removed actionCalls and other such code for the buttons.)

public class OOBFrame extends JFrame {
    private final Container c = getContentPane();
    private final DefaultListModel<Craft> textForWhite = new DefaultListModel<>();
    private final DefaultListModel<Craft> textForBlack = new DefaultListModel<>();
    private final JCraftList<Craft> whiteListedCrafts = new JCraftList<>(textForWhite);
    private final JCraftList<Craft> blackListedCrafts = new JCraftList<>(textForBlack);
    private final JScrollPane scrollerWhite = new JScrollPane(whiteListedCrafts);
    private final JScrollPane scrollerBlack = new JScrollPane(blackListedCrafts);
    private final SortedMap<Craft, ArrayList<JButton>> templateButtons = new TreeMap<>();
    private final JButton nextScreen = new JButton("Finish");
    private final JButton editing = new JButton("Edit selected");
    private final ArrayList<Craft> selectedCraftsFromList = new ArrayList<>();
    private JCraftPanel details;
    private final GridBagConstraints gcDetails;
    private JList<Craft> lastSelectedList;

    public OOBFrame(String title) {
        super(title);

        setLayout(new GridBagLayout());
        GridBagConstraints gc = new GridBagConstraints();

        c.setBackground(Gui.BACKGROUND);
        c.setLayout(new GridBagLayout());
        c.setPreferredSize(Gui.getMonitorSize());

        gc.gridx = 0;
        gc.gridy = 0;
        gc.fill = GridBagConstraints.HORIZONTAL;
        gc.anchor = GridBagConstraints.NORTH;
        gc.gridwidth = 6;
        getContentPane().add(new JMenuExt(1), gc);

        whiteListedCrafts.setBackground(Gui.BACKGROUND);
        whiteListedCrafts.setForeground(Color.WHITE);

        blackListedCrafts.setBackground(Gui.BACKGROUND);
        blackListedCrafts.setForeground(Color.WHITE);

        gc = new GridBagConstraints();
        gc.gridheight = OOB.TEMPLATE.getCrafts().size();
        gc.fill = GridBagConstraints.BOTH;
        gc.weighty = 4;
        gc.weightx = 1;
        gc.gridx = 0;
        gc.gridy = 1;
        getContentPane().add(scrollerWhite, gc);

        gc.gridx = 4;
        gc.gridwidth = 2;
        getContentPane().add(scrollerBlack, gc);


        gc.fill = GridBagConstraints.HORIZONTAL;
        gc.gridwidth = 1;
        gc.gridheight = 1;
        gc.gridy = 0;
        gc.gridx = 1;
        gc.weightx = 0.1;

        for (Craft craft : OOB.TEMPLATE.getCrafts()) {
            ++gc.gridy;
            templateButtons.put(craft, new ArrayList<>());
            templateButtons.get(craft).add(new JButton("+"));
            templateButtons.get(craft).add(new JButton("+"));

            c.add(templateButtons.get(craft).get(0), gc);
            ++gc.gridx;
            gc.weightx = 8;

            JLabel newLabel = new JLabel(craft.getName());
            newLabel.setForeground(Gui.FOREGROUND);
            newLabel.setHorizontalAlignment(SwingConstants.CENTER);
            newLabel.setPreferredSize(new Dimension(200, 20));
            c.add(newLabel, gc);

            gc.weightx = 0.1;
            ++gc.gridx;
            c.add(templateButtons.get(craft).get(1), gc);
            gc.gridx -= 2;

            templateButtons.get(craft).get(0).addActionListener(e -> {
                Craft newCraft = craft.copy(Side.WHITE);
                OOB.WHITE.addCraft(newCraft);
                textForWhite.addElement(newCraft);
                whiteListedCrafts.updateUI();
            });
            templateButtons.get(craft).get(1).addActionListener(e -> {
                Craft newCraft = craft.copy(Side.BLACK);
                OOB.BLACK.addCraft(newCraft);
                textForBlack.addElement(newCraft);
                blackListedCrafts.updateUI();
            });
        }

        gc.anchor = GridBagConstraints.SOUTH;
        ++gc.gridy;
        ++gc.gridy;
        gc.gridx = 5;
        gc.fill = GridBagConstraints.BOTH;
        //nextScreen.setPreferredSize(new Dimension(200, 100));
        getContentPane().add(nextScreen, gc);

        details = new JCraftPanel(" ", selectedCraftsFromList, false);
        gcDetails = new GridBagConstraints();
        gcDetails.gridx = 0;
        gcDetails.gridy = gc.gridy;
        gcDetails.gridwidth = 4;
        gcDetails.gridheight = 2;
        gcDetails.weighty = 2;
        gcDetails.fill = GridBagConstraints.BOTH;
        add(details, gcDetails);

        setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
        validate();
        pack();
        setExtendedState(JFrame.MAXIMIZED_BOTH);
        setVisible(false);
    }
}

导出的Swing项目显示不正常。

Thank you for any insight of what I could do wrong.
The Frame is also setVisible(true) in previous frame of the application, so that is not an issue.
I can provide any code of other labels and panels if needed or the whole project.

Edited:

Console run of the jar.
D:\GitProjects\KSP-NoU-Battle System 3\target>java -cp Battle_Simulator_3.0-1.0.0.0.jar impl.App
java.nio.file.NoSuchFileException: database.csv
[ERR 17:02:24] java.nio.file.NoSuchFileException: countermeasures.txt
[ERR 17:02:24] java.nio.file.NoSuchFileException: weapons.txt
[LOG 17:02:24] Crafts saved:      [0]

Then the log ends but the application first window starts. Followed by the wrong window.

Solved: Application was missing resource files resulting in non-loadable panels.

huangapple
  • 本文由 发表于 2020年5月29日 22:40:19
  • 转载请务必保留本文链接:https://java.coder-hub.com/62088498.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定