简体   繁体   中英

A Multiplicity of Issues With Java Swing

I'm quite inexperienced with Java graphical user interfaces, and I am only utilizing Java's GUI library because it's convenient for what I am currently trying to achieve. I have a JPanel, of which is having... multiple issues.

  • The JEditorPane lies upon the text, eclipsing the JLabel.
  • Not a border appears around the JPanel.
  • When the GUI opens, it's resized to minimum.
  • The JEditorPane is not resizing.
  • Attempting to add grid layouts doesn't work. It reads me a Exception in thread "AWT-EventQueue-0" java.lang.IllegalArgumentException: cannot add to layout: constraint must be a string (or null) error.
  • Half a cheer for newbie questions: how do I add in borders?

Here's my code:

import java.awt.*;
import java.awt.event.*; 
import java.awt.image.*;
import java.io.*;    
import javax.imageio.*;
import javax.swing.*; 
import javax.activation.MimetypesFileTypeMap;


public class UpdateMechanism {
    private static void showGUI() {
        JFrame frame = new JFrame("The Neverhood Restoration Project");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        BufferedImage icoImage = null;
        try {
            icoImage = ImageIO.read(
                frame.getClass().getResource("/nhood.bmp"));
        } catch (IOException e) {
            System.out.println(e);
        }
        frame.setIconImage(icoImage);

        JPanel heapPanel = new JPanel(); 
        frame.setSize(new Dimension(1024, 600));
        heapPanel.setBackground(new Color(0xA64343));

        JLabel headerLabel = new JLabel("The Neverhood Restoration Project");
        headerLabel.setHorizontalTextPosition(JLabel.CENTER);

        try {
            Font sFont = Font.createFont(Font.TRUETYPE_FONT, new File("DUGFB___.TTF")); 
            sFont = sFont.deriveFont(Font.PLAIN, 48);
            GraphicsEnvironment ge = GraphicsEnvironment.getLocalGraphicsEnvironment(); 
            ge.registerFont(sFont);
            headerLabel.setFont(sFont);
        } catch (FontFormatException|IOException e) { 
            System.out.println(e); 
        }

        JEditorPane updateLog = new JEditorPane(); 
        updateLog.setSize(800, Short.MAX_VALUE);
        JScrollPane scrollPane = new JScrollPane(updateLog);   
        updateLog.setEditable(false);   

        try {
            updateLog.setPage("http://theneverhood.sourceforge.net/");
        } catch (IOException e) {
            updateLog.setContentType("text/html");
            updateLog.setText("<html>The application could not load the webpage.</html>");
        }   

        frame.add(headerLabel);
        frame.add(scrollPane);
        frame.pack();
        frame.setVisible(true);
    }

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

Don't use the setSize() method on components. Layout managers MAY use the preferred/minimum/maximum sizes to determine how to lay out the components based on the rules of the layout manager. Instead do

//updateLog.setSize(800, Short.MAX_VALUE);
JScrollPane scrollPane = new JScrollPane(updateLog);   
scrollPane.setPreferredSize( new Dimension(800, 400) );

The JEditorPane lies upon the text, eclipsing the JLabel.

A JFrame uses a BorderLayout by default. Also by default all components get added to the center, but only the last added is displayed. Try:

//frame.add(headerLabel);
frame.add(headerLabel, BorderLayout.NORTH);

This line of code does nothing because you invoke pack() on the frame later:

frame.setSize(new Dimension(1024, 600));

how do I add in borders?

See the section from the Swing tutorial on How to Use Borders . It wouldn't hurt to read the other sections in the tuturial as well. There is one on layout managers.

Edit:

how do I center the text inside of the "BorderLayout.NORTH,"

Read the JLabel API. There is another setHorizontalXXX() method you should be using instead of the one you tried to use:

and why is he background color not displaying?

What is the heap panel? Why would setting the color of that do anything? You didn't add the panel to the frame or add any components to it.

If you want the background of the frame to change then you need to access the content pane. Again to understand why you need to read the Swing tutorial on How to Make Frames which explains about the content pane.

    frame.getContentPane().setBackground(new Color(0xA64343));

and why is he background color not displaying?

You are adding two components to the CENTER location of the default BorderLayout of the frame. If you add the header to the NORTH , it works quite well.

Don't call setSize on the frame if you call pack() .

updateLog.setSize(800, Short.MAX_VALUE); is also useless because the viewport of the wrapping scrollpane also has its own layout which will override your call.

Regardin borders, you can call setBorder and provide any factory value from BorderFactory or provide your own implementation.

Check out this updated version of your code:

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.FontFormatException;
import java.awt.GraphicsEnvironment;
import java.io.File;
import java.io.IOException;

import javax.swing.JEditorPane;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;

public class UpdateMechanism {
    private static void showGUI() {
        JFrame frame = new JFrame("The Neverhood Restoration Project");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        JPanel heapPanel = new JPanel();
        frame.setSize(new Dimension(1024, 600));
        heapPanel.setBackground(new Color(0xA64343));

        JLabel headerLabel = new JLabel("The Neverhood Restoration Project");
        headerLabel.setHorizontalTextPosition(JLabel.CENTER);

        try {
            Font sFont = Font.createFont(Font.TRUETYPE_FONT, new File("DUGFB___.TTF"));
            sFont = sFont.deriveFont(Font.PLAIN, 48);
            GraphicsEnvironment ge = GraphicsEnvironment.getLocalGraphicsEnvironment();
            ge.registerFont(sFont);
            headerLabel.setFont(sFont);
        } catch (FontFormatException | IOException e) {
            System.out.println(e);
        }

        JEditorPane updateLog = new JEditorPane();
        JScrollPane scrollPane = new JScrollPane(updateLog);
        updateLog.setEditable(false);

        try {
            updateLog.setPage("http://theneverhood.sourceforge.net/");
        } catch (IOException e) {
            updateLog.setContentType("text/html");
            updateLog.setText("<html>The application could not load the webpage.</html>");
        }

        frame.add(headerLabel, BorderLayout.NORTH);
        frame.add(scrollPane);
        frame.pack();
        frame.setVisible(true);
    }

    public static void main(String[] args) {
        javax.swing.SwingUtilities.invokeLater(new Runnable() {
            @Override
            public void run() {
                showGUI();
            }
        });
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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