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

java中gui編程

發布時間:2022-09-10 20:07:53

java gui圖形界面編程鍵盤輸入字母轉換大小寫

從鍵盤輸入一個小寫字母,然後輸入出對應的大寫字母,用兩種輸入輸入的方法實現。第一種使用控制輸入輸出,第二種是使用對話框輸入輸出。這樣即可實現javagui圖形界面編程鍵盤輸入字母轉換大小寫。

❷ Java GUI編程

我做了下修改,你運行一下看看。另外還有點小問題,不過我沒時間修改了,去忙了。

importjava.awt.Color;
importjava.awt.Dimension;
importjava.awt.Graphics;
importjavax.swing.JComponent;

{

privateintx;
privateinty;
privatefinalbooleanRIGHT=true;
//privatefinalbooleanLEFT=false;

privatebooleandirection;

publicMovingComponent()
{
x=0;
y=0;
direction=RIGHT;
}
publicintgetX(){
returnx;
}
publicvoidreverse(){
direction=!direction;
}

publicvoidpaintComponent(Graphicsg)
{
g.setColor(Color.GREEN);
g.fillRect(x,y,100,100);
}

publicvoidmove()
{
intwidth=getWidth();
if(direction){
x=x+2;

}else{
x=x-2;
}
repaint();
}
}

importjava.awt.event.ActionEvent;
importjava.awt.event.ActionListener;

importjavax.swing.JFrame;
importjavax.swing.Timer;

{

privateMovingComponentbox;

publicMovingFrame()
{
box=newMovingComponent();
add(box);
setSize(800,600);

TimerListenerlistener=newTimerListener();
Timert=newTimer(10,listener);
t.start();

}


{
@Override
publicvoidactionPerformed(ActionEventevent){
if((box.getX()*2+100>getWidth())||(box.getX()<0)){
//上面為什麼要乘以2,說實話,我暫時沒想明白
box.reverse();
}
System.out.println(box.getX()+""+getWidth());
box.move();
}
}
}importjavax.swing.JFrame;

publicclassMovingTester{

publicstaticvoidmain(Stringargs[])
{
JFrameframe=newMovingFrame();

frame.setVisible(true);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setTitle("MovingBox!");
}
}

❸ java的GUI編程,如何關閉一個窗體,而不是退出整個程序

JFrame的關閉不像Frame,點關閉按鈕自己會關閉,但是那樣做不好.可以用setDefaultCloseOperation(int operation);
其中operation有以下幾種
DO_NOTHING_ON_CLOSE(在 WindowConstants 中定義):不執行任何操作;要求程序在已注冊的 WindowListener 對象的 windowClosing 方法中處理該操作。
HIDE_ON_CLOSE(在 WindowConstants 中定義):調用任意已注冊的 WindowListener 對象後自動隱藏該窗體。
DISPOSE_ON_CLOSE(在 WindowConstants 中定義):調用任意已注冊 WindowListener 的對象後自動隱藏並釋放該窗體。
EXIT_ON_CLOSE(在 JFrame 中定義):使用 System exit 方法退出應用程序。僅在應用程序中使用。
默認情況下,該值被設置為 HIDE_ON_CLOSE。更改此屬性的值將導致激發屬性更改事件,其屬性名稱為 "defaultCloseOperation"。

❹ java的gui編程,高手來救啊

監聽的目前沒有啥好的想法
關於按鍵的有點,給你分享一下,理解不對的話,見諒啊。

你的問題現在是,先按住上方向鍵,坦克向上走,按住上鍵,又按住左鍵了,坦克向左走,此時放開左鍵了,坦克沒有繼續向上走,而需要你重新按向上鍵。

如果我理解對了,你繼續看,不對就算了、、、
如果是上面的問題,你程序的處理應該是監聽到Key按下事件,然後根據Key的值,向不同的方向移動,監聽到Key放開事件,就停止移動

如果想繼續移動,可以通過程序去判斷:
1:在監聽類,中定義一個Map<Integer> 保存按下的Key
2:監聽到Key按下事件,把Key保存到Map中,並向改方向移動。
3:監聽到Key放開事件,把Key從Map中移除,並判斷Map是否為空,為空停止移動,如果不為空,獲取Map中的Key值,繼續向Key所指向方向移動

註:這個方法只支持最多按兩個鍵,多了的話,比如現在按住3個鍵,放開一個了,不處理的話,因為Map不是排序的,隨便獲取一個Key的話,移動的方向可能不是自己想要的,如果要支持多鍵的,可以使用LinkedMap,他可以保存按鍵的保存順序,比如按鍵的順序為a,b,c 放開c的時候,迭代Map,第一個一定是b,你可以向b方向移動,放開b的時候,迭代Map,獲取就是a了

❺ java gui編程基礎

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class Max extends JFrame implements ActionListener{
//JFrame MaxFrame;
JTextField a;
JTextField b;
JTextField c;
JTextField d;

JButton BAd;
double maxc = 0;
int max= 0;
public Max (){
this.setBounds(300,300,300,200);
this.setVisible(true);
// MaxFrame = new JFrame("MaxFrame");
a = new JTextField("0.0");
b = new JTextField("0.0");
c = new JTextField("0.0");
BAd = new JButton("最大數");
d = new JTextField("0.0");
BAd.addActionListener(this);
this.getContentPane().setLayout(new FlowLayout(FlowLayout.CENTER,5,20));
this.getContentPane().add(a);
this.getContentPane().add(b);
this.getContentPane().add(c);
this.getContentPane().add(BAd);
this.getContentPane().add(d);

this.pack();
this.setVisible(true);
this.addWindowListener(new WindowAdapter(){
public void WindowClosing(WindowEvent e){
System.exit(0);
}
});
}
public void actionPerformed(ActionEvent event){

if (Double.valueOf(a.getText()).doubleValue() >Double.valueOf(b.getText()).doubleValue())

maxc = (Double.valueOf(a.getText()).doubleValue());
else maxc = (Double.valueOf(b.getText()).doubleValue());

if (maxc < Double.valueOf(c.getText()).doubleValue())

maxc = (Double.valueOf(c.getText()).doubleValue());
max = (int) maxc;
d.setText("" + max);

}
public static void main(String[] args){
new Max ();
}
}
//把滿意答案給樓上的那位

❻ 如何進行Java GUI圖形用戶界面編程

在遠標做過直接使用javax.swing和java.awt兩個包
一個簡單的GUI程序如下:
packagesix;
importjavax.swing.*;
importjava.awt.*;
importjava.awt.event.ActionEvent;
importjava.awt.event.ActionListener;
publicclassMain extendsJFrame{ //類Main繼承自JFrame
privateJPanel pane = null;
privateJPanel p = null;
privateCardLayout card = null;
privateJButton button_1 = null;
privateJButton button_2 = null;
privateJButton b1 = null,b2 = null,b3 = null;
privateJPanel p1 = null,p2 = null,p3 = null;
publicMain() //
{
super("卡片布局管理器測試");
try{
UIManager.setLookAndFeel("com.sun.java.swing.plaf.windows.WindowsLookAndFeel");
}
catch(Exception ex){
ex.printStackTrace();
}
//創建新卡片布局
card = newCardLayout(5,5);
pane = newJPanel(card);
p = newJPanel();
button_1 = newJButton("< 上一步");
button_2 = newJButton("下一步 >");
b1 = newJButton("1");b2 = newJButton("2");b3 = newJButton("3");
b1.setMargin(newInsets(2,2,2,2));
b2.setMargin(newInsets(2,2,2,2));
b3.setMargin(newInsets(2,2,2,2));
p.add(button_1);p.add(b1);p.add(b2);p.add(b3);p.add(button_2);
p1 = newJPanel();
p2 = newJPanel();
p3 = newJPanel();
p1.setBackground(Color.RED);
p2.setBackground(Color.BLUE);
p3.setBackground(Color.GREEN);
p1.add(newJLabel("JPanel_1"));
p2.add(newJLabel("JPanel_2"));
p3.add(newJLabel("JPanel_3"));
pane.add(p1,"p1");pane.add(p2,"p2");pane.add(p3,"p3");
//翻轉卡片布局動作
button_1.addActionListener(newActionListener(){
publicvoidactionPerformed(ActionEvent e){
card.previous(pane);
}
});
button_2.addActionListener(newActionListener(){
publicvoidactionPerformed(ActionEvent e){
card.next(pane);
}
});
b1.addActionListener(newActionListener(){
publicvoidactionPerformed(ActionEvent e){
card.show(pane, "p1");
}
});
b2.addActionListener(newActionListener(){
publicvoidactionPerformed(ActionEvent e){
card.show(pane,"p2");
}
});
b3.addActionListener(newActionListener(){
publicvoidactionPerformed(ActionEvent e){
card.show(pane, "p3");
}
});
this.getContentPane().add(pane);
this.getContentPane().add(p,BorderLayout.SOUTH);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setSize(300, 200);
this.setVisible(true);
}
publicstaticvoidmain(String[]args)
{
newMain(); //
}
}

❼ java中gui編程使用的多麼

個人理解:gui編程,主要是圖形化界面加上事件的處理,說實話,我感覺用的不多,不過主要是了解gui編程的思想,像他裡面的回調機制,匿名內部類。。。

❽ 簡單的GUI編程java編寫

import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;

import javax.swing.JOptionPane;

public class TestGUI extends Frame{
private TextField tf = null;
private Button btn = null;

public static void main(String[] args) {
new TestGUI().init();
}

public void init() {
//窗口關閉事件
addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {
System.exit(0);
}
});
setLayout(new FlowLayout());
tf = new TextField(15);
btn = new Button("計算");
//按鍵響應事件
btn.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
String s = "你輸入字元的個數為:" + tf.getText().length();
//彈窗顯示結果
JOptionPane.showMessageDialog(null,s);
}
});
add(tf);
add(btn);
setBounds(400, 300, 400, 300); //設置窗體的位置和尺寸
setVisible(true);
}
}

❾ JAVA的GUI編程是不是很重要

不是 其實初學學JAVA不太好 因為太容易 沒有C++那種底層的東西 JAVA在GUI方面和C++ VB不能比 JAVA真正擅長的是在伺服器方面 學習JAVA一定要把重點放在伺服器編程這塊

❿ Java實現GUI編程基本方法都有那些

典型的包括窗口(Window)、框架(Frame/JFrame)、對話框(Dialog/JDialog)、面板(Panel/JPanel)等。只有先創建了這些容器,其它界面元素如按鈕(Button/JButton)、標簽(Label/JLabel)、文本框(TextField/JTextField)等才有地方放。
②添加組件
為了實現GUI應用程序的功能,為了與用戶交換,需要在容器上添加各種組件/控制項。這需要根據具體的功能要求來決定用什麼組件。例如,如果需要提示信息,可用標簽(Label/JLabel);如果需要輸入少量文本,可用文本框(TextField/JTextField);如果需要輸入較多文本,可用文本區域(TextArea/JTextArea);如果需要輸入密碼,可用密碼域(JPasswordField)等等。
③安排組件
與傳統的Windows環境下的GUI軟體開發工具不同,為了更好地實現跨平台,Java程序中各組件的位置、大小一般不是以絕對量來衡量,而是以相對量來衡量。例如有時候,程序的組件的位置是按"東/East"、"西/West"、"南/South"、"北/North"、"中 /Center"這種方位來標識的。因此,在組織界面時,除了要考慮所需的組件種類外,還需要考慮如何安排這些組件的位置與大小。這一般是通過設置布局管理器(Layout Manager)及其相關屬性來實現的。事實上上述按方位來安排組件就是採用了Java中多種布局管理器里的BorderLayout布局管理器。
為了完成一個GUI應用程序所應具備的功能,除了適當地安排各種組件產生美觀的界面外,還需要處理各種界面元素事件,以便真正實現與用戶的交換,完成程序的功能。在Java程序中這一般是通過實現適當的事件監聽者介面來完成的。

閱讀全文

與java中gui編程相關的資料

熱點內容
華為交換機dhcp配置命令 瀏覽:314
androidbitmap縮小 瀏覽:270
單片機串口控制燈 瀏覽:83
大訊雲伺服器安裝視頻 瀏覽:783
華為演算法領先世界 瀏覽:653
linux路由重啟 瀏覽:565
php的模板編程 瀏覽:319
編譯器原理與實現書 瀏覽:708
dos選擇命令 瀏覽:16
apm固件編譯到單片機 瀏覽:120
聯通深藍卡都包含什麼app 瀏覽:263
如何判斷網路伺服器正常 瀏覽:649
路由器搭橋遠端伺服器地址是什麼 瀏覽:515
編譯動態庫時會連接依賴庫嗎 瀏覽:708
淘寶手機加密是隨機的嗎 瀏覽:672
解壓包子怎麼裝飾 瀏覽:585
四個數湊24演算法 瀏覽:676
哪一種不是vi編譯器的模式 瀏覽:169
xp在此處打開命令窗口 瀏覽:128
代碼編譯運行用什麼軟體 瀏覽:999