Java JButton子类初始化到JPanel的左上角
我有一个按钮矩阵,它是JButton的一个子类,都在JFrame中JPanel的GridLayout中。我最初在每个按钮上使用了一个空布局和设置框,但在阅读了一些建议空布局可能导致问题的帖子后,我改为GridLayout(在下面粘贴的代码中,注释掉了空布局相关代码)。这两种布局都会出现此问题。当使用标准JButton而不是我的子类时,不会出现问题。请让我知道我可以提供的其他信息,我可能遗漏了。我只是在学习Swing,所以我肯定我可能错过了一些明显的东西,但在谷歌搜索YouTube寻找解决方案后,我迷失了方向。谢谢 JFrame类:Java JButton子类初始化到JPanel的左上角,java,swing,jbutton,Java,Swing,Jbutton,我有一个按钮矩阵,它是JButton的一个子类,都在JFrame中JPanel的GridLayout中。我最初在每个按钮上使用了一个空布局和设置框,但在阅读了一些建议空布局可能导致问题的帖子后,我改为GridLayout(在下面粘贴的代码中,注释掉了空布局相关代码)。这两种布局都会出现此问题。当使用标准JButton而不是我的子类时,不会出现问题。请让我知道我可以提供的其他信息,我可能遗漏了。我只是在学习Swing,所以我肯定我可能错过了一些明显的东西,但在谷歌搜索YouTube寻找解决方案后,
package com.company;
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
public class swingUI extends JFrame implements ActionListener {
public static void main(String[] args) {
new swingUI();
}
public swingUI(){
this.setSize(400,400);
//this.setLocationRelativeTo(null);
Toolkit tk = Toolkit.getDefaultToolkit();
Dimension dim = tk.getScreenSize();
int xPos = (dim.width / 2) - (this.getWidth() / 2);
int yPos = (dim.height / 2) - (this.getHeight() / 2);
this.setLocation(xPos, yPos);
this.setResizable(true);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setTitle("My Frame");
this.setLayout(null);
this.addKeypadUI(6,4);
//this.addDisplay();
this.setVisible(true);
}
private void addKeypadUI(int rows, int cols){
JPanel panel = new JPanel();
panel.setBackground(Color.red);
panel.setBounds(0,0,200,300);
GridLayout gridLayout = new GridLayout(rows, cols);
panel.setLayout(gridLayout);
for (int c = 0; c < rows; c++) {
for (int r = 0; r < cols; r++) {
KeypadButton button = new KeypadButton(r,c);
button.setBackground(Color.BLACK);
//button.setBounds(25 * r + 10, 25 * c + 10, 20, 20);
button.setName(String.format("btn_%d_%d",r,c));
button.addActionListener(this);
panel.add(button);
}
}
this.add(panel);
}
// private void addDisplay(){
// JLabel jLabel = new JLabel();
// jLabel.setText("Hello?");
// jLabel.setForeground(Color.BLACK);
// jLabel.setBackground(Color.BLACK);
// jLabel.setBounds(250,20,110,110);
// this.add(jLabel);
//
// }
@Override
public void actionPerformed(ActionEvent e) {
KeypadButton o = (KeypadButton) e.getSource();
String name = o.getName();
System.out.println(name);
System.out.println(String.format("x: %d",o.getX()));
System.out.println(String.format("y: %d",o.getY()));
}
}
类
javax.swing.JComponent
有一个getX()
方法和一个getY()
方法JButton
是JComponent
的子类,因此继承了这些方法。因此,KeypadButton
类将覆盖这些方法并返回不正确的值。您希望x
成为网格中按钮的行,y
成为列,因此只需更改方法的名称,甚至类似于:
public int get_X(){
归还这个.x;
}
公共int get_Y(){
把这个还给我;
}
Classjavax.swing.JComponent
有一个getX()
方法和一个getY()
方法JButton
是JComponent
的子类,因此继承了这些方法。因此,KeypadButton
类将覆盖这些方法并返回不正确的值。您希望x
成为网格中按钮的行,y
成为列,因此只需更改方法的名称,甚至类似于:
public int get_X(){
归还这个.x;
}
公共int get_Y(){
把这个还给我;
}
啊,好的,谢谢!但是为什么按钮会跳到鼠标上方的正确位置呢?啊,好的,谢谢!但是为什么按钮会跳到鼠标上方的正确位置呢?
package com.company;
import javax.swing.*;
import java.awt.*;
public class KeypadButton extends JButton {
private int x;
private int y;
KeypadButton(int x, int y){
this.x = x;
this.y = y;
}
public int getX(){
return this.x;
}
public int getY(){
return this.y;
}
}