⑴ 用java語言設計一個界面,
首先:採用什麼技術實現
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圖形界面設計
importjava.awt.Color;
importjava.awt.Font;
importjava.awt.Graphics;
importjavax.swing.JFrame;
importjavax.swing.JPanel;
{
publicTestFrame(){
add(newCirclePanel());
setSize(300,230);
setLocationRelativeTo(null);
setDefaultCloseOperation(EXIT_ON_CLOSE);
} publicstaticvoidmain(String[]args){
newTestFrame().setVisible(true);
}
//繪制圖形的面板
classCirclePanelextendsJPanel{
intR=50;//直徑
@Override
protectedvoidpaintComponent(Graphicsg){
super.paintComponent(g);
g.setColor(Color.BLUE);//設置顏色為藍色
g.drawOval(80,30,R,R);//圓形外接矩形的左頂點坐標是80,30;
g.setColor(Color.BLACK);
g.drawOval(120,30,R,R);
g.setColor(Color.RED);
g.drawOval(160,30,R,R);
g.setColor(Color.YELLOW);
g.drawOval(100,65,R,R);
g.setColor(Color.GREEN);
g.drawOval(140,65,R,R);
g.setColor(Color.BLUE);//設置顏色為藍色
g.setFont(newFont("宋體",Font.BOLD,22));//設置字體
g.drawString("奧運五環旗",90,160);
}
}
}
⑶ 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編寫一個簡單用戶登陸界面
什麼都不說了 直接給你代碼吧x0dx0apackage com.moliying.ui;x0dx0aimport java.awt.BorderLayout;x0dx0aimport java.awt.Container;x0dx0aimport java.awt.FlowLayout;x0dx0aimport java.awt.List;x0dx0aimport java.awt.event.ActionEvent;x0dx0aimport java.awt.event.ActionListener;x0dx0aimport java.io.BufferedWriter;x0dx0aimport java.io.FileOutputStream;x0dx0aimport java.io.OutputStreamWriter;x0dx0aimport java.util.ArrayList;x0dx0aimport java.util.Arrays;x0dx0aimport javax.swing.JButton;x0dx0aimport javax.swing.JFrame;x0dx0aimport javax.swing.JLabel;x0dx0aimport javax.swing.JPanel;x0dx0aimport javax.swing.JPasswordField;x0dx0aimport javax.swing.JTextField;x0dx0apublic class Login {x0dx0aprivate JFrame frame = new JFrame("登錄");x0dx0aprivate Container c = frame.getContentPane();x0dx0aprivate JTextField username = new JTextField();x0dx0aprivate JPasswordField password = new JPasswordField();x0dx0aprivate JButton ok = new JButton("確定");x0dx0aprivate JButton cancel = new JButton("取消");x0dx0apublic Login() {x0dx0aframe.setSize(300, 200);x0dx0aframe.setBounds(450, 300, 300, 200);x0dx0ac.setLayout(new BorderLayout());x0dx0ainitFrame();x0dx0aframe.setVisible(true);x0dx0a}x0dx0aprivate void initFrame() {x0dx0a// 頂部x0dx0aJPanel titlePanel = new JPanel();x0dx0atitlePanel.setLayout(new FlowLayout());x0dx0atitlePanel.add(new JLabel("系統管理員登錄"));x0dx0ac.add(titlePanel, "North");x0dx0a// 中部表單x0dx0aJPanel fieldPanel = new JPanel();x0dx0afieldPanel.setLayout(null);x0dx0aJLabel a1 = new JLabel("用戶名:");x0dx0aa1.setBounds(50, 20, 50, 20);x0dx0aJLabel a2 = new JLabel("密 碼:");x0dx0aa2.setBounds(50, 60, 50, 20);x0dx0afieldPanel.add(a1);x0dx0afieldPanel.add(a2);x0dx0ausername.setBounds(110, 20, 120, 20);x0dx0apassword.setBounds(110, 60, 120, 20);x0dx0afieldPanel.add(username);x0dx0afieldPanel.add(password);x0dx0ac.add(fieldPanel, "Center");x0dx0a// 底部按鈕x0dx0aJPanel buttonPanel = new JPanel();x0dx0abuttonPanel.setLayout(new FlowLayout());x0dx0abuttonPanel.add(ok);x0dx0abuttonPanel.add(cancel);x0dx0ac.add(buttonPanel, "South");x0dx0ax0dx0aok.addActionListener(new ActionListener() {x0dx0ax0dx0a@Overridex0dx0apublic void actionPerformed(ActionEvent e) {x0dx0aSystem.out.println(username.getText().toString());x0dx0a}x0dx0a});x0dx0ax0dx0acancel.addActionListener(new ActionListener() {x0dx0ax0dx0a@Overridex0dx0apublic void actionPerformed(ActionEvent e) {x0dx0aframe.setVisible(false);x0dx0a}x0dx0a});x0dx0a}x0dx0apublic static void main(String[] args) {x0dx0a//new Login();x0dx0ax0dx0aString ss = "abbabbbaabbbccba";x0dx0ax0dx0aSystem.out.println(ss.split("b").length);x0dx0ax0dx0a}x0dx0a}
⑸ java怎麼做swing界面
swing做界面,一般需要一種基本知識:
一:關於布局的知識,常用的布局要熟悉,比如邊界布局,流式布局,絕對布局,網格布局等. 布局決定了組件(按鈕,文本框,下拉框等)所在的位置
二:關於事件響應機制,比如點擊按鈕事件,如何響應.
三:一些常用的組件,按鈕, 文本框, 下拉框,復選按鈕, 單選按鈕, 等
舉例: 簡單的代碼,實現給窗口隨機更換背景色
importjava.awt.*;
importjava.awt.event.*;
importjavax.swing.*;
{
publicDemoFrame(){
JButtonjb1=newJButton("點擊換背景色");//定義按鈕組件
jb1.addActionListener(this);//給按鈕添加事件響應機制,按鈕點擊時執行actionPerformed方法
add(jb1);//把組件添加到窗口
setLayout(newFlowLayout());//流式布局
setTitle("Color");//窗口標題
setSize(300,200);//大小
setLocationRelativeTo(null);//居中
setDefaultCloseOperation(EXIT_ON_CLOSE);//點擊關閉時退出系統
setVisible(true);//窗口可見
}
publicstaticvoidmain(String[]args){//main方法,啟動窗口
newDemoFrame();//創建窗口
}
publicvoidactionPerformed(ActionEvente){//當按鈕被點擊時,執行下面的代碼
//隨機顏色,並設置成為窗口的背景色
getContentPane().setBackground(newColor((int)(Math.random()*256),(int)(Math.random()*256),(int)(Math.random()*256)));
}
}
⑹ 用java程序編寫一個簡單的登錄界面怎麼寫
程序如下:
mport java.awt.HeadlessException;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.ImageIcon;
import javax.swing.JButton;
@SuppressWarnings("serial")
public class MainFrame extends JFrame {
JLabel lbl1 = new JLabel("用戶名:");
JLabel lbl2 = new JLabel("密 碼:");
JTextField txt = new JTextField("admin",20);
JPasswordField pwd = new JPasswordField(20);
JButton btn = new JButton("登錄");
JPanel pnl = new JPanel();
private int error = 0;
public MainFrame(String title) throws HeadlessException {
super(title);
init();
}
private void init() {
this.setResizable(false);
pwd.setEchoChar('*');
pnl.add(lbl1);
pnl.add(txt);
btn.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
if ("admin".equal花憨羔窖薏忌割媳公顱s(new String(pwd.getPassword()))){
pnl.removeAll();
JLabel lbl3 = new JLabel();
ImageIcon icon = new ImageIcon(this.getClass().getResource("pic.jpg"));
lbl3.setIcon(icon);
pnl.add(lbl3);
}
else{
if(error < 3){
JOptionPane.showMessageDialog(null,"密碼輸入錯誤,請再試一次");
error++;
}
else{
JOptionPane.showMessageDialog(null,"對不起,您不是合法用戶");
txt.setEnabled(false);
pwd.setEnabled(false);
btn.setEnabled(false);
}
}
}
});
}
public static void main(String[] args) {
MainFrame frm = new MainFrame("測試");
frm.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frm.setBounds(100, 100, 300, 120);
frm.setVisible(true);
}
}