當前位置:首頁 » 編程語言 » java怎麼做圖形界面

java怎麼做圖形界面

發布時間: 2023-08-05 21:10:50

① 怎樣用java編寫圖形界面的Application程序

java編寫圖形界面需要用到swing等組件,可以在eclipse中安裝windowbuilder來開發窗體,自動生成窗體代碼,然後自己再根據需要修改,如:


package mainFrame;


import java.awt.EventQueue;

import java.awt.event.MouseAdapter;

import java.awt.event.MouseEvent;


import javax.swing.ImageIcon;

import javax.swing.JButton;

import javax.swing.JCheckBox;

import javax.swing.JFrame;

import javax.swing.JLabel;

import javax.swing.JPanel;

import javax.swing.JPasswordField;

import javax.swing.JTextField;

import javax.swing.SwingConstants;

import javax.swing.UIManager;

import javax.swing.;

import javax.swing.border.EmptyBorder;


public class Mian_login extends JFrame {


private JPanel contentPane;

private JTextField text_LoginName;

private JPasswordField Login_password;


/**

* Launch the application.

*/

public static void main(String[] args) {

EventQueue.invokeLater(new Runnable() {

@Override

public void run() {

try {

Mian_login frame = new Mian_login();

frame.setVisible(true);

} catch (Exception e) {

e.printStackTrace();

}

}

});

}


/**

* Create the frame.

*/

public Mian_login() {

setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

setBounds(500, 200, 443, 300);

setResizable(false);

setTitle("登 錄");

/*獲取系統按鈕樣式*/

String lookAndFeel = UIManager.getSystemLookAndFeelClassName();

try {

UIManager.setLookAndFeel(lookAndFeel);

} catch (ClassNotFoundException e1) {

e1.printStackTrace();

} catch (InstantiationException e1) {

e1.printStackTrace();

} catch (IllegalAccessException e1) {

e1.printStackTrace();

} catch ( e1) {

e1.printStackTrace();

}

contentPane = new JPanel();

contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));

setContentPane(contentPane);

contentPane.setLayout(null);

JPanel panel = new JPanel();

panel.setOpaque(false);

panel.setBounds(0, 0, 434, 272);

contentPane.add(panel);

panel.setLayout(null);

JButton btn_Login = new JButton("u767Bu5F55");

btn_Login.addMouseListener(new MouseAdapter() {

@Override

public void mouseClicked(MouseEvent e) {

}

});

btn_Login.setBounds(88, 195, 70, 23);

panel.add(btn_Login);

JButton btn_cancel = new JButton("u53D6u6D88");

btn_cancel.addMouseListener(new MouseAdapter() {

@Override

public void mouseClicked(MouseEvent e) {

dispose();

}

});

btn_cancel.setBounds(268, 195, 70, 23);

panel.add(btn_cancel);

JLabel lblNewLabel_name = new JLabel("u7528u6237u540D");

lblNewLabel_name.setHorizontalAlignment(SwingConstants.CENTER);

lblNewLabel_name.setOpaque(true);

lblNewLabel_name.setBounds(88, 48, 70, 23);

panel.add(lblNewLabel_name);

JLabel lblNewLabel_passwd = new JLabel("u5BC6u7801");

lblNewLabel_passwd.setHorizontalAlignment(SwingConstants.CENTER);

lblNewLabel_passwd.setOpaque(true);

lblNewLabel_passwd.setBounds(88, 102, 70, 23);

panel.add(lblNewLabel_passwd);

JCheckBox chckbx_remember = new JCheckBox("u8BB0u4F4Fu5BC6u7801");

chckbx_remember.setBounds(102, 150, 84, 23);

panel.add(chckbx_remember);

text_LoginName = new JTextField();

text_LoginName.setBounds(182, 48, 156, 23);

panel.add(text_LoginName);

text_LoginName.setColumns(10);

Login_password = new JPasswordField();

Login_password.setBounds(182, 102, 156, 23);

panel.add(Login_password);

JCheckBox chckbx_AutoLogin = new JCheckBox("u81EAu52A8u767Bu5F55");

chckbx_AutoLogin.setBounds(233, 150, 84, 23);

panel.add(chckbx_AutoLogin);

JLabel Label_background = new JLabel("");

Label_background.setIcon(new ImageIcon("E:\JAVA_workplace\0002-u754Cu9762u8BBEu8BA1\images\background3.jpg"));

Label_background.setBounds(0, 0, 437, 272);

contentPane.add(Label_background);

}

}


② 用Java語言設計一個界面,

  1. 首先:採用什麼技術實現

java語言可以使用awt 和swing等技術實現圖形界面

推薦使用Swing,因為Swing比AWT更專業,更漂亮,組件更豐富,功能更強大。


2. 其次:分析採用什麼布局

邊界布局BorderLayout,配合表格布局GridLayout,既簡單又美觀


3. 最後:分析需求中需要用的組件

學生姓名 學號 顯示信息 需要用到文本框JTextField

單選按鈕 需要用到組件JRadioButton

復選框 需要用到組件JCheckBox

組合框 需要用到組件JComboBox


圖片效果

//導入所需要的包
importjava.awt.event.*;
importjavax.swing.border.*;
importjavax.swing.*;
importjava.awt.*;
{//寫一個類繼承自JFrame窗體
//定義組件
=1L;
privateJPanelcontentPane;
privateJTextFieldtfName,tfNum,allInfo;
privateJRadioButtonrb1,rb2;
privateJCheckBoxcb1,cb2,cb3;
privateJComboBox<String>t1,t2,t3;
publicstaticvoidmain(String[]args){
EventQueue.invokeLater(newRunnable(){
publicvoidrun(){
try{
ClassFrameframe=newClassFrame();//創建一個窗口實例
frame.setVisible(true);//讓該窗口實例可見
}catch(Exceptione){
e.printStackTrace();
}
}
});
}
/**
*窗口屬性的設置,內部組件的初始化
*/
publicClassFrame(){
setTitle("選課ing...");//標題
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);//設置關閉是退出JVM
setSize(450,339);//設置窗體大小
setLocationRelativeTo(null);//窗體居中
contentPane=newJPanel();//內容面板
contentPane.setBorder(newEmptyBorder(5,5,5,5));
contentPane.setLayout(newBorderLayout(0,0));//設置布局
setContentPane(contentPane);
JPanelpanel=newJPanel(newGridLayout(5,1,5,10));//5行1列的表格布局
panel.setBorder(newTitledBorder(null,"",TitledBorder.LEADING,TitledBorder.TOP,null,null));
contentPane.add(panel,BorderLayout.CENTER);//給panel添加邊框
JPanelpanel_1=newJPanel();
panel.add(panel_1);
JLabellabel=newJLabel("姓名");
panel_1.add(label);
tfName=newJTextField();
panel_1.add(tfName);
tfName.setColumns(10);
JLabellabel_2=newJLabel("學號");
panel_1.add(label_2);
tfNum=newJTextField();
tfNum.setColumns(10);
panel_1.add(tfNum);
rb1=newJRadioButton("男");
panel_1.add(rb1);
rb1.setSelected(true);//設置單選按鈕中,默認選擇的按鈕
rb2=newJRadioButton("女");
panel_1.add(rb2);
ButtonGroupbts=newButtonGroup();//單選按鈕需要加入同一個ButonGroup中才能生效
bts.add(rb1);
bts.add(rb2);
JPanelpanel_2=newJPanel();
panel.add(panel_2);
cb1=newJCheckBox("高等數學");
panel_2.add(cb1);
t1=newJComboBox<String>();
t1.setModel(newDefaultComboBoxModel<String>(newString[]{"林老師","趙老師","孫老師"}));
panel_2.add(t1);
JPanelpanel_3=newJPanel();
panel.add(panel_3);
cb2=newJCheckBox("世界經濟");
panel_3.add(cb2);
t2=newJComboBox<String>();
t2.setModel(newDefaultComboBoxModel<String>(newString[]{"張老師","劉老師"}));
panel_3.add(t2);
JPanelpanel_4=newJPanel();
panel.add(panel_4);
cb3=newJCheckBox("音樂賞析");
panel_4.add(cb3);
t3=newJComboBox<String>();
t3.setModel(newDefaultComboBoxModel<String>(newString[]{"王老師","周老師"}));
panel_4.add(t3);
JPanelpanel_5=newJPanel();
panel.add(panel_5);
JButtonjbOk=newJButton("確定");
panel_5.add(jbOk);
JButtonjbRest=newJButton("重填");
panel_5.add(jbRest);
JPanelpanelSouth=newJPanel();
contentPane.add(panelSouth,BorderLayout.SOUTH);
JLabellabe=newJLabel("選課信息");
labe.setHorizontalAlignment(SwingConstants.LEFT);
panelSouth.add(labe);
allInfo=newJTextField();
allInfo.setColumns(30);
panelSouth.add(allInfo);
JPanelpanelNorth=newJPanel();
contentPane.add(panelNorth,BorderLayout.NORTH);
JLabellabelTitle=newJLabel("學生選課界面");
labelTitle.setForeground(Color.DARK_GRAY);
labelTitle.setFont(newFont("宋體",Font.BOLD,20));
panelNorth.add(labelTitle);

//給確定按鈕添加事件處理代碼
jbOk.addActionListener(newActionListener(){
publicvoidactionPerformed(ActionEvente){
StringBuilderinfo=newStringBuilder();
Stringname=tfName.getText();
Stringnum=tfNum.getText();
Stringsex;
if(rb1.isSelected()){
sex="男";
}else{
sex="女";
}
info.append(name+num+sex);
if(cb1.isSelected()){
Stringc=cb1.getText();
Stringt=t1.getSelectedItem().toString();
info.append(""+c+t);
}
if(cb2.isSelected()){
Stringc=cb2.getText();
Stringt=t2.getSelectedItem().toString();
info.append(""+c+t);

}
if(cb3.isSelected()){
Stringc=cb3.getText();
Stringt=t3.getSelectedItem().toString();
info.append(""+c+t);
}
allInfo.setText(info.toString());//把學生信息和選課信息放到文本框
}
});
//給重填按鈕設置事件處理代碼
jbRest.addActionListener(newActionListener(){
publicvoidactionPerformed(ActionEvente){
tfName.setText("");
tfNum.setText("");
rb1.setSelected(true);
cb1.setSelected(false);
t1.setSelectedIndex(0);
cb2.setSelected(false);
t2.setSelectedIndex(0);
cb3.setSelected(false);
t3.setSelectedIndex(0);
allInfo.setText("");
}
});
}
}

③ 在java中,設計圖形用戶界面需要經歷哪幾個基本步驟

1, 分析業務需求, 理順業務邏輯
2, 根據業務需要考慮使用何種容器(JFrame ,Frame ,JWindow..) 需要的數量.
3, 每個容器(窗口)使用組件
4.組件使用何種布局方式(邊界布局,絕對布局,網格布局..),排列在容器(窗口)中
5.組件中需要響應那些事件, 把事件響應代碼寫好,然後綁定到組件上(addListener...)
6.調試,測試

④ java怎麼實現圖形化界面

java圖形化界面還是有很多內容要學習的,可以參考 如下實例:

publicclassTestextendsJFrame{
MyPanelmp=null;
publicstaticvoidmain(String[]args){
//TODOAuto-generatedmethodstub
Testjf=newTest();
}
publicTest(){
mp=newMyPanel();
this.add(mp);
//設置標題
this.setTitle("繪圖");
//設置窗體大小
this.setSize(400,300);
//設置窗體的位置
this.setLocation(100,100);
//限制窗體的大小
this.setResizable(false);
//關閉窗體時,同時退出java虛擬機
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
//顯示窗體
this.setVisible(true);
}
}
//定義一個MyPanel(我自己的面板,用於繪圖和實現繪圖區域)
classMyPanelextendsJPanel
{
//覆蓋JPanel的paint方法
//Graphics是繪圖的重要類,可以把它理解成一隻畫筆
publicvoidpaint(Graphicsg)
{
//1。調用父類函數完成初始化
super.paint(g);
////畫圓
//g.drawOval(100,100,20,20);
////畫直線
//g.drawLine(50,150,150,200);
////畫矩形邊框
//g.drawRect(150,150,30,40);
//
////設置顏色。默認為黑色
//g.setColor(Color.blue);
////填充矩形
//g.fillRect(10,10,20,30);
//畫弧形
g.drawArc(200,10,100,150,120,-80);
//在面板上畫圖片
Imageim=Toolkit.getDefaultToolkit().getImage(Panel.class.getResource("圖片路徑"));
//顯示圖片
g.drawImage(im,10,10,200,180,this);
//畫字
g.setColor(Color.red);
g.setFont(newFont("華文彩雲",Font.BOLD,20));
g.drawString("要寫的字",80,220);
}
}

⑤ Java編程 設計一個圖形用戶界面。界麵包括三個單選按鈕、兩個復選框、一個列表、一個文本區和一個按

程序如下:
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.ButtonGroup;
import javax.swing.JButton;
import javax.swing.JCheckBox;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JRadioButton;
import javax.swing.JTextArea;
public class JFrameDemo extends JFrame implements ActionListener
{
private JPanel panel;
private JButton button;
private JTextArea textArea;
private JCheckBox musicBox;
private JCheckBox danceBox;
private JRadioButton hanButton;
private JRadioButton manButton;
private JRadioButton huiButton;
private ButtonGroup buttonGroup;
public JFrameDemo()
{
panel = new JPanel();
button = new JButton("確定");
textArea = new JTextArea(40,30);
musicBox = new JCheckBox("唱歌"橡滲);
danceBox = new JCheckBox("跳舞梁念脊"高輪);
huiButton = new JRadioButton("回族");
hanButton = new JRadioButton("漢族");
manButton = new JRadioButton("滿族");

buttonGroup = new ButtonGroup();
buttonGroup.add(huiButton);
buttonGroup.add(hanButton);
buttonGroup.add(manButton);

panel.setLayout(new FlowLayout(3));
panel.add(huiButton);
panel.add(hanButton);
panel.add(manButton);
panel.add(musicBox);
panel.add(danceBox);
panel.add(button);
panel.add(textArea);

add(panel);
setTitle("選擇興趣愛好");
setBounds(100, 100, 400, 280);
setResizable(false);
setVisible(true);

this.button.addActionListener(this);

}
public static void main(String[] args)
{
new JFrameDemo();
}
@Override
public void actionPerformed(ActionEvent e)
{
if(e.getSource() == this.button)
{
String info = "";
if(this.huiButton.isSelected())
{
info += this.huiButton.getText() + "\n";
}
if(this.hanButton.isSelected())
{
info += this.hanButton.getText() + "\n";
}
if(this.manButton.isSelected())
{
info += this.manButton.getText() + "\n";
}
if(this.danceBox.isSelected())
{
info += this.danceBox.getText() + "\n";
}
if(this.musicBox.isSelected())
{
info += this.musicBox.getText() + "\n";
}
this.textArea.setText(info);
}
}
}

有問題歡迎提問,滿意請採納,謝謝!

熱點內容
優路教育伺服器連接不上怎麼回事 發布:2025-02-06 23:03:49 瀏覽:140
資料庫加速 發布:2025-02-06 23:02:14 瀏覽:564
蘋果ipodpro如何連接安卓手機 發布:2025-02-06 23:00:56 瀏覽:528
android格式化sd卡 發布:2025-02-06 23:00:50 瀏覽:981
郝斌資料庫 發布:2025-02-06 22:44:57 瀏覽:181
全息存儲器 發布:2025-02-06 22:43:51 瀏覽:116
游戲源碼如何使用 發布:2025-02-06 22:43:40 瀏覽:714
表與資料庫 發布:2025-02-06 22:42:47 瀏覽:439
典型宣傳短片拍攝腳本 發布:2025-02-06 22:33:27 瀏覽:551
php資料庫配置 發布:2025-02-06 22:29:38 瀏覽:17