package?jisuanqi_new;
成都創(chuàng)新互聯(lián)公司是一家業(yè)務(wù)范圍包括IDC托管業(yè)務(wù),虛擬主機(jī)、主機(jī)租用、主機(jī)托管,四川、重慶、廣東電信服務(wù)器租用,資陽服務(wù)器托管,成都網(wǎng)通服務(wù)器托管,成都服務(wù)器租用,業(yè)務(wù)范圍遍及中國大陸、港澳臺以及歐美等多個(gè)國家及地區(qū)的互聯(lián)網(wǎng)數(shù)據(jù)服務(wù)公司。
import?java.awt.*;
import?java.awt.event.*;
public?class?JiSuanQi_new?implements?ActionListener
{
Panel?p1;//聲明面板p1
TextField?t1;//聲明文本行t1
String[]?label?=?{"7","8","9","/","4","5","6","*","1","2","3","-","0",".","=","+"};//聲明標(biāo)簽數(shù)組label1存放按鈕上的標(biāo)簽
Button[]?b;//聲明按鈕數(shù)組存放16個(gè)按鈕
private?int?i;//聲明i以備后用
private?String?op1?=?"0";//運(yùn)算數(shù)備用
private?String?operator?=?"+";//運(yùn)算符備用
private?boolean?append?=?false;//備用
public?JiSuanQi_new()//構(gòu)造方法
{
t1=new?TextField();//初始化文本行t1
b?=?new?Button[label.length];//初始化按鈕數(shù)組b
p1=new?Panel();//初始化面板p1
p1.setLayout(new?GridLayout(4,4,4,4));//使面板選擇網(wǎng)格布局管理器以備儲存16個(gè)按鈕(4行4列)
for(int?i=0;ib.length;i++)//利用for循環(huán)把標(biāo)簽放在按鈕上,使每個(gè)按鈕添加事件監(jiān)聽器,在面板p1上添加上16個(gè)按鈕
{
b[i]?=?new?Button(label[i]);//把標(biāo)簽依次放在16個(gè)按鈕上
b[i].addActionListener(this);//使每個(gè)按鈕添加動(dòng)作事件監(jiān)聽器
p1.add(b[i]);?//分別將按鈕添加到面板p1上
}
Frame?f=new?Frame("計(jì)算器1.0");//初始化窗口f,起名字計(jì)算器1.0
f.setLayout(new?BorderLayout());//為窗口選擇邊界布局管理器
f.add(BorderLayout.NORTH,t1);//把文本行他添加到窗口的北部
f.add(BorderLayout.CENTER,p1);//把面吧p1添加到窗口的中間
f.addWindowListener(new?WindowAdapter(){//給窗口f添加窗口事件監(jiān)聽器
public?void?windowClosing(WindowEvent?eve){//運(yùn)行窗口關(guān)閉方法
System.exit(0);//退出程序
}
});
f.setSize(250,?250);//設(shè)置窗口大小
f.setLocation(200,200);
f.setVisible(true);//顯示窗口
}
public?static?void?main(String?args[])
{
new?JiSuanQi_new();?//調(diào)用構(gòu)造方法
}
public?void?actionPerformed(ActionEvent?ae)
{//按鈕被操作發(fā)生
String?comm?=?ae.getActionCommand();//返回與此動(dòng)作相關(guān)的命令字符串,即:使用者第一次點(diǎn)擊的按鈕是什么。
if("0123456789".indexOf(comm)!=-1)//如果相關(guān)命令字符串為0~9之間的數(shù)字則執(zhí)行
{
if(append){
String?temp?=?t1.getText();//新數(shù)字
t1.setText(temp+comm);
}else{?????????????????????????//因?yàn)榇藭r(shí)append為false執(zhí)行這個(gè)
t1.setText(comm);?//將文本行t1設(shè)置為相關(guān)命令字符串(你按中的按鈕代碼)
append?=??true;//此時(shí)append=true若繼續(xù)按按鈕若繼續(xù)按數(shù)字的話1.第一次的按話不會改變2.非第一次按得話會覆蓋之前按得數(shù)字(即缺點(diǎn):只能進(jìn)行單個(gè)數(shù)字的計(jì)算)
}
}
else?if(("+-*/".indexOf(comm)!=-1))//如果相關(guān)命令字符串為+-*/之間的數(shù)字則執(zhí)行
{
//保存
//t1.setText(comm);
op1?=?t1.getText();//把第一個(gè)數(shù)賦值給op1
operator?=?comm;//把命令字符串賦值給operator
append?=?false;//此時(shí)append為false即若繼續(xù)按按鈕若按數(shù)字的話將重復(fù)上面的動(dòng)作,按符號的話將覆蓋之前的符號
}
else?if("=".equals(comm))//如果按的是=號,則按條件進(jìn)行下面的運(yùn)算
{
String?op2?=?t1.getText();//op2第二個(gè)數(shù)
double?d1?=?Double.parseDouble(op1);
double?d2?=?Double.parseDouble(op2);
if(operator.equals("+")){
d1?=?d1?+?d2?;
}else?if(operator.equals("-")){
d1?=?d1?-?d2;
}else?if(operator.equals("*")){
d1?=?d1?*?d2;
}else?{
d1?=?d1?/?d2;
}
t1.setText(d1+"");//顯示計(jì)算結(jié)果
append?=?false;
}
else?if(".".equals(comm))//若是.號繼續(xù)按
{
String?temp?=?t1.getText();
if(temp.indexOf(".")==-1){
t1.setText(temp+".");
append?=?true;
}
}
}
}
首先:采用什么技術(shù)實(shí)現(xiàn)
java語言可以使用awt 和swing等技術(shù)實(shí)現(xiàn)圖形界面
推薦使用Swing,因?yàn)镾wing比AWT更專業(yè),更漂亮,組件更豐富,功能更強(qiáng)大。
2. ??其次:分析采用什么布局
邊界布局BorderLayout,配合表格布局GridLayout,既簡單又美觀
3. ??最后:分析需求中需要用的組件
學(xué)生姓名 學(xué)號 ?顯示信息 需要用到文本框JTextField
單選按鈕 需要用到組件?JRadioButton
復(fù)選框 ? ?需要用到組件?JCheckBox
組合框 ? ?需要用到組件?JComboBox
圖片效果
參考代碼如下
//導(dǎo)入所需要的包
import?java.awt.event.*;
import?javax.swing.border.*;
import?javax.swing.*;
import?java.awt.*;
public?class?ClassFrame?extends?JFrame?{//?寫一個(gè)類繼承自JFrame?窗體
//?定義組件
private?static?final?long?serialVersionUID?=?1L;
private?JPanel?contentPane;
private?JTextField?tfName,?tfNum,?allInfo;
private?JRadioButton?rb1,?rb2;
private?JCheckBox?cb1,?cb2,?cb3;
private?JComboBoxString?t1,?t2,?t3;
public?static?void?main(String[]?args)?{
EventQueue.invokeLater(new?Runnable()?{
public?void?run()?{
try?{
ClassFrame?frame?=?new?ClassFrame();//?創(chuàng)建一個(gè)窗口實(shí)例
frame.setVisible(true);//?讓該窗口實(shí)例可見
}?catch?(Exception?e)?{
e.printStackTrace();
}
}
});
}
/**
?*?窗口屬性的設(shè)置,內(nèi)部組件的初始化
?*/
public?ClassFrame()?{
setTitle("選課ing...");//標(biāo)題
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);//?設(shè)置關(guān)閉是退出JVM
setSize(450,?339);//?設(shè)置窗體大小
setLocationRelativeTo(null);//?窗體居中
contentPane?=?new?JPanel();//?內(nèi)容面板
contentPane.setBorder(new?EmptyBorder(5,?5,?5,?5));
contentPane.setLayout(new?BorderLayout(0,?0));//?設(shè)置布局
setContentPane(contentPane);
JPanel?panel?=?new?JPanel(new?GridLayout(5,?1,?5,?10));//5行1列的表格布局
panel.setBorder(new?TitledBorder(null,?"",?TitledBorder.LEADING,?TitledBorder.TOP,?null,?null));
contentPane.add(panel,?BorderLayout.CENTER);//給panel添加邊框
JPanel?panel_1?=?new?JPanel();
panel.add(panel_1);
JLabel?label?=?new?JLabel("姓名");
panel_1.add(label);
tfName?=?new?JTextField();
panel_1.add(tfName);
tfName.setColumns(10);
JLabel?label_2?=?new?JLabel("學(xué)號");
panel_1.add(label_2);
tfNum?=?new?JTextField();
tfNum.setColumns(10);
panel_1.add(tfNum);
rb1?=?new?JRadioButton("男");
panel_1.add(rb1);
rb1.setSelected(true);//設(shè)置單選按鈕中,默認(rèn)選擇的按鈕
rb2?=?new?JRadioButton("女");
panel_1.add(rb2);
ButtonGroup?bts?=?new?ButtonGroup();//單選按鈕需要加入同一個(gè)ButonGroup中才能生效
bts.add(rb1);
bts.add(rb2);
JPanel?panel_2?=?new?JPanel();
panel.add(panel_2);
cb1?=?new?JCheckBox("高等數(shù)學(xué)");
panel_2.add(cb1);
t1?=?new?JComboBoxString();
t1.setModel(new?DefaultComboBoxModelString(new?String[]?{?"林老師",?"趙老師",?"孫老師"?}));
panel_2.add(t1);
JPanel?panel_3?=?new?JPanel();
panel.add(panel_3);
cb2?=?new?JCheckBox("世界經(jīng)濟(jì)");
panel_3.add(cb2);
t2?=?new?JComboBoxString();
t2.setModel(new?DefaultComboBoxModelString(new?String[]?{?"張老師",?"劉老師"?}));
panel_3.add(t2);
JPanel?panel_4?=?new?JPanel();
panel.add(panel_4);
cb3?=?new?JCheckBox("音樂賞析");
panel_4.add(cb3);
t3?=?new?JComboBoxString();
t3.setModel(new?DefaultComboBoxModelString(new?String[]?{?"王老師",?"周老師"?}));
panel_4.add(t3);
JPanel?panel_5?=?new?JPanel();
panel.add(panel_5);
JButton?jbOk?=?new?JButton("確定");
panel_5.add(jbOk);
JButton?jbRest?=?new?JButton("重填");
panel_5.add(jbRest);
JPanel?panelSouth?=?new?JPanel();
contentPane.add(panelSouth,?BorderLayout.SOUTH);
JLabel?labe?=?new?JLabel("選課信息");
labe.setHorizontalAlignment(SwingConstants.LEFT);
panelSouth.add(labe);
allInfo?=?new?JTextField();
allInfo.setColumns(30);
panelSouth.add(allInfo);
JPanel?panelNorth?=?new?JPanel();
contentPane.add(panelNorth,?BorderLayout.NORTH);
JLabel?labelTitle?=?new?JLabel("學(xué)生選課界面");
labelTitle.setForeground(Color.DARK_GRAY);
labelTitle.setFont(new?Font("宋體",?Font.BOLD,?20));
panelNorth.add(labelTitle);
//給確定按鈕添加事件處理代碼
jbOk.addActionListener(new?ActionListener()?{
public?void?actionPerformed(ActionEvent?e)?{
StringBuilder?info?=?new?StringBuilder();
String?name?=?tfName.getText();
String?num?=?tfNum.getText();
String?sex;
if?(rb1.isSelected())?{
sex?=?"男";
}?else?{
sex?=?"女";
}
info.append(name?+?num?+?sex);
if?(cb1.isSelected())?{
String?c?=?cb1.getText();
String?t?=?t1.getSelectedItem().toString();
info.append("?"?+?c?+?t);
}
if?(cb2.isSelected())?{
String?c?=?cb2.getText();
String?t?=?t2.getSelectedItem().toString();
info.append("?"?+?c?+?t);
}
if?(cb3.isSelected())?{
String?c?=?cb3.getText();
String?t?=?t3.getSelectedItem().toString();
info.append("?"?+?c?+?t);
}
allInfo.setText(info.toString());//把學(xué)生信息和選課信息放到文本框
}
});
//給重填按鈕?設(shè)置事件處理代碼
jbRest.addActionListener(new?ActionListener()?{
public?void?actionPerformed(ActionEvent?e)?{
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("");
}
});
}
}
package?cn.com.baiDu;
import?javax.swing.JFrame;
import?javax.swing.JPanel;
import?javax.swing.JTabbedPane;
public?class?TestPanel?extends?JFrame{
JTabbedPane?container;
public?TestPanel?(){
super("TestPanel");
init();
}
/**
*?界面組件初始化
*/
private?void?init(){
//定義界面大小
this.setSize(250,?250);
//定義界面出現(xiàn)在顯示器中間
this.setLocationRelativeTo(null);
//定義界面右上角的x符號退出
this.setDefaultCloseOperation(this.EXIT_ON_CLOSE);
//定義界面大小不可變
this.setResizable(false);
this.setName("Analyse");
//添加界面組件
setContainer();
//添加組件監(jiān)聽器
//??addListener();
//定義界面可見,放測試方法里面了
//??this.setVisible(true);
}
private?void?setContainer()?{
//實(shí)例切換面板
container?=?new?JTabbedPane();
//具體顯示面板1
JPanel?container1?=?new?JPanel();
//具體顯示面板2
JPanel?container2?=?new?JPanel();
//添加顯示面板到切換面板
container.addTab("面板1",?container1);
container.addTab("面板2",?container2);
//添加切換面板到界面
this.add(container);
}
/**
*?@param?args
*/
public?static?void?main(String[]?args)?{
TestPanel?test?=?new?TestPanel();
test.setVisible(true);
}
}
import?java.awt.Color;
import?java.awt.Dimension;
import?java.awt.GridBagConstraints;
import?java.awt.GridBagLayout;
import?java.awt.Insets;
import?java.awt.event.ActionEvent;
import?java.awt.event.ActionListener;
import?javax.swing.JButton;
import?javax.swing.JFrame;
import?javax.swing.JPanel;
import?javax.swing.JTextField;
public?class?Calculator2?extends?JPanel{
String[][]?lables?=?{
{"CE","C","Back","%"},
{"7","8","9","/"},
{"4","5","6","*"},
{"1","2","3","+"},
{"0",".","=","-"}
};
JTextField?display;
String?commands?=?"CEBack%/*+-=";
private?double?result;
private?String?lastCommand;
private?boolean?start;
public?static?void?main(String[]?args)?{
JFrame?f?=?new?JFrame("Test");
f.setContentPane(new?Calculator2());
f.pack();
f.setVisible(true);
f.setDefaultCloseOperation(3);
}
public?Calculator2(){
result?=?0;
lastCommand?=?"=";
start?=?true;
GridBagLayout?layout?=?new?GridBagLayout();
GridBagConstraints?gbc?=?new?GridBagConstraints();
this.setLayout(layout);
gbc.insets?=?new?Insets(1,?1,?1,?1);
gbc.gridheight?=?1;
gbc.fill?=?GridBagConstraints.BOTH;
Dimension?dimension?=?new?Dimension(70,?30);
gbc.gridwidth?=?4;
display?=?new?JTextField();
display.setPreferredSize(dimension);
display.setHorizontalAlignment(JTextField.RIGHT);
this.add(display,?gbc);
ActionListener?insert?=?new?InsertAction();
gbc.gridwidth?=?1;
for?(int?i?=?0;?i??lables.length;?i++)?{
for?(int?j?=?0;?j??lables[0].length;?j++)?{
gbc.gridx?=?j;
gbc.gridy?=?i+1;
JButton?b?=?new?JButton(lables[i][j]);
if?(j==3)
b.setForeground(Color.RED);
b.setPreferredSize(dimension);
b.addActionListener(insert);
this.add(b,gbc);
}
}
}
private?class?InsertAction?implements?ActionListener{
@Override
public?void?actionPerformed(ActionEvent?e)?{
String?input?=?e.getActionCommand();
if(commands.contains(input)){
if(start){
if(input.equals("-")){
display.setText(input);
start?=false;
}
}?else?{
calculate(Double.parseDouble(display.getText()));
lastCommand?=?input;
start?=?true;
}
}?else?{
if(start){
display.setText("");
start?=?false;
}
display.setText(display.getText()?+?input);
}
}
}
private?void?calculate(double?x)?{
if?(lastCommand.equals("+"))?result?+=x;
else?if?(lastCommand.equals("-"))?result?-=x;
else?if?(lastCommand.equals("*"))?result?*=x;
else?if?(lastCommand.equals("/"))?result?/=x;
else?if?(lastCommand.equals("="))?result?=x;
display.setText(""?+?result);
}
}