源碼:
創(chuàng)新互聯(lián)是專業(yè)的尼金平網(wǎng)站建設(shè)公司,尼金平接單;提供成都網(wǎng)站設(shè)計(jì)、成都網(wǎng)站建設(shè),網(wǎng)頁設(shè)計(jì),網(wǎng)站設(shè)計(jì),建網(wǎng)站,PHP網(wǎng)站建設(shè)等專業(yè)做網(wǎng)站服務(wù);采用PHP框架,可快速的進(jìn)行尼金平網(wǎng)站開發(fā)網(wǎng)頁制作和功能擴(kuò)展;專業(yè)做搜索引擎喜愛的網(wǎng)站,專業(yè)的做網(wǎng)站團(tuán)隊(duì),希望更多企業(yè)前來合作!import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class GUIProgram {public static void main(String[] args) {new CustomJFrame();
}
}
class CustomJFrame extends JFrame {private JPanel root;
private JPanel northOfRoot;
private JPanel buttonsPanel;
private JTextField rowTextField;
private JTextField colTextField;
public CustomJFrame() {setTitle("Custom JFrame Test");
setSize(300, 400);
// setLayout(null);
init();
setVisible(true);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
void init() {root = new JPanel();
root.setLayout(new BorderLayout());
add(root);
northOfRoot = new JPanel();
northOfRoot.setLayout(new FlowLayout());
root.add(northOfRoot, BorderLayout.NORTH);
JLabel rowText = new JLabel("行數(shù): ");
northOfRoot.add(rowText);
rowTextField = new JTextField(5);
rowTextField.addActionListener(new CustomActionListener(e ->generate()));
rowTextField.addKeyListener(new CustomLimitKeyInputListener());
northOfRoot.add(rowTextField);
JLabel colText = new JLabel("列數(shù): ");
northOfRoot.add(colText);
colTextField = new JTextField(5);
colTextField.addActionListener(new CustomActionListener(e ->generate()));
colTextField.addKeyListener(new CustomLimitKeyInputListener());
northOfRoot.add(colTextField);
// JButton generateButton = new JButton("生成");
// generateButton.addActionListener(new CustomActionListener(e ->{//
// }));
// northOfRoot.add(generateButton);
}
void generate() {String _rowText = rowTextField.getText();
String _colText = colTextField.getText();
try { //將String轉(zhuǎn)化為int類型
int row = Integer.parseInt(_rowText);
int col = Integer.parseInt(_colText);
//判斷數(shù)字是否超出要求
row = isValidParams(row) ? row : 10;
col = isValidParams(col) ? col : 10;
rowTextField.setText(String.valueOf(row));
colTextField.setText(String.valueOf(col));
drawButtonPanel(row, col);
} catch (NumberFormatException n) { //這里其實(shí)無法到達(dá),因?yàn)榍懊嬗蠧ustomLimitKeyInputListener監(jiān)聽器限制了輸入只能是數(shù)字,鎖有不會(huì)出現(xiàn)這個(gè)異常,沒刪掉因?yàn)閼械脛h
JOptionPane.showMessageDialog(root, "輸入應(yīng)為0-11", "warn", JOptionPane.WARNING_MESSAGE);
}
}
boolean isValidParams (int param) {if (param<= 0 || param >= 11) {return false;
}
return true;
}
void drawButtonPanel(int row, int col) {if (buttonsPanel == null) {buttonsPanel = new JPanel();
root.add(buttonsPanel, BorderLayout.CENTER);
} else {buttonsPanel.removeAll();
}
buttonsPanel.setLayout(new GridLayout(row, col));
//渲染按鈕
for (int i = 0; i< row; i++) {for (int j = 0; j< col; j++) {JButton jButton = new JButton();
buttonsPanel.add(jButton);
jButton.addMouseListener(new MouseAdapter() {@Override
public void mouseEntered(MouseEvent e) {jButton.setText("*");
}
@Override
public void mouseExited(MouseEvent e) {jButton.setText("");
}
});
}
}
SwingUtilities.updateComponentTreeUI(buttonsPanel);
}
}
@FunctionalInterface
interface DealHandler {void deal(Object something);
}
class CustomActionListener implements ActionListener {private DealHandler dealHandler;
public CustomActionListener(DealHandler dealHandler) {this.dealHandler = dealHandler;
}
@Override
public void actionPerformed(ActionEvent e) {dealHandler.deal(e);
}
}
class CustomLimitKeyInputListener extends KeyAdapter {@Override
public void keyTyped(KeyEvent e) {if (!Character.isDigit(e.getKeyChar())) {//不是數(shù)字限制傳遞
e.consume();
}
}
}
你是否還在尋找穩(wěn)定的海外服務(wù)器提供商?創(chuàng)新互聯(lián)www.cdcxhl.cn海外機(jī)房具備T級(jí)流量清洗系統(tǒng)配攻擊溯源,準(zhǔn)確流量調(diào)度確保服務(wù)器高可用性,企業(yè)級(jí)服務(wù)器適合批量采購,新人活動(dòng)首月15元起,快前往官網(wǎng)查看詳情吧
網(wǎng)頁題目:JavaSwing編寫樣例-創(chuàng)新互聯(lián)
文章起源:http://m.newbst.com/article40/hpeho.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供搜索引擎優(yōu)化、自適應(yīng)網(wǎng)站、云服務(wù)器、網(wǎng)站維護(hù)、App開發(fā)、網(wǎng)站策劃
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來源: 創(chuàng)新互聯(lián)
猜你還喜歡下面的內(nèi)容