gpt4 book ai didi

java - 如何在 Java AWT 中使面板在框架内可见?

转载 作者:行者123 更新时间:2023-12-02 02:51:07 27 4
gpt4 key购买 nike

我正在学习 Java AWT 来创建 GUI 应用程序。我正在处理下面的代码,我无法使面板在框架内可见。这是我的代码:

        import java.awt.*;
import java.awt.event.*;

/**
*
* @author kiran
*/
public class UserInterface
{
Frame UI;
private static double UIWidth, UIHeight;



/**
* Constructs User Interface
*/
public UserInterface()
{
UI = new Frame("frame");
Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
UIWidth = screenSize.getWidth();
UIHeight = screenSize.getHeight();
buildFrame();
buildMessageInputArea();
}
/**
* returns the width of the UI
* @return returns the width of the UI
*/
public static double getUIWidth()
{
return UIWidth;
}

/**
* returns the width of the UI
* @return returns the width of the UI
*/
public static double getUIHeight()
{
return UIHeight;
}

/**
* Builds the frame
*/
private void buildFrame()
{
UI.setSize((int)UIWidth,(int)UIHeight*96/100);
UI.setVisible(true);
UI.setLayout(new FlowLayout());
UI.addWindowListener(new Actions());
}

private void buildMessageInputArea()
{
Panel current = new TextAreaPanel().getPanel();
current.setVisible(true);
UI.add(current);

}
}

class TextAreaPanel extends Frame
{
private Panel textAreaPanel;
TextArea msgInputArea;

public TextAreaPanel()
{
textAreaPanel = new Panel();
msgInputArea = new TextArea(1000,(int)UserInterface.getUIWidth() * 80/100);
}

private void addTextArea()
{
textAreaPanel.add(msgInputArea);
}

public Panel getPanel()
{
return textAreaPanel;
}

}

class Actions extends WindowAdapter
{
@Override
public void windowClosing(WindowEvent c)
{
System.exit(0);
}
}

如何使面板在框架内可见?

最佳答案

How do I make a panel visible inside frame in Java AWT?

如所见,代码存在两个基本问题,可以通过更改以下内容来修复:

  1. 在顶级容器上调用 setVisible(true) 之前,将面板/文本区域添加到 GUI。

    虽然可以在容器可见后将组件添加到容器中,但它们需要特殊处理,在这种情况下没有必要。

  2. 将文本区域添加到面板!

这是代码,变成了Minimal, Complete, and Verifiable example通过添加 main(String[]) 方法,实现这两个更改,以及对代码其他方面的更多解释性注释。

import java.awt.*;
import java.awt.event.*;

public class UserInterface {

Frame UI;
private static double UIWidth, UIHeight;

public static void main(String[] args) {
Runnable r = () -> {
new UserInterface();
};
EventQueue.invokeLater(r);
}

/**
* Constructs User Interface
*/
public UserInterface() {
UI = new Frame("frame");
// setting a GUI to full screen while accounting for the task
// bar can be achieved in a single line of code.
Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
UIWidth = screenSize.getWidth();
UIHeight = screenSize.getHeight();
// these need to be called in the reverse order to ensure the
// components are added before the GUI is set visible.
buildMessageInputArea();
buildFrame();
}

/**
* returns the width of the UI
*
* @return returns the width of the UI
*/
public static double getUIWidth() {
return UIWidth;
}

/**
* returns the width of the UI
*
* @return returns the width of the UI
*/
public static double getUIHeight() {
return UIHeight;
}

/**
* Builds the frame
*/
private void buildFrame() {
UI.setSize((int) UIWidth, (int) UIHeight * 96 / 100);
UI.setVisible(true);
UI.setLayout(new FlowLayout());
UI.addWindowListener(new Actions());
}

private void buildMessageInputArea() {
Panel current = new TextAreaPanel().getPanel();
current.setVisible(true);
UI.add(current);
}
}

// does not need to be a fram
//class TextAreaPanel extends Frame {
class TextAreaPanel {

private Panel textAreaPanel;
TextArea msgInputArea;

public TextAreaPanel() {
textAreaPanel = new Panel();
// these number represent columns and rows, not pixels!
//msgInputArea = new TextArea(1000, (int) UserInterface.getUIWidth() * 80 / 100);
msgInputArea = new TextArea(40, 60);
// add the text area to the panel!
textAreaPanel.add(msgInputArea);
}

/** not called by anything else
private void addTextArea() {
textAreaPanel.add(msgInputArea);
}
**/

public Panel getPanel() {
return textAreaPanel;
}
}

// This can be achieved in a single line of code
class Actions extends WindowAdapter {

@Override
public void windowClosing(WindowEvent c) {
System.exit(0);
}
}

添加/扩展 @mKorbel 和 @camickr 的评论:

  1. 为什么使用 AWT?请参阅this answer有很多充分的理由放弃 AWT 组件而转而使用 Swing。
  2. 参见Composition over inheritance .
  3. 在 GUI 中使用static 通常会导致问题,而不是解决问题。大多数(如果不是全部)标记为静态的方法应通过使用对象实例调用方法的代码简化为非静态。

关于java - 如何在 Java AWT 中使面板在框架内可见?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43833843/

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