java圖形化界面設計
❶ 用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編程 設計一個圖形用戶界面。界麵包括三個單選按鈕、兩個復選框、一個列表、一個文本區和一個按
程序如下:
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);
}
}
}
有問題歡迎提問,滿意請採納,謝謝!