0
我正在學習Java AWT來創建GUI應用程序。我正在處理下面的代碼,我無法在框架內顯示面板。這是我的代碼:如何在Java AWT框架中顯示面板?
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);
}
}
我該如何讓面板在框架內可見?
框架延伸或擴展的JFrame,AAAND文本區域或JTextArea中,面板或JPanel的,請使用搖擺,而不是老AWT – mKorbel
閱讀從Swing教程中的部分上[如何使用文本區域(HTTP://文檔.oracle.com/JavaSE的/教程/ uiswing /組件/ textarea.html)。 'TextDemo'代碼將告訴你如何更好地構建你的代碼。不需要所有擴展類或靜態方法。從教程中的工作代碼開始,並進行更改.. – camickr
@camickr他並不是在問Swing,他想知道如何用AWT做到這一點。 – CodingNinja