iT邦幫忙

2025 iThome 鐵人賽

DAY 15
0

今日內容:JOptionPane、TextField、JCheckBox


JOptionPane

跳出一個小視窗,用於提示使用者某些事項
不像JFrame或JPanel、他不需要建構出物件,並且可以將結果回傳給程式

import javax.swing.JOptionPane;

public class Main{
    public static void main(String[] args){
        // JOptionPane.showMessageDialog(Parent Component, 內容, 視窗標題, 視窗形式);
        JOptionPane.showMessageDialog(null, "text", "title", JOptionPane.PLAIN_MESSAGE);
        JOptionPane.showMessageDialog(null, "text", "title", JOptionPane.INFORMATION_MESSAGE);
        JOptionPane.showMessageDialog(null, "text", "title", JOptionPane.QUESTION_MESSAGE);
        JOptionPane.showMessageDialog(null, "text", "title", JOptionPane.WARNING_MESSAGE);
        JOptionPane.showMessageDialog(null, "text", "title", JOptionPane.ERROR_MESSAGE);
        
        int answer = JOptionPane.showConfirmDialog(null, "Bro do you even code?", "title", JOptionPane.YES_NO_CANCEL_OPTION);
        String name = JOptionPane.showInputDialog("What is your name? ");
        System.out.println("Hello " + name);
    }
}

image PLAIN_MESSAGE
image INFORMATION_MESSAGE
image QUESTION_MESSAGE
image WARNING_MESSAGE
image ERROR_MESSAGE
image YES_NO_CANCEL_OPTION
image showInputDialog

下面這個是比較完整的全部自訂的方式

JOptionPane.showOptionDialog(
    parent component, 內容, 標題, 選項, 形式,
    自訂圖示(會覆蓋形式), 自訂選項(會覆蓋選項), 預設選擇值);
// 自訂圖示用imageicon,自訂選項可以開String[]

JOptionPane.showOptionDialog(
    null, "You are awesome!", "title", JOptionPane.YES_NO_CANCEL_OPTION,
    JOptionPane.INFORMATION_MESSAGE, null, null, 0);

image
可以設置變數儲存使用者的選項


TextField

用來增加、接收或設置文字的組件

import javax.swing.JFrame;
import javax.swing.JTextField;
import javax.swing.JButton;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

public class MyFrame extends JFrame implements ActionListener{
    JButton button;
    JTextField textField;
    MyFrame(){
        this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        this.setLayout(new FlowLayout());

        button = new JButton("Submit");
        button.addActionListener(this);

        textField = new JTextField();
        textField.setPreferredSize(new Dimension(250, 40));
        textField.setFont(new Font("Consolas", Font.PLAIN, 35));
        textField.setForeground(Color.GREEN);
        textField.setBackground(Color.black);
        textField.setCaretColor(Color.white);
        textField.setText("username");

        this.add(button);
        this.add(textField);
        this.pack();
        this.setVisible(true);
    }
    
    @Override
    public void actionPerformed(ActionEvent e){
        if(e.getSource() == button){
            System.out.println("Welcome, " + textField.getText());
            button.setEnabled(false);
            textField.setEditable(false);
        }
    }
}

image
image
image


JCheckbox

就是一個可以被勾選的格子

import javax.swing.JFrame;
import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JCheckBox;
import javax.swing.JButton;
import javax.swing.ImageIcon;

public class MyFrame extends JFrame implements ActionListener{
    JButton button;
    JCheckBox checkBox;
    ImageIcon xIcon;
    ImageIcon checkIcon;

    MyFrame(){
        this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        this.setLayout(new FlowLayout());

        xIcon = new ImageIcon("x.png");
        checkIcon = new ImageIcon("check.png");

        button = new JButton();
        button.setText("Submit");
        button.addActionListener(this);

        checkBox = new JCheckBox();
        checkBox.setText("I'm not a robot");
        checkBox.setFocusable(false);
        checkBox.setFont(new Font("Consolas", Font.PLAIN, 35));
        checkBox.setIcon(xIcon); // 未點擊時
        checkBox.setSelectedIcon(checkIcon); // 點擊後

        this.add(button);
        this.add(checkBox);
        this.pack();
        this.setVisible(true);
    }
    @Override
    public void actionPerformed(ActionEvent e){
        if(e.getSource() == button){
            System.out.println(checkBox.isSelected()); // 在terminal回報點擊狀況(確認用)
        }
    }
}

image 點擊前
image 點擊後


結語

今天是接觸Java GUI的第四天,也是進行挑戰的一半了,目前預計大約在挑戰第20天時開始製作貪食蛇遊戲本體,如果能夠正常完成的話,後續會對他做優化。而最後如果都已經完美完成了,那也許可以用所學到的東西繼續製作其他小遊戲!
今天也是快樂學習的一天,明天繼續!/images/emoticon/emoticon18.gif


上一篇
Day 14:Java GUI (三)
下一篇
Day 16:Java GUI (五)
系列文
30天從基礎學起Java,直到做出我的第一個遊戲20
圖片
  熱門推薦
圖片
{{ item.channelVendor }} | {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中

尚未有邦友留言

立即登入留言