如何在JavaAWT中使面板在框架内可见?
我正在学习JavaAWT来创建GUI应用程序。我正在处理下面的代码,无法使面板在框架内可见。这是我的密码:如何在JavaAWT中使面板在框架内可见?,java,swing,awt,panel,frame,Java,Swing,Awt,Panel,Frame,我正在学习JavaAWT来创建GUI应用程序。我正在处理下面的代码,无法使面板在框架内可见。这是我的密码: import java.awt.*; import java.awt.event.*; /** * * @author kiran */ public class UserInterface { Frame UI;
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);
}
}
如何使面板在框架内可见
如何在JavaAWT中使面板在框架内可见
正如所见,代码存在两个基本问题,可以通过更改以下内容来解决:
setVisible(true)
之前,将面板/文本区域添加到GUI
虽然可以在容器可见后将组件添加到容器中,但它们需要特殊处理,在这种情况下不需要这样做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的评论:
static
通常会导致问题,而不是修复问题。大多数(如果不是全部)标记为静态的方法都应该通过使用对象实例调用该方法的代码简化为非静态方法setVisible(true)
之前,将面板/文本区域添加到GUI
虽然可以在容器可见后将组件添加到容器中,但它们需要特殊处理,在这种情况下不需要这样做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的评论:
static
通常会导致问题,而不是修复问题。大多数(如果不是全部)标记为静态的方法都应该通过使用对象实例调用该方法的代码简化为非静态方法扩展Frame或扩展JFrame、aa和TextArea或JTextArea、Panel或JPanel,请使用Swing而不是旧的awt。请参阅上的Swing教程中的部分。
TextDemo
代码将向您展示如何更好地构建代码。不需要所有扩展类或静态方法。从教程中的工作代码开始并进行更改。@camickr他不是在问Swing,他想知道如何使用AWT。@Programmer101,无论使用AWT还是Swing,逻辑都是一样的,只是组件名称发生了更改。我的评论是因为当前代码的设计很糟糕。没有理由不能使用Swing教程中的代码结构。仅仅因为有人问一些问题并不意味着他们知道他们在谈论什么。问这个问题时,为什么OP中包含了“摇摆标签”?在任何情况下,如果建议不合适,OP都应该回答并澄清问题。@Programgrammer 101是因为OP不理解AWT和Swing之间的区别吗?建议OP重新考虑他们对API的选择是否是不明智的,因为这些API至少仍在使用中?OP使用AWT有什么原因吗?由于Swing位于AWT之上,并且完全缺乏AWT教程,因此将Swing作为信息的可行来源extends Frame或extends JFrame、aa和TextArea或JTextArea、Panel或JPanel引用没有什么错,请使用Swing而不是旧的AWT,从Swing教程中删除该部分。TextDemo
代码将向您展示如何更好地构建代码。不需要所有扩展类或静态方法。从教程中的工作代码开始并进行更改。@camickr他不是在问Swing,他想知道如何使用AWT。@Programmer101,无论使用AWT还是Swing,逻辑都是一样的,只是组件名称发生了更改。我的评论是因为当前代码的设计很糟糕。没有理由不能使用Swing教程中的代码结构。仅仅因为有人问一些问题并不意味着他们知道他们在谈论什么。问这个问题时,为什么OP中包含了“摇摆标签”?在任何情况下,如果建议不合适,OP都应该回答并澄清问题。@Programgrammer 101是因为OP不理解AWT和Swing之间的区别吗?建议OP重新考虑他们对API的选择是否是不明智的,因为这些API至少仍在使用中?OP使用AWT有什么原因吗?由于Swing位于AWT之上,而且完全没有AWT教程,所以将Swing作为可行的信息源引用并没有什么错