當前位置:首頁 » 編程語言 » java圖形界面布局

java圖形界面布局

發布時間: 2025-03-20 19:56:39

1. 用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("");
}
});
}
}

2. java swing界面設計

GUI圖形界面設計的重點是布局

SWING也是採用AWT的布局方式,進行布局管理的。(實現LayoutManager介面的方法,來進行管理布局,API中已有實現類,我們通常只需要指定實現類,而不需要自己重寫方法)


常用的布局有絕對布局, 邊界布局BorderLayout,流布局FlowLayout,表格布局GridLayout。

JFrame等重量級組件,默認布局是邊界布局,JPanel輕量級組件,默認布局是流布局


  • 絕對布局:布局的特點,需要指定每個組件的大小,和具體位置。

  1. 優點:充分的自定義,充分的自由,可以寫出漂亮的 ,細致的界面

  2. 缺點:絕對布局在不同的操作系統下,會有一些不同程度的變化,導致界面變形,甚至組件重疊等。在同一操作系統下,窗口放大縮小,界面也會變形

  3. 絕對布局的範例


    3. java圖形界面上如何居中對齊

    直接修改panel中控制項的對齊方式,例如
    jCheckBox1.setHorizontalAlignment(SwingConstants.CENTER);

    4. java里邊圖形界面編程

    修改成這樣:
    import java.awt.*;
    import javax.swing.*;
    public class test2 extends JFrame
    {
    public static void main(String[] args)
    {
    test2 a = new test2();
    }

    public test2()
    {
    JButton a1 = new JButton("東");
    JButton a2 = new JButton("西");
    JButton a3 = new JButton("南");
    JButton a4 = new JButton("北");
    JButton a5 = new JButton("中");
    this.add(a1, BorderLayout.EAST);
    this.add(a2, BorderLayout.WEST);
    this.add(a3, BorderLayout.SOUTH);
    this.add(a4, BorderLayout.NORTH);
    this.add(a5, BorderLayout.CENTER);
    this.setTitle("邊界布局BorderLayout");
    this.setSize(500, 400);
    this.setLocation(800, 600);
    this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    this.setVisible(true);
    }
    }

熱點內容
視頻伺服器搭建海康 發布:2025-04-30 23:50:29 瀏覽:90
xp共享怎麼設置密碼 發布:2025-04-30 23:50:24 瀏覽:155
租用電腦主機搭建伺服器 發布:2025-04-30 23:28:06 瀏覽:729
php子類調用父類方法 發布:2025-04-30 23:11:55 瀏覽:673
存檔加密id 發布:2025-04-30 22:20:50 瀏覽:741
mac搭建php環境 發布:2025-04-30 21:58:13 瀏覽:134
雅迪電動車配置有哪些 發布:2025-04-30 21:38:27 瀏覽:154
為什麼用文件存儲取代mysql 發布:2025-04-30 21:17:26 瀏覽:609
我的世界免費伺服器ip 發布:2025-04-30 20:41:26 瀏覽:772
華為雲相冊在哪裡找安卓11 發布:2025-04-30 20:19:59 瀏覽:271