简体   繁体   中英

Window not resizing

I'm writing a method to show and resize a window, but it's not resizing it for some reason.

static void frame(JFrame f) {

    JFrame frame = f;
    int frameWidth = 500;
    int frameHeight = 500;
    Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
    frame.setBounds((int) screenSize.getWidth() - frameWidth, 0, frameWidth, frameHeight);
    frame.setLocationRelativeTo(null);
    frame.setBackground(Color.WHITE);
    frame.setVisible(true);
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}

Here's my paint method:

package events;

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

public class P extends JPanel {

static P g = new P();
int x = 500;
int y = 500;

@Override
public Dimension getPreferredSize() {
    return new Dimension(x, y);

}

public void paintComponent(Graphics g) {

    super.paintComponent(g);

}

}

And this is in my main method:

    frame.add(P.g);
    P.g.setLayout(null);
    text[0] = new JTextArea(0, 0);
    text[0].setLineWrap(true);
    text[0].setEditable(false);
    JScrollPane scroll = new JScrollPane(text[0]);

    scroll.setBounds(20, 20, 450, 110);
    P.g.add(scroll);

    frame(frame);

In this code, we can see two important points:

  • Static usage of the variable P g = new P()
  • There is no call (reference) to JFrame.pack()

First of all, the variable P g = new P() has been initialized, however in this code I can not observer anyone adjusts for WIDTH and HEIGTH associated with preferred size. As second point, in this code there is no reference for JFrame.pack() method, originally inherited from java.awt.Window.pack() . This method is important because it is used on Windows and Frames to be sized to fit the preferred size and layouts of its subcomponents. The method pack() it is not only one way to adjust frames . See quote available on Oracle documentation below:

An alternative to pack is to establish a frame size explicitly by calling setSize or setBounds (which also sets the frame location). In general, using pack is preferable to calling setSize, since pack leaves the frame layout manager in charge of the frame size, and layout managers are good at adjusting to platform dependencies and other factors that affect component size.

Some useful links:


package sample;

import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Toolkit;
import javax.swing.JFrame;
import javax.swing.JPanel;

class P extends JPanel {
    static P g = new P();
    int x = 500;
    int y = 500;

    @Override
    public Dimension getPreferredSize() {
        return new Dimension(x, y);

    }

    @Override
    public void paintComponent(Graphics g) {
        super.paintComponent(g);
    }
}

public class MyFrame extends javax.swing.JFrame {
    public MyFrame() { }
    static void frame(JFrame f) {
        JFrame frame = f;
        int frameWidth = 500;
        int frameHeight = 500;
        Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
        frame.setBounds((int) screenSize.getWidth() - frameWidth, 0, frameWidth, frameHeight);
        frame.setLocationRelativeTo(null);
        frame.setBackground(Color.WHITE);
        frame.setVisible(true);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    }
    public static void main(String args[]) {

        // 1. creating the frame window
        //
        MyFrame myFrame = new MyFrame();
        myFrame.add( P.g );
        P.g.setLayout( null );

        // others usefull  operations
        // configuring others objetcs
        // to be added on P.g

        // text[0] = new JTextArea(0, 0);
        // text[0].setLineWrap(true);
        // text[0].setEditable(false);
        // JScrollPane scroll = new JScrollPane(text[0]);
        // P.g.add(scroll);

        // 2. setting preferred size
        Dimension preferredSize = P.g.getPreferredSize();
        myFrame.setPreferredSize(preferredSize);

        // 3. need apply the pack method (here or any other place)
        // calling your method frame(JFrame)
        frame(myFrame);

        myFrame.setVisible( true );
        myFrame.pack();   
    }             
}

Maybe can be throw a RT-exception as NullPointerException due to plataform dependent issues or because the security questions. See below:

Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
at events.P.paintComponent(P.java:82)
at javax.swing.JComponent.paint(Unknown Source)
at javax.swing.JComponent.paintChildren(Unknown Source)
at javax.swing.JComponent.paint(Unknown Source)
at javax.swing.JComponent.paintChildren(Unknown Source)
at javax.swing.JComponent.paint(Unknown Source)
at javax.swing.JLayeredPane.paint(Unknown Source)
at javax.swing.JComponent.paintChildren(Unknown Source)
at javax.swing.JComponent.paintToOffscreen(Unknown Source)
at javax.swing.BufferStrategyPaintManager.paint(Unknown Source)
at javax.swing.RepaintManager.paint(Unknown Source)
at javax.swing.JComponent.paint(Unknown Source)
at java.awt.GraphicsCallback$PaintCallback.run(Unknown Source)
at sun.awt.SunGraphicsCallback.runOneComponent(Unknown Source)
at sun.awt.SunGraphicsCallback.runComponents(Unknown Source)
at java.awt.Container.paint(Unknown Source)
at java.awt.Window.paint(Unknown Source)
at javax.swing.RepaintManager$4.run(Unknown Source)
at javax.swing.RepaintManager$4.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Source)
at javax.swing.RepaintManager.paintDirtyRegions(Unknown Source)
at javax.swing.RepaintManager.paintDirtyRegions(Unknown Source)
at javax.swing.RepaintManager.prePaintDirtyRegions(Unknown Source)
at javax.swing.RepaintManager.access$1300(Unknown Source)
at javax.swing.RepaintManager$ProcessingRunnable.run(Unknown Source)
at java.awt.event.InvocationEvent.dispatch(Unknown Source)
at java.awt.EventQueue.dispatchEventImpl(Unknown Source)
at java.awt.EventQueue.access$400(Unknown Source)
at java.awt.EventQueue$3.run(Unknown Source)
at java.awt.EventQueue$3.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Source)
at java.awt.EventQueue.dispatchEvent(Unknown Source)
at java.awt.EventDispatchThread.pumpOneEventForFilters(Unknown Source)
at java.awt.EventDispatchThread.pumpEventsForFilter(Unknown Source)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.run(Unknown Source)

So, it can be fixed using the queue via EventQueue more details available at Oracle Docs

Fix the problem wrapping the code inside the main method with EventQueue according to the code below:

public static void main(String args[]) {

    java.awt.EventQueue.invokeLater(new Runnable() {
        @Override
        public void run() {

            // 1. creating the frame window
            //
            MyFrame myFrame = new MyFrame();
            myFrame.add( P.g );
            P.g.setLayout( null );

            // others usefull  operations
            // configuring others objetcs
            // to be added on P.g

            // text[0] = new JTextArea(0, 0);
            // text[0].setLineWrap(true);
            // text[0].setEditable(false);
            // JScrollPane scroll = new JScrollPane(text[0]);
            // P.g.add(scroll);

            // 2. setting preferred size
            Dimension preferredSize = P.g.getPreferredSize();
            myFrame.setPreferredSize(preferredSize);

            // 3. need apply the pack method (here or any other place)
            // calling your method frame(JFrame)
            frame(myFrame);

            myFrame.setVisible( true );
            myFrame.pack();   

        }
    });

}     

PS: @Skillet, sorry for anyone misinterpretation about the code that you provided.

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