Java GUI 綜合應用實例
其實是一次Java上機實驗,題目包含:按鈕,文本框、文本域、複選框、單選按鈕組、面板、彈窗JDialog,列表JList,下拉列表JComboBox,滾動條JScrollPane, 以及以上所有組件對應的常用監聽器方法的使用。碼這做備忘了。
題目: 1,2沒用我刪了,代碼中註釋與題號對應。
3.在 Frame 窗口中設計一個按鈕,按鈕顯示爲退出程序,單擊按鈕時,退出程序,編寫“退出程序”的事件處理代碼。
4.在 Frame 窗口中設計一個文本框和一個文本區域,文本框內容改變時,將文本框中的內容顯示在文本區域中;在文本框中按回車鍵時,清空文本區域的內容。
5.在 Frame 窗口中設計一個複選按鈕和一個普通按鈕,複選按鈕選中時,普通按鈕的背景色爲青色,未選中時爲灰色。
6.在 Frame 窗口中設計一個單選按鈕組和一個普通按鈕,單選按鈕組中包含三個單選,文本說明分別爲“普通”、“黑體”和“斜體”選擇文本標籤爲“普通”的單選按鈕時,普通按鈕中的文字爲普通字體,選擇文本標籤爲“黑體”的單選按鈕時,普通按鈕中的文字的字體爲黑體,選擇文本標籤爲“斜體”的單選按鈕時,普通按鈕中的文字的字體爲斜體。*
7.在 Frame 窗口中設計一個下拉列表和一個按鈕,下拉列表中有 10、14、18 三個選項,選擇 10 時,按鈕中文字的字號爲 10,選擇 14 時,按鈕中文字的字號爲 14,選擇18 時,按鈕中文字的字號爲 18。*
8.在 Frame 窗口中設計一個列表和兩個標籤,在第一個標籤中顯示列表中被雙擊的選項的內容,在第二個標籤中顯示列表中被選中的所有選項的內容。
9.在 Frame 窗口中設計一個標籤,編程確定當前鼠標的位置座標代碼,以(X1,Y1)、
(X2,Y2)的形式顯示在標籤中。
10.在 Frame 窗口中設計一按鈕,另外創建另一個 Frame,當單擊按鈕時,彈出另一個 Frame。
根據題目先自己設計出 UI佈局:
然後就Coding了
我把所有內容都寫到同一個類中,每題都新建類很麻煩,但是放到一起就要求代碼質量必須很高,各個部分在寫之前要做好規劃,減小耦合性。代碼有點長,但是結構和可讀性比較高,一個構造器,在構造器中創建窗口,調用初始化函數,在初始化函數中傳入面板參數,調用各題對應的方法。在各題對應的方法中用到的監聽器部分採用內部類,或匿名內部類編寫。main方法中new一個本類就ok了。
看一下效果吧:
和之前設計的UI相比:
基本實現了,上代碼吧,奧利奧,淦了兄弟們。
package akali.JavaClass;
import javafx.css.Size;
import javafx.scene.Group;
import javax.swing.*;
import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener;
import java.awt.*;
import java.awt.event.*;
/**
* @description: 第六次實驗 Java GUI
* @author: 孔垂禹
* @time: 2020/4/30 18:25
* @fromProject: Java_GUI
* @Version: V1.0
*/
class Experiment_06 extends Frame {
public static void main(String[] args) {
new Experiment_06();
}
//構造器
public Experiment_06(){
this.exitButton(); //3.添加退出按鈕
this.init(); //添加背景面板,初始化
//設置窗口基本佈局
this.setTitle("實驗六");
this.setBounds(400,200,650,400);
//窗體使用絕對定位
this.setLayout(null);
this.setVisible(true);
this.setResizable(false);
this.addWindowListener(new WindowAdapter() {
@Override
public void windowClosing(WindowEvent e) {
System.exit(0);
}
}); //設置窗口可關閉
}
//創建主面板並初始化
public void init(){
Panel panel0 = new Panel(); //主面板
panel0.setBounds(40,25,570,325);
panel0.setBackground(new Color(215, 215, 215));
// panel0.setLayout(new GridLayout(1,2,5,5));
panel0.setLayout(null);
this.add(panel0);
//分面板 NOTE:由於鼠標定位只能針對一個面板,所以將之前的分面板放棄
// Panel panel1 = new Panel(); //分面板1
// Panel panel2 = new Panel(); //分面板2
// panel1.setLayout(null);
// panel2.setLayout(null);
//***************************方法調用**********************************************
//向分面板中添加組件:通過傳參調方法來實現,這樣結構比較清楚 ,全部改成主面板
this.textBlank(panel0); //4.添加文本框文本域
this.btnColor(panel0); //5.添加複選框和按鈕
this.btnFont(panel0); //6.添加單選按鈕組
this.btnFontSize(panel0); //7.添加下拉列表
this.listLabels(panel0); //8.添加列表和標籤
this.mouseLocation(panel0);//9.添加鼠標座標監聽
this.myDialog(panel0); //10.添加彈窗
//*********************************************************************************
// panel0.add(panel1);
// panel0.add(panel2);
}
/*
* 3、在 Frame 窗口中設計一個按鈕,按鈕顯示爲退出程序,
* 單擊按鈕時,退出程序,編寫“退出程序”的事件處理代碼。
*/
public void exitButton(){
JButton exitBtn = new JButton("退出");
//將按鈕使用座標佈局
exitBtn.setLocation(520,360);
exitBtn.setSize(110,25);
//添加事件監聽器
exitBtn.addActionListener(actionEvent -> System.exit(0)); //Lambda表達式
this.add(exitBtn);
}
/*4.在 Frame 窗口中設計一個文本框和一個文本區域,文本框內容改變時,
*將文本框中的內容顯示在文本區域中;在文本框中按回車鍵時,清空文本區域的內容。
*/
public void textBlank(Panel panel){
//文本框佈局
JLabel label1 = new JLabel("文本框:");
label1.setLocation(12,23);
label1.setSize(50,20);
TextField textField = new TextField(14);
textField.setLocation(70,25);
textField.setSize(230,20);
//文本域佈局
JLabel label2 = new JLabel("文本域:");
label2.setLocation(12,55);
label2.setSize(50,20);
TextArea textArea = new TextArea(15,14);
/* textArea.setSize(230,200); */
JScrollPane scrollPane = new JScrollPane(textArea);
scrollPane.setSize(230,180);
scrollPane.setLocation(70,55);
//文本事件監聽
textField.addTextListener(textEvent -> { //Lambda表達式
TextField field = (TextField) textEvent.getSource();
String input = field.getText();
textArea.setText(input);
});
//鍵盤監聽
textField.addKeyListener(new KeyAdapter() {
@Override
public void keyPressed(KeyEvent e) {
super.keyPressed(e);
if (e.getKeyCode() == KeyEvent.VK_ENTER){
textArea.setText(" ");
}
}
});
panel.add(label1);
panel.add(textField);
panel.add(label2);
panel.add(scrollPane);
}
/*5.在 Frame 窗口中設計一個複選按鈕和一個普通按鈕,複選按鈕選中時,
*普通按鈕的背景色爲青色,未選中時爲灰色。
*/
public void btnColor(Panel panel){
JCheckBox checkBox = new JCheckBox("設置顏色");
checkBox.setBackground(new Color(215, 215, 215));
JButton colorBtn = new JButton("顏色按鈕");
checkBox.setBounds(12+296,23,100,20);
colorBtn.setBounds(130+296,15,120,36);
colorBtn.setBackground(new Color(176, 176, 176));
checkBox.addItemListener(new ItemListener() {
@Override
public void itemStateChanged(ItemEvent itemEvent) {
JCheckBox eventItem = (JCheckBox) itemEvent.getItem();
if (eventItem.isSelected()){ //如果被選中
colorBtn.setBackground(new Color(99, 175, 173));
}else {
colorBtn.setBackground(new Color(176, 176, 176));
}
}
});
panel.add(checkBox);
panel.add(colorBtn);
}
/* 6.在 Frame 窗口中設計一個單選按鈕組和一個普通按鈕,單選按鈕組中包含三個單選,
*文本說明分別爲“普通”、“黑體”和“斜體”選擇文本標籤爲“普通”的單選按鈕時,普通按鈕中的文字爲普通字體,
*選擇文本標籤爲“黑體”的單選按鈕時,普通按鈕中的文字的字體爲黑體,選擇文本標籤爲“斜體”的單選按鈕時,
* 普通按鈕中的文字的字體爲斜體。
*/
public void btnFont(Panel panel){
JLabel label3 = new JLabel("字體:");
JButton fontBtn = new JButton("字體按鈕");
final JRadioButton norFont = new JRadioButton("普通");
norFont.setBackground(new Color(215, 215, 215));
final JRadioButton borFont = new JRadioButton("黑體");
borFont.setBackground(new Color(215, 215, 215));
final JRadioButton iteFont = new JRadioButton("斜體");
iteFont.setBackground(new Color(215, 215, 215));
//添加按鈕組
ButtonGroup group = new ButtonGroup();
group.add(norFont);
group.add(borFont);
group.add(iteFont);
//佈局
label3.setBounds(12+296,55,50,20);
norFont.setBounds(12+296,80,50,17);
borFont.setBounds(12+296,100,50,17);
iteFont.setBounds(12+296,120,50,17);
fontBtn.setBounds(130+296,95,120,36);
//添加監聽器 ,爲了讓三個按鈕共用一個監聽器,這裏就不再直接new 監聽器,而是寫一個繼承ItemListener的內部類
class MyRadioListener implements ItemListener{
@Override
public void itemStateChanged(ItemEvent itemEvent) {
if (itemEvent.getSource() == norFont){
fontBtn.setFont(new Font("宋體",Font.PLAIN, 12));
}else if (itemEvent.getSource() == borFont){
fontBtn.setFont(new Font("宋體",Font.BOLD, 12));
}else if (itemEvent.getSource() == iteFont){
fontBtn.setFont(new Font("宋體",Font.ITALIC, 12));
}
}
}
MyRadioListener myRadioListener = new MyRadioListener();
norFont.addItemListener(myRadioListener);
borFont.addItemListener(myRadioListener);
iteFont.addItemListener(myRadioListener);
panel.add(label3);
panel.add(fontBtn);
panel.add(norFont);
panel.add(borFont);
panel.add(iteFont);
}
/*7.在 Frame 窗口中設計一個下拉列表和一個按鈕,下拉列表中有 10、14、18 三個選項,
*選擇 10 時,按鈕中文字的字號爲 10,選擇 14 時,按鈕中文字的字號爲 14,
*選擇18 時,按鈕中文字的字號爲 18。
*/
public void btnFontSize(Panel panel){
JLabel label4 = new JLabel("字號:");
JButton fontSizeBtn = new JButton("字號按鈕");
JComboBox<String> comboBox = new JComboBox<String>();
//下拉列表添加選項
comboBox.addItem(null);
comboBox.addItem("10");
comboBox.addItem("14");
comboBox.addItem("18");
//佈局
label4.setBounds(12+296,140,50,20);
comboBox.setBounds(12+296,165,80,20);
fontSizeBtn.setBounds(130+296,157,120,36);
//監聽
comboBox.addItemListener(new ItemListener() {
@Override
public void itemStateChanged(ItemEvent itemEvent) {
JComboBox eventSource = (JComboBox) itemEvent.getSource();
if (eventSource.getSelectedItem() == "10"){
fontSizeBtn.setFont(new Font("宋體",Font.PLAIN, 10));
}else if (eventSource.getSelectedItem() =="14"){
fontSizeBtn.setFont(new Font("宋體",Font.PLAIN, 14));
}else if (eventSource.getSelectedItem() =="18"){
fontSizeBtn.setFont(new Font("宋體",Font.PLAIN, 18));
}
}
});
//添加進面板
panel.add(label4);
panel.add(comboBox);
panel.add(fontSizeBtn);
}
/*8.在 Frame 窗口中設計一個列表和兩個標籤,在第一個標籤中顯示列表中被雙擊的選項的內容,
*在第二個標籤中顯示列表中被選中的所有選項的內容
*/
public void listLabels(Panel panel){
JLabel label5 = new JLabel("列 表:");
JLabel label6 = new JLabel();
JLabel label7 = new JLabel();
String[] strList = {"老師辛苦","助教師姐辛苦","orz","Java-GUI"};
JList list = new JList(strList);
JScrollPane scrollPane = new JScrollPane(list);
//佈局
label5.setBounds(12,240,50,20);
scrollPane.setBounds(70,245,100,60);
label6.setBounds(175,245,100,35);
label6.setOpaque(true);
label6.setBackground(new Color(244, 244, 244));
label7.setBounds(175,285,100,35);
label7.setOpaque(true);
label7.setBackground(new Color(244, 244, 244));
list.setSelectionMode(ListSelectionModel.MULTIPLE_INTERVAL_SELECTION); //設置爲可以多選
//監聽
list.addMouseListener(new MouseAdapter() { //雙擊監聽
@Override
public void mouseClicked(MouseEvent e) {
if (e.getClickCount() == 2){
JList item1 = (JList) e.getSource();
label6.setText(item1.getSelectedValue().toString());
}
}
});
list.addListSelectionListener(new ListSelectionListener() { //多選監聽
@Override
public void valueChanged(ListSelectionEvent listSelectionEvent) {
JList items = (JList) listSelectionEvent.getSource();
String strList = "<html><body><p>"; //換行
for (int i = 0; i < items.getSelectedValuesList().size(); i++) {
strList += items.getSelectedValuesList().get(i)+"<br>";
}
strList.substring(0,strList.length()-5);
strList += "</p></body></html>";
label7.setText(strList);
}
});
panel.add(label5);
panel.add(label6);
panel.add(label7);
panel.add(scrollPane);
}
/*9.在 Frame 窗口中設計一個標籤,編程確定當前鼠標的位置座標代碼,以(X1,Y1)、
*(X2,Y2)的形式顯示在標籤中。*
*/
public void mouseLocation(Panel panel){
JLabel label8 = new JLabel("鼠標座標:X:");
JLabel label9 = new JLabel("Y:");
TextField xField = new TextField();
TextField yField = new TextField();
//佈局
label8.setBounds(12+296,210,80,20);
xField.setBounds(100+296,210,40,20);
label9.setBounds(150+296,210,30,20);
yField.setBounds(180+296,210,40,20);
//監聽
panel.addMouseMotionListener(new MouseMotionAdapter() {
@Override
public void mouseMoved(MouseEvent e) {
super.mouseMoved(e);
xField.setText(""+e.getX());
yField.setText(""+e.getY());
}
});
panel.add(label8);
panel.add(label9);
panel.add(xField);
panel.add(yField);
}
/*10.在 Frame 窗口中設計一按鈕,另外創建另一個 Frame,當單擊按鈕時,彈出另一個 Frame。 */
public void myDialog(Panel panel){
JButton digBtn = new JButton("說明");
digBtn.setBounds(180+200,270,110,25);
digBtn.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent actionEvent) {
JDialog dialog = new JDialog();
// dialog.setDefaultCloseOperation(WindowConstants.DO_NOTHING_ON_CLOSE);
dialog.setVisible(true);
dialog.setLayout(new FlowLayout());
dialog.setBounds(420,370,190,130);
JLabel label = new JLabel("*<(¦3[▓▓] good night");
dialog.add(label);
}
});
panel.add(digBtn);
}
}