導航:首頁 > 編程語言 > java中frame

java中frame

發布時間:2022-04-12 06:28:26

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

}

閱讀全文

與java中frame相關的資料

熱點內容
程序員看不懂怎麼辦 瀏覽:271
linux操作系統題 瀏覽:765
單片機無符號數加法 瀏覽:227
應用隱藏加密怎麼關閉 瀏覽:269
汽車空調的壓縮機電線有什麼用 瀏覽:429
電腦加密圖片如何取消加密 瀏覽:340
慧凈電子51單片機視頻 瀏覽:343
javamap賦值 瀏覽:165
什麼app可以玩掌機游戲 瀏覽:46
java簡單聊天室 瀏覽:462
通用汽車編程軟體 瀏覽:432
一級抗震框架梁箍筋加密區規定是多少 瀏覽:974
教你如何把安卓手機變成蘋果 瀏覽:11
app編譯分類 瀏覽:323
怎麼用伺服器的資源包 瀏覽:199
oa軟體手機登陸伺服器地址 瀏覽:289
androidrtp打包 瀏覽:723
信息被加密碼了怎麼辦 瀏覽:420
彈出光碟命令 瀏覽:517
kdj公式源碼分享 瀏覽:355