新聞中心
這里有您想知道的互聯(lián)網(wǎng)營(yíng)銷解決方案
JavaSwing編寫樣例-創(chuàng)新互聯(lián)
- 編寫滿足以下要求的GUI程序。
① 頂部?jī)蓚€(gè)文本框只接受大于0小于11的整數(shù)。
② 文本框數(shù)字改變時(shí),自動(dòng)刷新下部網(wǎng)格區(qū)域的按鈕。
③ 鼠標(biāo)進(jìn)入按鈕時(shí),在該按鈕上顯示“*”。
④ 鼠標(biāo)移出按鈕時(shí),隱藏該按鈕上的文字。
源碼:
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í)無(wú)法到達(dá),因?yàn)榍懊嬗蠧ustomLimitKeyInputListener監(jiān)聽器限制了輸入只能是數(shù)字,鎖有不會(huì)出現(xiàn)這個(gè)異常,沒(méi)刪掉因?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ù)器適合批量采購(gòu),新人活動(dòng)首月15元起,快前往官網(wǎng)查看詳情吧
名稱欄目:JavaSwing編寫樣例-創(chuàng)新互聯(lián)
標(biāo)題URL:http://www.dlmjj.cn/article/hpeho.html