重庆分公司,新征程启航
为企业提供网站建设、域名注册、服务器等服务
源码:
创新互联是专业的尼金平网站建设公司,尼金平接单;提供成都网站设计、成都网站建设,网页设计,网站设计,建网站,PHP网站建设等专业做网站服务;采用PHP框架,可快速的进行尼金平网站开发网页制作和功能扩展;专业做搜索引擎喜爱的网站,专业的做网站团队,希望更多企业前来合作!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("行数: ");
northOfRoot.add(rowText);
rowTextField = new JTextField(5);
rowTextField.addActionListener(new CustomActionListener(e ->generate()));
rowTextField.addKeyListener(new CustomLimitKeyInputListener());
northOfRoot.add(rowTextField);
JLabel colText = new JLabel("列数: ");
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转化为int类型
int row = Integer.parseInt(_rowText);
int col = Integer.parseInt(_colText);
//判断数字是否超出要求
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) { //这里其实无法到达,因为前面有CustomLimitKeyInputListener监听器限制了输入只能是数字,锁有不会出现这个异常,没删掉因为懒得删
JOptionPane.showMessageDialog(root, "输入应为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())) {//不是数字限制传递
e.consume();
}
}
}
你是否还在寻找稳定的海外服务器提供商?创新互联www.cdcxhl.cn海外机房具备T级流量清洗系统配攻击溯源,准确流量调度确保服务器高可用性,企业级服务器适合批量采购,新人活动首月15元起,快前往官网查看详情吧