A. java中的Frame用法问题
其实这两个都差不多!
new MyFrame("窗体");
是在不需要在这个类里面操作,给别的类来用! 一般用来 返回
return new MyFram("窗体");
B. java中Frame("测试窗口")创建窗口,运行后标题栏显示为居中,应当如何修改该Frame对象标题栏的对齐方式。
方法一.继承JFrame重写setTitle方法。这样你的JFrame所有的标题都是居中的。
方法二.可以试试这个
new JFrame(" 标题");
上面的方法,可以让每个使用你的JFrame的对象,都居中。
但是只有一个窗口的话就使用第二个方法。
C. Java 的Frame和Panel的区别
[Panel]
Panel is the simplest container class. A panel provides space in which an ap
plication can attach any other component, including other panels.
<详见java.awt.Panel>
<注意:以下大写指类,小写泛指对象,比如Frame指Frame类,frame泛指Frame对象。>
好象是看出区别了:Frame是Window的子类,一个Frame对象就是一个有标题有边界
的顶层窗口。Panel是最简单的容器类,是Container的子类。一个Panel对象就是要给应
用程序提供空间,用来添加组件,包括其它的Panel对象。
追本溯源,其实Frame和Panel还是有些亲戚关系的:Frame是Window的直接子类,W
indow又是Container的直接子类,而Panel是Container的直接子类,它们都是从Contai
ner里扩展出来的,是叔侄关系。它们的老祖宗是Component(Container是Component的
子类),Component是基类,回溯本源到此为止,已经是根了。
Frame和Panel都是容器类,那么它们在使用上有什么区别呢?
你可以创建一个panel对象,在上面添加组件,比如单独建立一个TestPanel.java
的源文件(TestPanel extends Panel)。但是因为Panel不是顶层容器,所以你不能直
接显示你创建的这个panel对象,必须装在顶层容器里比如嵌入一个frame里,才能显示
。(为什么Frame对象可以直接显示,而Panel对象不能,文章最后有解释。)
Frame是顶层容器,一般不单独使用(注意只是一般,还是可以直接嵌入组件的),而是
习惯在frame里嵌入panel,再在panel上面添加组件。你在用Jbuilder创建一个Applica
tion(比如TestApp和TestAppFrame)的时候,在你的框架文件TestAppFrame里就会自动
生成一个叫contentPane的JPanel对象。
Panel是一般容器,可以自身嵌套(比如在panel1里嵌入panel2);但Frame已经是
顶层容器了,自身不能嵌套。
我们用得最多的JFrame和JPanel,就是Frame和Panel在Swing下的扩展(JFrame是Fr
ame子类和JPanel是Panel的子类)。
我们用Jbuilder创建Application时会自动生成一些代码,其中的frame.pack()一句
是什么意思?<注:frame是一个已生成的框架类对象>
我们在java.awt.Frame的源文件里找不到pack()方法的定义。看了java.awt.Window
才知
道,原来在Window类里定义了pack()和show()两个显示窗口的方法,被Frame继承了过来
。这可能也是panel无法单独使用的一个原因吧,Panel和它的直接超类Container里,都
没有定义类似pack()和show()的显示容器的方法。
D. 如何在java中建立frame
一般而言可以用两种方法实现。
第一种
importjavax.swing.*;
publicclassFrameDemo1{
publicstaticvoidmain(String[]args){
//创建一个JFrame对象
JFramejf=newJFrame();
//设置窗口的标题栏
jf.setTitle("窗口一");
//创建一个标签组件
JLabeljl=newJLabel("窗口里的标签组件");
//把标签组件添加到窗口界面
jf.add(jl);
//设置窗口的位置
jf.setLocation(200,120);
//设置窗口的大小
jf.setSize(300,280);
//设置窗口的可见性
jf.setVisible(true);
}
}
第二种方法
importjava.awt.*;
importjava.awt.event.*;
importjavax.swing.*;
//写一个类去继承JFrame
{
//定义组件
JLabeljl;
JButtonjb;
publicFrameDemo2(){
//初始化组件
jl=newJLabel("窗口里的标签组件");
jb=newJButton("窗口里的按钮");
//给按钮添加事件响应,点击按钮改变标签组件上的文字的颜色
jb.addActionListener(newActionListener(){
@Override
publicvoidactionPerformed(ActionEvente){
jl.setForeground(Color.RED);
}
});
//设置窗口的布局,为边界布局
this.setLayout(newBorderLayout());
//添加组件到指定的位置
this.add(jl,BorderLayout.CENTER);
this.add(jb,BorderLayout.SOUTH);
//窗口的this.setLocation(120,100)和this.setSize(300,280)的整合写法setBounds(....);
this.setBounds(120,100,300,280);
//设置点击窗口的关闭按钮执行的默认操作,关闭程序
this.setDefaultCloseOperation(EXIT_ON_CLOSE);
//设置窗口的标题栏
this.setTitle("窗口二");
//窗口默认是不可见的,所以需要设置窗口的可见性为true
this.setVisible(true);
}
publicstaticvoidmain(String[]args){
newFrameDemo2();
}
}
E. Java中frame w=new frame什么意思
java中new关键字是用来创建对象实例的。
frame w=new frame();应该是创建一个frame类的实例吧。
如果是JFrame w = new JFrame();就是用来创建一个窗体对象实例w。JFrame是java类库提供的类,用来创建图形界面窗体。
F. java 关于Frame类
当需要扩展Frame的功能或者想在实例化的多做功能的时候,就写上extends frame,
譬如想在实例化Frame的同时设定size
public class MyFrame extends Frame {
public MyFrame(String s, int width, int height) {
super(s);
super.setSize(width, height);
}
}
这样你实例化MyFrame的时候就已经设定好大小了,少写一些代码
public static void main(String args[]){
MyFrame frame = new MyFrame("My Frame", 300, 200);
}
同样功能的不继承,则需要
public static void main(String args[]){
Frame frame = new Frame("My Frame");
frame.setSize(300, 200);
}
clear?
G. JAVA中,Frame和Panel默认的布局管理器分别是什么类型
JAVA中,Framel默认的布局管理器是BorderLayout类型,Panel默认的布局管理器是FlowLayout类型。
FlowLayout是Panel 和 Applet 的默认布局管理器。在该布局管理器中,组件在容器中按照从上到下,从左到右的顺序进行排列,行满后则换行。
BorderLayout是Window、Frame和Dialog的默认布局管理器,其将容器分成North、South、East、West和Center 5个区域,每个区域只能放置一个组件。使用BorderLayout时,如果容器大小发生变换,组件的相对位置不变。
(7)java中frame扩展阅读:
其它相关的布局管理器:
1、网格布局管理器(GridLayout):
GridLayout 可使容器中的各个组件呈网格状布局,平局占据容器的空间,即使容器的大小发生变化,每个组件还是平均占据容器的空间。和FlowLayout一样,GridLayout也是按照从上到下,从左到右的规律进行排列的。
2、卡片布局管理器(CardLayout):
CardLayout能够帮助用户处理两个乃至跟多的成员共享同一显示空间。它把容器分成许多层,每层的显示空间占据整个容器的大小,并且每层之允许反之一个组件,可以通过Panel来实现每层复杂的用户界面。
H. java中 frame有哪些属性及方法,(全部的)
自己可以查看JAVA API文档嘛
我把下面的贴出来已经觉得自己很白痴了
http://gceclub.sun.com.cn/Java_Docs/html/zh_CN/api/java/awt/Frame.html
-------------------
J2SE5.0 API,可以自己参看DOC
字段摘要
static int CROSSHAIR_CURSOR
已过时。 由 Cursor.CROSSHAIR_CURSOR 取代。
static int DEFAULT_CURSOR
已过时。 由 Cursor.DEFAULT_CURSOR 取代。
static int E_RESIZE_CURSOR
已过时。 由 Cursor.E_RESIZE_CURSOR 取代。
static int HAND_CURSOR
已过时。 由 Cursor.HAND_CURSOR 取代。
static int ICONIFIED
此状态位指示将 frame 图标化。
static int MAXIMIZED_BOTH
此状态位掩码指示将 frame 完全最大化(水平和垂直方向)。
static int MAXIMIZED_HORIZ
此状态位指示在水平方向将 frame 最大化。
static int MAXIMIZED_VERT
此状态位指示在垂直方向将 frame 最大化。
static int MOVE_CURSOR
已过时。 由 Cursor.MOVE_CURSOR 取代。
static int N_RESIZE_CURSOR
已过时。 由 Cursor.N_RESIZE_CURSOR 取代。
static int NE_RESIZE_CURSOR
已过时。 由 Cursor.NE_RESIZE_CURSOR 取代。
static int NORMAL
Frame 处于 "normal" 状态。
static int NW_RESIZE_CURSOR
已过时。 由 Cursor.NW_RESIZE_CURSOR 取代。
static int S_RESIZE_CURSOR
已过时。 由 Cursor.S_RESIZE_CURSOR 取代。
static int SE_RESIZE_CURSOR
已过时。 由 Cursor.SE_RESIZE_CURSOR 取代。
static int SW_RESIZE_CURSOR
已过时。 由 Cursor.SW_RESIZE_CURSOR 取代。
static int TEXT_CURSOR
已过时。 由 Cursor.TEXT_CURSOR 取代。
static int W_RESIZE_CURSOR
已过时。 由 Cursor.W_RESIZE_CURSOR 取代。
static int WAIT_CURSOR
已过时。 由 Cursor.WAIT_CURSOR 取代。
从类 java.awt.Component 继承的字段
BOTTOM_ALIGNMENT, CENTER_ALIGNMENT, LEFT_ALIGNMENT, RIGHT_ALIGNMENT, TOP_ALIGNMENT
从接口 java.awt.image.ImageObserver 继承的字段
ABORT, ALLBITS, ERROR, FRAMEBITS, HEIGHT, PROPERTIES, SOMEBITS, WIDTH
构造方法摘要
Frame()
构造 Frame 的一个新实例(初始时不可见)。
Frame(GraphicsConfiguration gc)
使用屏幕设备的指定 GraphicsConfiguration 创建一个 Frame。
Frame(String title)
构造一个新的、初始不可见的、具有指定标题的 Frame 对象。
Frame(String title, GraphicsConfiguration gc)
构造一个新的、初始不可见的、具有指定标题和 GraphicsConfiguration 的 Frame 对象。
方法摘要
void addNotify()
通过将此 Frame 连接到本机屏幕资源,从而使其成为可显示的。
protected void finalize()
移除输入方法和上下文,并从 AppContext 中移除此 Frame。
AccessibleContext getAccessibleContext()
获取与此 Frame 有关的 AccessibleContext。
int getCursorType()
已过时。 从 JDK version 1.1 开始,由 Component.getCursor() 取代。
int getExtendedState()
获取此 frame 的状态。
static Frame[] getFrames()
返回一个数组,包含由应用程序创建的所有 Frame。
Image getIconImage()
获取此 frame 显示在最小化图标中的图像。
Rectangle getMaximizedBounds()
获取此 frame 的最大化边界。
MenuBar getMenuBar()
获取此 frame 的菜单栏。
int getState()
获取此 frame 的状态(已废弃)。
String getTitle()
获得 frame 的标题。
boolean isResizable()
指示此 frame 是否可由用户调整大小。
boolean isUndecorated()
指示此 frame 是否未装饰。
protected String paramString()
返回表示此 Frame 状态的字符串。
void remove(MenuComponent m)
从此 frame 移除指定的菜单栏。
void removeNotify()
通过移除与本机屏幕资源的连接,将此 Frame 设置为不可显示的。
void setCursor(int cursorType)
已过时。 从 JDK version 1.1 开始,由 Component.setCursor(Cursor) 取代。
void setExtendedState(int state)
设置此 frame 的状态。
void setIconImage(Image image)
设置此 frame 要显示在最小化图标中的图像。
void setMaximizedBounds(Rectangle bounds)
设置此 frame 的最大化边界。
void setMenuBar(MenuBar mb)
将此 frame 的菜单栏设置为指定的菜单栏。
void setResizable(boolean resizable)
设置此 frame 是否可由用户调整大小。
void setState(int state)
设置此 frame 的状态(已废弃)。
void setTitle(String title)
将此 frame 的标题设置为指定的字符串。
void setUndecorated(boolean undecorated)
禁用或启用此 frame 的装饰。
从类 java.awt.Window 继承的方法
addPropertyChangeListener, addPropertyChangeListener, addWindowFocusListener, addWindowListener, addWindowStateListener, applyResourceBundle, applyResourceBundle, createBufferStrategy, createBufferStrategy, dispose, getBufferStrategy, getFocusableWindowState, getFocusCycleRootAncestor, getFocusOwner, getFocusTraversalKeys, getGraphicsConfiguration, getInputContext, getListeners, getLocale, getMostRecentFocusOwner, getOwnedWindows, getOwner, getToolkit, getWarningString, getWindowFocusListeners, getWindowListeners, getWindowStateListeners, hide, isActive, isAlwaysOnTop, isFocusableWindow, isFocusCycleRoot, isFocused, isLocationByPlatform, isShowing, pack, postEvent, processEvent, processWindowEvent, processWindowFocusEvent, processWindowStateEvent, removeWindowFocusListener, removeWindowListener, removeWindowStateListener, setAlwaysOnTop, setBounds, setCursor, setFocusableWindowState, setFocusCycleRoot, setLocationByPlatform, setLocationRelativeTo, show, toBack, toFront
从类 java.awt.Container 继承的方法
add, add, add, add, add, addContainerListener, addImpl, applyComponentOrientation, areFocusTraversalKeysSet, countComponents, deliverEvent, doLayout, findComponentAt, findComponentAt, getAlignmentX, getAlignmentY, getComponent, getComponentAt, getComponentAt, getComponentCount, getComponents, getComponentZOrder, getContainerListeners, getFocusTraversalPolicy, getInsets, getLayout, getMaximumSize, getMinimumSize, getMousePosition, getPreferredSize, insets, invalidate, isAncestorOf, isFocusCycleRoot, , isFocusTraversalPolicySet, layout, list, list, locate, minimumSize, paint, paintComponents, preferredSize, print, printComponents, processContainerEvent, remove, remove, removeAll, removeContainerListener, setComponentZOrder, setFocusTraversalKeys, setFocusTraversalPolicy, , setFont, setLayout, transferFocusBackward, transferFocusDownCycle, update, validate, validateTree
从类 java.awt.Component 继承的方法
action, add, addComponentListener, addFocusListener, addHierarchyBoundsListener, addHierarchyListener, addInputMethodListener, addKeyListener, addMouseListener, addMouseMotionListener, addMouseWheelListener, bounds, checkImage, checkImage, coalesceEvents, contains, contains, createImage, createImage, createVolatileImage, createVolatileImage, disable, disableEvents, dispatchEvent, enable, enable, enableEvents, enableInputMethods, firePropertyChange, firePropertyChange, firePropertyChange, firePropertyChange, firePropertyChange, firePropertyChange, firePropertyChange, firePropertyChange, firePropertyChange, getBackground, getBounds, getBounds, getColorModel, getComponentListeners, getComponentOrientation, getCursor, getDropTarget, getFocusListeners, getFocusTraversalKeysEnabled, getFont, getFontMetrics, getForeground, getGraphics, getHeight, getHierarchyBoundsListeners, getHierarchyListeners, getIgnoreRepaint, getInputMethodListeners, getInputMethodRequests, getKeyListeners, getLocation, getLocation, getLocationOnScreen, getMouseListeners, getMouseMotionListeners, getMousePosition, getMouseWheelListeners, getName, getParent, getPeer, getPropertyChangeListeners, getPropertyChangeListeners, getSize, getSize, getTreeLock, getWidth, getX, getY, gotFocus, handleEvent, hasFocus, imageUpdate, inside, isBackgroundSet, isCursorSet, isDisplayable, isDoubleBuffered, isEnabled, isFocusable, isFocusOwner, isFocusTraversable, isFontSet, isForegroundSet, isLightweight, isMaximumSizeSet, isMinimumSizeSet, isOpaque, isPreferredSizeSet, isValid, isVisible, keyDown, keyUp, list, list, list, location, lostFocus, mouseDown, mouseDrag, mouseEnter, mouseExit, mouseMove, mouseUp, move, nextFocus, paintAll, prepareImage, prepareImage, printAll, processComponentEvent, processFocusEvent, processHierarchyBoundsEvent, processHierarchyEvent, processInputMethodEvent, processKeyEvent, processMouseEvent, processMouseMotionEvent, processMouseWheelEvent, removeComponentListener, removeFocusListener, removeHierarchyBoundsListener, removeHierarchyListener, removeInputMethodListener, removeKeyListener, removeMouseListener, removeMouseMotionListener, removeMouseWheelListener, removePropertyChangeListener, removePropertyChangeListener, repaint, repaint, repaint, repaint, requestFocus, requestFocus, requestFocusInWindow, requestFocusInWindow, reshape, resize, resize, setBackground, setBounds, setComponentOrientation, setDropTarget, setEnabled, setFocusable, setFocusTraversalKeysEnabled, setForeground, setIgnoreRepaint, setLocale, setLocation, setLocation, setMaximumSize, setMinimumSize, setName, setPreferredSize, setSize, setSize, setVisible, show, size, toString, transferFocus, transferFocusUpCycle
从类 java.lang.Object 继承的方法
clone, equals, getClass, hashCode, notify, notifyAll, wait, wait, wait
从接口 java.awt.MenuContainer 继承的方法
getFont, postEvent
I. JAVA用frame实现图中2个窗口 怎么写啊
图片看起来很模糊,隐约看到需要一个登录窗口,那就分享一下以前练习的登录窗口demo吧。
先上效果图:
登录界面
源码如下:
AbsoluteLoginFrame.java
public class AbsoluteLoginFrame extends JFrame {
private static final int LOGIN_WIDTH = 600;
private static final int LOGIN_HEIGHT = 400;
private static final long serialVersionUID = -2381351968820980500L;
public AbsoluteLoginFrame(){
//设置窗口标题
setTitle("登录界面");
//设置一个初始面板,填充整个窗口
JPanel loginPanel = new JPanel();
//设置背景颜色
loginPanel.setBackground(new Color(204, 204, 204));//#CCC
loginPanel.setLayout(null);
JPanel centerPanel = new JPanel();
centerPanel.setBackground(Color.WHITE);
centerPanel.setBounds(114, 70, 360, 224);
centerPanel.setLayout(null);
JLabel jLabel = new JLabel("用户名:");
jLabel.setOpaque(true);
jLabel.setBackground(Color.YELLOW);
jLabel.setBounds(60, 60, 54, 20);
JLabel label = new JLabel("密 码:");
label.setOpaque(true);
label.setBackground(Color.CYAN);
label.setBounds(60, 90, 54, 20);
JTextField textField = new JTextField(15);
textField.setBounds(130, 60, 166, 21);
JPasswordField passwordField = new JPasswordField(15);
passwordField.setBounds(130, 90, 166, 21);
JButton jButton = new JButton("登录");
jButton.setBounds(148, 120, 62, 28);
centerPanel.add(jLabel);
centerPanel.add(label);
centerPanel.add(textField);
centerPanel.add(jButton);
centerPanel.add(passwordField);
loginPanel.add(centerPanel);
getContentPane().add(loginPanel);//将初始面板添加到窗口中
setSize(LOGIN_WIDTH, LOGIN_HEIGHT);//设置窗口大小
setLocation(Screen.getCenterPosition(LOGIN_WIDTH, LOGIN_HEIGHT));//设置窗口位置
setDefaultCloseOperation(EXIT_ON_CLOSE);//设置窗口默认关闭方式
setResizable(false);
setVisible(true);
}
public static void main(String[] args) {
new AbsoluteLoginFrame();
}
}
Screen.java
public class Screen {
private int width;
private int height;
public Screen(){
Toolkit toolkit = Toolkit.getDefaultToolkit();
Dimension screenSize = toolkit.getScreenSize();
this.width = screenSize.width;
this.height = screenSize.height;
}
public static Point getCenterPosition(int width, int height){
Screen screen = new Screen();
int x = (screen.getWidth() - width) / 2;
int y = (screen.getHeight() - height) / 2;
return new Point(x, y);
}
public int getWidth() {
return width;
}
public void setWidth(int width) {
this.width = width;
}
public int getHeight() {
return height;
}
public void setHeight(int height) {
this.height = height;
}
}
J. Java Frame 类的用法
原因在你你的class名字就是Panel.java和java.awt.Panel同名了,JVM默认搜索从本类开始,修改
import java.awt.Button;
import java.awt.Frame;
public class Panel {
/**
* @param args
*/
public static void main(String[] args) {
Frame f = new Frame();
f.setBounds(100, 100, 300, 300);
Button b = new Button("BB");
java.awt.Panel p = new java.awt.Panel();
p.add(b);
f.add(p);
f.setVisible(true);
}
}