在 Java 中向 jpanel 添加动态按钮
Add dynamic buttons to jpanel in Java
我正在尝试在 Java 中编写一个程序,该程序将使一个按钮出现在 JPanel 中的位置和时间。为此,我有方法 运行,代码如下:
public void run(){
panel.addMouseListener(new MouseAdapter() {
public void mousePressed(MouseEvent e){
JButton button = new JButton();
button.setVisible(true);
button.setAlignmentX(e.getXOnScreen());
button.setAlignmentY(e.getYOnScreen());
panel.add(button);
panel.revalidate();
panel.repaint();
}
});
}
问题是,无论我点击哪里,按钮都不会出现。
此代码应在单击面板时显示一个按钮。它不会让它出现在光标处,但应该很容易添加。每次单击面板时,它也会创建一个新按钮。如果您只想要一个按钮,只需将此行 JButton button = new JButton();
移到 mousePressed 事件
之外
public static void main(String[] args) {
JFrame frame = new JFrame();
frame.setSize(500, 500);
JPanel panel = new JPanel();
panel.setSize(500, 500);
frame.add(panel);
frame.show();
run(panel);
}
public static void run(JPanel panel){
panel.addMouseListener(new MouseAdapter() {
public void mousePressed(MouseEvent e){
JButton button = new JButton();
button.setVisible(true);
panel.add(button);
panel.revalidate();
}
});
}
a button appears in a JPanel where and when it's clicked.
button.setAlignmentX(e.getXOnScreen());
button.setAlignmentY(e.getYOnScreen());
您使用了一些不正确的方法。
要在面板中定位组件,您需要使用:
button.setLocation(...);
但是,您不能使用 getXOnScreen() 方法,因为它与屏幕相关。您需要相对于面板定位组件。所以你需要使用:
button.setLocation(e.getX(), e.getY());
这仍然不够,因为当您使用空布局时,您还负责确定组件的大小。所以你还需要使用:
button.setSize( button.getPreferredSize() );
您还需要确保面板使用的是空布局,否则布局管理器将覆盖 size/location。
这对我有用(即使没有添加按钮标签)
public static void main(String[] args) {
final JPanel panel = new JPanel();
JFrame frame = new JFrame("test swing");
frame.setAlwaysOnTop(true);
frame.setSize(400, 200);
frame.add(panel);
frame.setVisible(true);
panel.addMouseListener(new MouseAdapter() {
@Override
public void mousePressed(MouseEvent e){
JButton button = new JButton();
button.setVisible(true);
button.setAlignmentX(e.getXOnScreen());
button.setAlignmentY(e.getYOnScreen());
panel.add(button);
panel.revalidate();
panel.repaint();
}
});
}
我正在尝试在 Java 中编写一个程序,该程序将使一个按钮出现在 JPanel 中的位置和时间。为此,我有方法 运行,代码如下:
public void run(){
panel.addMouseListener(new MouseAdapter() {
public void mousePressed(MouseEvent e){
JButton button = new JButton();
button.setVisible(true);
button.setAlignmentX(e.getXOnScreen());
button.setAlignmentY(e.getYOnScreen());
panel.add(button);
panel.revalidate();
panel.repaint();
}
});
}
问题是,无论我点击哪里,按钮都不会出现。
此代码应在单击面板时显示一个按钮。它不会让它出现在光标处,但应该很容易添加。每次单击面板时,它也会创建一个新按钮。如果您只想要一个按钮,只需将此行 JButton button = new JButton();
移到 mousePressed 事件
public static void main(String[] args) {
JFrame frame = new JFrame();
frame.setSize(500, 500);
JPanel panel = new JPanel();
panel.setSize(500, 500);
frame.add(panel);
frame.show();
run(panel);
}
public static void run(JPanel panel){
panel.addMouseListener(new MouseAdapter() {
public void mousePressed(MouseEvent e){
JButton button = new JButton();
button.setVisible(true);
panel.add(button);
panel.revalidate();
}
});
}
a button appears in a JPanel where and when it's clicked.
button.setAlignmentX(e.getXOnScreen());
button.setAlignmentY(e.getYOnScreen());
您使用了一些不正确的方法。
要在面板中定位组件,您需要使用:
button.setLocation(...);
但是,您不能使用 getXOnScreen() 方法,因为它与屏幕相关。您需要相对于面板定位组件。所以你需要使用:
button.setLocation(e.getX(), e.getY());
这仍然不够,因为当您使用空布局时,您还负责确定组件的大小。所以你还需要使用:
button.setSize( button.getPreferredSize() );
您还需要确保面板使用的是空布局,否则布局管理器将覆盖 size/location。
这对我有用(即使没有添加按钮标签)
public static void main(String[] args) {
final JPanel panel = new JPanel();
JFrame frame = new JFrame("test swing");
frame.setAlwaysOnTop(true);
frame.setSize(400, 200);
frame.add(panel);
frame.setVisible(true);
panel.addMouseListener(new MouseAdapter() {
@Override
public void mousePressed(MouseEvent e){
JButton button = new JButton();
button.setVisible(true);
button.setAlignmentX(e.getXOnScreen());
button.setAlignmentY(e.getYOnScreen());
panel.add(button);
panel.revalidate();
panel.repaint();
}
});
}