gpt4 book ai didi

java - 窗口未调整大小

转载 作者:行者123 更新时间:2023-11-30 08:18:51 24 4
gpt4 key购买 nike

我正在编写一种方法来显示窗口并调整窗口大小,但由于某种原因它没有调整窗口大小。

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);
}

这是我的绘画方法:

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);

}

}

这是我的主要方法:

    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);

最佳答案

在这段代码中,我们可以看到两个要点:

  • 变量的静态用法P g = new P()
  • 没有调用(引用)JFrame.pack()

首先,变量P g = new P()已经初始化,但是在这段代码中我无法观察到任何人调整WIDTH高度与首选尺寸相关。第二点,在此代码中没有引用最初继承自 java.awt.Window.pack()JFrame.pack() 方法。此方法很重要,因为它在 Windows 和 Frames 上使用,可以调整大小以适合其子组件的首选大小和布局。 pack() 方法不仅仅是调整帧的一种方法。请参阅以下 Oracle 文档中的引用:

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.

一些有用的链接:

<小时/>
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();
}
}

由于平台相关问题或安全问题,可能会抛出 RT 异常作为 NullPointerException。见下文:

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)

因此,可以通过 EventQueue 使用队列来修复此问题,更多详细信息请访问 Oracle Docs

根据以下代码修复使用 EventQueue 将代码包装在 main 方法内的问题:

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,对于任何人对您提供的代码的误解表示抱歉。

关于java - 窗口未调整大小,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29266703/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com